程序需要不断循环直到键入键“ Q” /“ q”

时间:2019-07-17 02:51:22

标签: java object java.util.scanner do-loops

程序提取数字,我希望它继续循环直到用户键入键“ Q” /“ q”。例如,当用户按下“ O”键时,程序应打印他们输入的数字的一位,以此类推,以显示用户输入的任何3位数字。当我现在运行代码时,没有输出,但是也没有错误。

import java.util.Scanner;
public class DigitExtractor {

    public static void main(String[] args) 
            throws java.io.IOException{

        char input;
        input = (char) System.in.read();
        Scanner s = new Scanner(System.in);

        variables Num = new variables();

        do {

            System.out.print("Enter an integer: ");
            String wholeNumber = s.nextLine();

            Num.ones = wholeNumber.charAt(2);
            Num.tens = wholeNumber.charAt(1);
            Num.hundred = wholeNumber.charAt(0);

            System.out.println("show (W)hole number.");
            System.out.println("show (O)nes place number.");
            System.out.println("show (T)ens place number.");
            System.out.println("show (H)undreds place number.");

            input = (char) System.in.read();
            System.out.println("Enter your choice: " + input);

            if(input == 'W' || input == 'w') {
                System.out.println(Num.WholeNum);
            }
            else if(input == 'O' || input == 'o') {
                System.out.println(Num.ones);
            }
            else if(input == 'T' || input == 't') {
                System.out.println(Num.tens);
            }
            else if(input == 'H' || input == 'H') {
                System.out.println(Num.hundred);

            }
        } while (input == 'q');
    }
}

class variables {
    char hundred; 
    char tens; 
    char ones;
    char WholeNum;
}

1 个答案:

答案 0 :(得分:1)

阅读越来越混乱。要使用扫描仪读取整数,我选择了nextInt。有帮助。我采用了您的方法,即不要将事情分解成小块。并且(修订)我仅使用扫描仪进行读取,甚至可以选择字符。在您必须按下该选项之前,我还会提示您。

       public static void main(String[] args)


 throws java.io.IOException {
       int hundred; //my compiler was fussy about having the extra class
       int tens;
       int ones;
       char input = ' '; //initialize outside loop
       Scanner s = new Scanner(System.in);



   do {

       System.out.print("Enter an integer: ");
       int wholeNumber = s.nextInt();

       ones = wholeNumber % 10;
       tens = (wholeNumber / 10) % 10;
       hundred = (wholeNumber / 100) % 10;

       System.out.println("show (W)hole number.");
       System.out.println("show (O)nes place number.");
       System.out.println("show (T)ens place number.");
       System.out.println("show (H)undreds place number.");
       System.out.println("(Q)uit");
       System.out.print("Enter your choice: ");
       input = s.next().trim().charAt(0); //using scanner only
       //System.out.println("Enter your choice: " + input);

       if (input == 'W' || input == 'w') {
           System.out.println(wholeNumber);
       } else if (input == 'O' || input == 'o') {
           System.out.println(ones);
       } else if (input == 'T' || input == 't') {
           System.out.println(tens);
       } else if (input == 'H' || input == 'H') {
           System.out.println(hundred);

       }
   } while ((input != 'q') && (input != 'Q'));


}

   }