想法:使用pannter 类检查键盘输入是否是数字,不然反复提示,知道输入数字再比较大小。
问题:不能反复检查键盘输入
请给指导一下://比较两个输入数字的大小
//判断键盘输入限定只能是数字
//校验结束后,比较数字大小
/////////////////////
import java.io.*;
import java.util.regex.Pattern;
import java.util.Scanner;;public class CompareValue {
// 使用Pattern 判断输入是否是数字/////
////正则表达式判断///////////////////
public static boolean isNumeric(String str){
Pattern pattern=Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
public static boolean isNumeric1(String strt){
for(int i=strt.length();--i>=0;){
int chr=strt.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
public static void main(String arg[]) throws IOException{
String firstnumber=null;
String secondnumber=null;
System.out.println("Please input first number");
try {
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
firstnumber=br1.readLine();
Scanner scanner =new Scanner(br1);
while (!isNumeric1(firstnumber)){
System.out.println("It is not numeric!Input it again.");
scanner.next();
} catch (IOException e1){
System.out.print(e1.getMessage());}
//
System.out.println("Please input second number");
try {
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
secondnumber=br2.readLine();
}
catch (IOException e2){
System.out.print(e2.getMessage());
}//字符串转为 数字类型才能比较实际的数字大小 //
//////////////////////////////////////
if(Double.parseDouble(firstnumber)>Double.parseDouble(secondnumber)){
System.out.println("from big to small :"+firstnumber+"\t"+secondnumber);
}
else{
System.out.println("from big to small :"+secondnumber+"\t"+firstnumber);
}
}
}
问题:不能反复检查键盘输入
请给指导一下://比较两个输入数字的大小
//判断键盘输入限定只能是数字
//校验结束后,比较数字大小
/////////////////////
import java.io.*;
import java.util.regex.Pattern;
import java.util.Scanner;;public class CompareValue {
// 使用Pattern 判断输入是否是数字/////
////正则表达式判断///////////////////
public static boolean isNumeric(String str){
Pattern pattern=Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
public static boolean isNumeric1(String strt){
for(int i=strt.length();--i>=0;){
int chr=strt.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
public static void main(String arg[]) throws IOException{
String firstnumber=null;
String secondnumber=null;
System.out.println("Please input first number");
try {
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
firstnumber=br1.readLine();
Scanner scanner =new Scanner(br1);
while (!isNumeric1(firstnumber)){
System.out.println("It is not numeric!Input it again.");
scanner.next();
} catch (IOException e1){
System.out.print(e1.getMessage());}
//
System.out.println("Please input second number");
try {
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
secondnumber=br2.readLine();
}
catch (IOException e2){
System.out.print(e2.getMessage());
}//字符串转为 数字类型才能比较实际的数字大小 //
//////////////////////////////////////
if(Double.parseDouble(firstnumber)>Double.parseDouble(secondnumber)){
System.out.println("from big to small :"+firstnumber+"\t"+secondnumber);
}
else{
System.out.println("from big to small :"+secondnumber+"\t"+firstnumber);
}
}
}
解决方案 »
- 为什么ch[i]改为ch之后就提示>=不能用于比较char,char[] 类型呢?
- 一个关于接口的java问题
- 新鲜的面试题,大家帮忙
- 如何把类的对象转成 byte []
- 如何刷新 Terminal Window
- 谁帮我找一找错呀
- 大家知道哪有java1.4.2的中文API和CHM啊,小弟初学JAVA,希望得到大家的支持
- classpath问题
- Connection reset by peer: JVM_recv in socket input stream read这个问题到底怎么解决??
- 使用java如何分析系统不能识别的字符串?
- might-be bug on synchronizing over Object
- this()和supper()
public static void main(String arg[]) throws IOException{
String firstnumber=null;
String secondnumber=null;
String flagToQuit = "C";
while (flagToQuit.equalsIgnoreCase("c")){
System.out.println("Please input first number");
Scanner scanner = new Scanner(System.in);
firstnumber = scanner.next();
while (!isNumeric1(firstnumber)){
System.out.println("It is not numeric!Input it again.");
firstnumber = scanner.next();
} System.out.println("Please input second number");
secondnumber = scanner.next();
while (!isNumeric1(secondnumber)){
System.out.println("It is not numeric!Input it again.");
secondnumber = scanner.next();
}
if(Double.parseDouble(firstnumber)>Double.parseDouble(secondnumber)){
System.out.println("from big to small :"+firstnumber+"\t"+secondnumber);
}
else{
System.out.println("from big to small :"+secondnumber+"\t"+firstnumber);
}
System.out.println("Press \"C\" or \"c\" to Continue, or other keys to quit.");
flagToQuit = scanner.next();
}
而且用Scanner就直接扫描System.in就可以了 所以我在main方法里改得比较多
double num1 = getNumber("请输入第一个数字:");
double num2 = getNumber("请输入第二个数字:");
System.out.println("结果:" + num1 + (num1 > num2 ? " 大于 " : (num1 == num2 ? " 等于 " : " 小于 ")) + num2);
} private static double getNumber(String prompt) {
while (true) {
System.out.print(prompt);
try {
return Double.parseDouble(new BufferedReader(new InputStreamReader(System.in)).readLine());
} catch (Exception e) {
System.out.println("您输入的不是数字。");
}
}
}
}