码迷,mamicode.com
首页 > 编程语言 > 详细

java-TransDemo

时间:2017-07-19 00:19:42      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:oid   system.in   source   lang   port   stat   string   public   1.0   

字符串转换成其他基本类型,以及包装类的自动装箱、拆箱

 1 package com.example;
 2 import java.util.Scanner;
 3 import java.lang.*;
 4 /**
 5  * GuessDemo.java Description:判断输入的字符串是整数?小数?
 6  * 
 7  * @author raizoo
 8  * Created on 17-7-18 下午10:23
 9  * @version 1.0
10  * @since JDK8.0
11  * 
12  * @thows Exception: 无
13  */
14 public class GuessDemo {
15     public static void main(String[] args){
16         //输入字符串
17         Scanner scan = new Scanner(System.in);
18         System.out.print("输入字符串:");
19         String target = scan.nextLine();  //输入字符串
20         System.out.println();
21 
22         /*
23          * parseInt/parseDouble Description: 包装类-装箱/拆箱
24          * 其中,Integer.parseInt(String source)    解析字符串,转换为int型
25          *     Double.parseDouble(String source)  解析字符串,转换为double型
26          * @param String source
27          * @return 转换成的对应类型,如例中int/double
28          * @thows Exception: 无
29          */
30         if(target.matches("\\d+")){  //判断输入字符串是整数?
31             int integ = Integer.parseInt(target);
32             System.out.println("是整数:"+integ);
33         }else if(target.matches("\\d+\\.\\d+")){  //判断输入字符串是小数
34             double doub = Double.parseDouble(target);
35             System.out.println("是小数:"+doub);
36         }else{
37             System.out.println("不是数字");
38         }
39     }
40 }

 

java-TransDemo

标签:oid   system.in   source   lang   port   stat   string   public   1.0   

原文地址:http://www.cnblogs.com/DeRozan/p/7203444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!