如何把字符串转化为数字?下面通过程序来演示
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.lang.math.NumberUtils;
public class ScoreDemo
{
public static void main(String[] args)
{
while (true)
{
try
{
System.out.println("请输入一个成绩:");
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
String str = br.readLine();
// 判断字符串是否是数字
boolean isnum = NumberUtils.isNumber(str);
if (isnum)
{
double score = Double.parseDouble(str);
System.out.println("score=" + str);
if (score > 90 && score <= 100)
{
System.out.println("优");
} else if (score > 80 && score <= 90)
{
System.out.println("良");
} else if (score > 70 && score <= 80)
{
System.out.println("中");
} else if (score >= 60 && score <= 70)
{
System.out.println("及格");
} else if (score >= 0 && score < 60)
{
System.out.println("差");
} else
{
System.out.println("输入错误");
}
} else
{
if (str.equals("exit"))
{
return;
}
System.out.println("输入非法");
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
|