Java语言的输入判断整数问题,求大佬看看
Java语言程序中一输入非整数(字母或者小数)就报错,我明明设置了检测,却没用,求大佬帮帮忙importjava.util.Scanner;importjava.util...
Java语言 程序中一输入非整数(字母或者小数)就报错,我明明设置了检测,却没用,求大佬帮帮忙
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class isTriangle {
public static boolean isNum(String str){
Pattern pat = Pattern.compile("[0-9]*");
Matcher mat = pat.matcher(str);
if( !mat.matches() ){
System.out.println("输入非法!请输入正整数!");
return false;
}
return true;
}
/*public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
} */
public static void TriangleType(String A,String B,String C){
int a=Integer.parseInt(A);
int b=Integer.parseInt(B);
int c=Integer.parseInt(C);
if(a+b<=c||a+c<=b||b+c<=a){
System.out.println("不能构成三角形!");
}else if((a==b&&b!=c)||(b==c&&a!=b)||(a==c&&b!=a)){
System.out.println("构成等腰三角形!");
}else if(a==b&&b==c){
System.out.println("构成等边三角形!");
}else{
System.out.println("构成一般三角形!");
}
}
public static void main(String args[]){
String[] x=new String[3];
System.out.println("请输入三条边的数值:");
Scanner sc=new Scanner(System.in);
for(int i=0;i<x.length;i++){
x[i]=sc.next();
if(Integer.parseInt(x[i])>100||Integer.parseInt(x[i])<1){
System.out.println("输入值超过了范围!");
}
isNum(x[i]);
}
System.out.print("三条边分别为:");
for(int i=0;i<3;i++){
System.out.print(x[i]+" ");
}
System.out.println();
TriangleType(x[0],x[1],x[2]);
}
} 展开
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class isTriangle {
public static boolean isNum(String str){
Pattern pat = Pattern.compile("[0-9]*");
Matcher mat = pat.matcher(str);
if( !mat.matches() ){
System.out.println("输入非法!请输入正整数!");
return false;
}
return true;
}
/*public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
} */
public static void TriangleType(String A,String B,String C){
int a=Integer.parseInt(A);
int b=Integer.parseInt(B);
int c=Integer.parseInt(C);
if(a+b<=c||a+c<=b||b+c<=a){
System.out.println("不能构成三角形!");
}else if((a==b&&b!=c)||(b==c&&a!=b)||(a==c&&b!=a)){
System.out.println("构成等腰三角形!");
}else if(a==b&&b==c){
System.out.println("构成等边三角形!");
}else{
System.out.println("构成一般三角形!");
}
}
public static void main(String args[]){
String[] x=new String[3];
System.out.println("请输入三条边的数值:");
Scanner sc=new Scanner(System.in);
for(int i=0;i<x.length;i++){
x[i]=sc.next();
if(Integer.parseInt(x[i])>100||Integer.parseInt(x[i])<1){
System.out.println("输入值超过了范围!");
}
isNum(x[i]);
}
System.out.print("三条边分别为:");
for(int i=0;i<3;i++){
System.out.print(x[i]+" ");
}
System.out.println();
TriangleType(x[0],x[1],x[2]);
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |