【问题标题】:How to make integer input appear after rather than before message that prompts user input?如何使整数输入出现在提示用户输入的消息之后而不是之前?
【发布时间】:2019-05-10 07:09:43
【问题描述】:

我正在创建一个程序,在该程序中反复要求用户输入输入,直到扫描仪读取 0。出于某种奇怪的原因,每当我输入一个整数时,它会出现在下一条要求输入的消息出现之前。解决此问题的任何帮助非常感谢。

    Scanner input = new Scanner(System.in);
    System.out.println("Enter an integer, the input ends if it is 0: ");

    int number;
    int posCount = 0;
    int negCount = 0;
    int total = 0;

    while((number = input.nextInt()) != 0) {
        System.out.println("Enter an integer, the input ends if it is 0:");
        if(number < 0) {

            negCount++; 

        }
        if(number > 0) {
            posCount++;

        }


        total += number;    
    }


    System.out.println("The number of negatives is " + negCount);
    System.out.println("The number of positives is " + posCount);
    System.out.println("The total is " + total);

【问题讨论】:

    标签: java while-loop java.util.scanner


    【解决方案1】:

    请使用System.out.print() 而不是System.out.println()

    System.out.print("Enter an integer, the input ends if it is 0: ");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 2012-11-10
      • 2019-10-05
      • 2023-03-15
      相关资源
      最近更新 更多