zoukankan      html  css  js  c++  java
  • java-TransDemo

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

     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 }
  • 相关阅读:
    PHP数字补零的两种方法
    php 防跨站表单提交
    PHP字符串压缩
    原生JS实现表单序列化serialize()
    java并发实现原子操作
    Effective java -- 9 并发/序列化
    Effective java -- 8 异常
    Effective java -- 7 通用程序设计
    Effective java -- 6 方法
    Effective java -- 5 枚举和注解
  • 原文地址:https://www.cnblogs.com/DeRozan/p/7203444.html
Copyright © 2011-2022 走看看