一个for循环来运行/停止程序

时间:2013-02-17 10:19:12

标签: java for-loop char compare

即时编写一个比较两个整数的程序,我希望程序运行直到用户厌倦并想要退出,为此,用户将输入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 个答案:

答案 0 :(得分: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循环结束前再次收到用户的输入。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

例外是因为

a = in.nextInt();

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

你需要在循环结束时添加类似:

btn = (char) System.in.read();

答案 3 :(得分: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(); }
}