要求用户输入一个whole number 和非负数,然后验证用户输入的是否正确
下面的是例子。 谢谢。 Enter the distance in centimeters
553
553 cm = 0 miles, 6 yards, 0 feet, and 1.79 inches Enter the distance in centimeters
-5
Exception in thread "main" java.lang.RuntimeException: Value out of range
.... Enter the distance in centimeters
2.5
Exception in thread "main" java.util.InputMismatchException
... Enter the distance in centimeters
a
Exception in thread "main" java.util.InputMismatchException
下面的是例子。 谢谢。 Enter the distance in centimeters
553
553 cm = 0 miles, 6 yards, 0 feet, and 1.79 inches Enter the distance in centimeters
-5
Exception in thread "main" java.lang.RuntimeException: Value out of range
.... Enter the distance in centimeters
2.5
Exception in thread "main" java.util.InputMismatchException
... Enter the distance in centimeters
a
Exception in thread "main" java.util.InputMismatchException
代码:
Pattern p = Pattern.compile("([0-9]*)?");
Matcher m = p.matcher("12133");
boolean b=m.matches();
b为true表示满足要求,否则就不满足
12133是用户输入的数字,你可以测试一下
float inch = yard * 36;
output.println(inch);
我想让inch精确到小数点后面两位,应该如何做啊?
//四舍五入方法
参数:d_val被精确的浮点数,i_sit 保留位数
public double getRound(double d_val,int i_sit){
String ls_temp = "";
ls_temp = String.valueOf(d_val);
int li_index = ls_temp.indexOf(".");
int li_len = ls_temp.length();
int li_weak = li_len - (li_index + 1);
String ls_str;
//位数
char ch_index;
double d_unit,d_temp=d_val;
String ls_subStr=ls_temp;
for (int i=li_len-1; i>=li_index; i--){ ch_index = ls_temp.charAt(i);
li_weak = i - li_index;
ls_str = "0.";
if (li_weak == i_sit){
ls_subStr = ls_subStr.substring(0,i+1);
d_temp = Double.parseDouble(ls_subStr);
break;
}
if (Integer.parseInt(String.valueOf(ch_index))>=5){
for (int l=0;l<li_weak-1;l++){
if (l != li_weak -2)
ls_str = ls_str + "0";
else
ls_str = ls_str + "1";
}
d_unit = Double.parseDouble(ls_str);
d_temp = Double.parseDouble(ls_subStr) + d_unit;
ls_subStr = String.valueOf(d_temp);
continue;
}
else{
ls_subStr = ls_subStr.substring(0,i);
d_temp = Double.parseDouble(ls_subStr);
continue;
}
}
return d_temp;
}
→ 1、一边上网,一边赚钱!
→ 2、及时了解最新信息;
→ 3、自由参加各种网络拍卖,赢得巨大乐趣;
→ 4、免费参与多个广告商随时开展的各种抽奖活动...
心动?立刻行动!点击这里,免费注册:http://www.ads4cn.com/newsbar/refferer.asp?elflovebobo
算了,看你着急,还是告诉你吧
float pai=3.14159265;
int len=2;
pai=
(new java.math.BigDecimal(pai).divide(new java.math.BigDecimal(1),len,0)).floatValue();
可以结帖了吧
是不是bigDecimal这个method提供这个功能? 好长一句啊,看不懂,详细解释一下嘛。我求渔 不求鱼
java.text.DecimalFormat nf = new java.text.DecimalFormat("0.00");
pai=Double.parseDouble(nf.format(pai));