如何把字符串转化为数字?下面通过程序来演示
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.lang.math.NumberUtils;
public class WeekDemo
{
public static void main(String[] args)
{
try
{
while (true)
{
System.out.println("请输入一个整数:");
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
String str = br.readLine();
// 判断字符串是否是数字
boolean isnum = NumberUtils.isNumber(str);
ok: if (isnum)
{
int num = Integer.parseInt(str);
switch (num)
{
case 1:
System.out.println("星期1");
break ok;
case 2:
System.out.println("星期1");
break ok;
case 3:
System.out.println("星期3");
break ok;
case 4:
System.out.println("星期4");
break ok;
case 5:
System.out.println("星期5");
break ok;
case 6:
System.out.println("星期6");
break ok;
case 7:
System.out.println("星期日");
break ok;
default:
System.out.println("输入错误");
break ok;
}
} else
{
if (str.equals("exit"))
{
return;
}
System.out.println("输入非法");
}
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
|