【问题标题】:a for loop to run/stop program用于运行/停止程序的 for 循环
【发布时间】:2013-02-01 22:11:28
【问题描述】:

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

}

}

【问题讨论】:

  • 用户应该在哪里输入'q'?因为在我看来,您只允许输入数字。

标签: java for-loop char compare


【解决方案1】:

这是简化程序。

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(); }
}

【讨论】:

    【解决方案2】:

    你的循环应该是这样的:

    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 循环结束之前再次从用户那里获取输入。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        例外是因为

        a = in.nextInt();
        

        当您输入char 时,您会尝试读取int

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-07
          • 1970-01-01
          • 2019-10-22
          • 2022-01-11
          • 1970-01-01
          相关资源
          最近更新 更多