我想从用户那里获取税号,并检查我得到的数字是否正确,但是我有一个问题,.When,我运行程序,如果用户首先给出一个错误的号码(一个数字小于或超过九位数),我看到我写的消息是正确的,但是当用户在输入后给出一个正确的数字,我再次看到消息,循环永远不会stops.If,我会感激它,因为我被卡住了:)
在这里输入图像描述在这里输入图像描述
发布于 2022-06-08 13:15:42
当你第二次在while循环中得到输入时,问题就发生了.因为你不会再检查锡号了。我建议您创建一个函数来检查TIN是否有效,并使用while循环不断地获取输入。
就像这样:
public static void main(String[] args) {
System.out.println("Give me your TIN number");
Scanner in = new Scanner(System.in);
long TIN = in.nextLong();
//while the input is not valid ("!" negates the boolean) print invalid input and get the input again
while(!isValidTIN(TIN)){
System.out.println("Wrong number! Try Again");
TIN = in.nextLong();
};
// Countinue with your program
System.out.println("Success");
}
public static boolean isValidTIN(long TIN){
int count = 0;
while(TIN > 0){
TIN /= 10; //You can also use TIN /= 10 instead of TIN = TIN / 10
count++;
}
return count == 9;
}下一次粘贴代码,而不是粘贴图像
https://stackoverflow.com/questions/72545833
复制相似问题