0

我正在编写一个比较两个整数的程序,我希望程序运行直到用户感到疲倦并想要退出,为此用户将输入 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));
    }

}

}
4

4 回答 4

1

你的循环应该是这样的:

try{  
    btn = (char) System.in.read();
    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));

   System.out.println("enter 'q' to quit program and any other key to start it again");
   btn = (char) System.in.read();
  } // END OF WHILE LOOP
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

while另请注意,您需要在循环结束之前再次从用户那里获取输入。

于 2013-02-17T10:23:29.687 回答
1

那是因为在 while 循环中,您正在扫描整数,但“q”是一个字符。您需要让用户有机会在 while 循环中btn 阅读

于 2013-02-17T10:24:06.897 回答
0

btn检查时需要更改。您只在循环之前设置一次,并且永远不会更新。

例外是因为

a = in.nextInt();

int当您输入 a 时,您尝试阅读char

您需要在循环末尾添加如下内容:

btn = (char) System.in.read();
于 2013-02-17T10:23:09.280 回答
0

这是简化的程序。

public static void main(String[] args){
    // Scanner
    Scanner s = new Scanner(System.in);
    // Enter the program
    System.out.println("enter 'q' to quit program and any other key to start it");
    int a, b;
    try {
        char key;
        while ((key = s.next().charAt(0))!='q'){
            System.out.println("enter two numbers to compare them");
            System.out.println("please enter first number:");
            a = s.nextInt();
            System.out.println("please enter second number:");
            b = s.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));
        }
    } catch (Exception e){ e.printStackTrace(); }
}
于 2013-02-17T10:35:11.737 回答