Chinaunix首页 | 论坛 | 博客
  • 博客访问: 199955
  • 博文数量: 47
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1259
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-24 10:20
文章分类
文章存档

2014年(21)

2013年(26)

分类: Java

2014-03-17 16:14:28

在java中,为了得到安全的数据输入,例如 int float double char ...需要先输入一个字符串,然后运用Double Float Integer 类提供的Xxx.praseXxx();方法得到所需的数据。一下是方法:

package exercise;

import java.io.*;

import java.util.*;

public class e3 {

public static void main(String[]args)

{

System.out.println("enter a int");

int a=getInt();

System.out.println("enter a float");

float b=getFloat();

System.out.println("enter a char");

char c=getChar();

System.out.println("enter a double");

double d=getDouble();

System.out.println("input");

System.out.println(a+" "+b+" "+c+" "+d);

}

public static String getString() throws IOException//得到一个字符串的函数

{

InputStreamReader str=new InputStreamReader(System.in);

BufferedReader s1=new BufferedReader(str);

String f=s1.readLine();

return f;

}

public static int getInt() throws IOException//得到int的函数

{

String s=getString();

return Integer.parseInt(s);

}

public static char getChar()throws IOException//得到char的函数

{

String s=getString();

return s.charAt(0);

}

public static double getDouble()throws IOException//得到double的函数

{

String s=getString();

return Double.parseDouble(s);

}

public static float getFloat()throws IOException//得到float的方法

{

String s=getString();

return Float.parseFloat(s);

}
阅读(1470) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~