我正在编写一个比较两个整数的程序,我希望程序运行直到用户感到疲倦并想要退出,为此用户将输入 q 退出。我想编写一个while循环来比较两个字符,一个是用户输入的字符,另一个是分配给它的'q'。但是,当我输入 q 时,我的程序崩溃了,为什么会这样,我该如何解决
import java.io.IOException;
import java.util.Scanner;
public class numComp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a, b;
char key = 'q';
System.out.println("enter 'q' to quit program and any other key to start it");
char btn = 0;
try {
btn = (char) System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (btn != key){
System.out.println("enter two numbers to compare them");
System.out.println("please enter first number:");
a = in.nextInt();
System.out.println("please enter second number:");
b = in.nextInt();
if (a==b)
System.out.println("the numbers you entered are the same");
else if (a>b)
System.out.println("your first number is greater then your second number by "+ (a-b));
else
System.out.println("your first number is smaller then your second number by "+ (b-a));
}
}
}