Java-我如何继续要求用户输入?

问题描述 投票:0回答:1

我已经在这个程序上学习了几个小时,但我仍然不了解其逻辑。该程序要求用户输入整数。

一旦用户输入了整数,它将向用户输出所有正数,负数和输入的总数,以及数字的平均值。

这是我被困的地方:

然后,它将询问用户是否要继续。用户将输入'y''n'来指示他们是否愿意继续。如果用户输入“ y”,则循环将再次进行;如果用户输入“ n”,则将显示再见消息。我不知道该如何设置。

我已经尝试过do while循环,而我目前正在对该程序使用while循环。我还缺少一条语句,该语句让用户知道如果没有输入其他数字,他们只会输入零。我也不知道如何设置它,但是我尝试了if语句,但是它不起作用。我的代码如下。我正在使用Java 8。

公共类CountPositiveNegativeAndAverageOfIntsProgram {

public static void main(String [] args)
{

     System.out.print("Enter an integer, the input ends if it is 0: ");
     //Creating a scanner object
    Scanner input = new Scanner(System.in);
    int userInput = input.nextInt();

    //creating variables that will be used in the program.
    int countPositive = 0;
    int countNegative = 0;
    int total = 0;
    double average = 0;

    while(userInput != 0)
    {            

        if(userInput > 0)
        {
        //using a counter to add how many positive numbers are entered.
        countPositive++;
        }
        else
        {
          countNegative++;      
        }

        total += userInput;
        //I added 0.0 to avoid integer division. This felt like a small hack.
        average = total / (countPositive + countNegative + 0.0);
        userInput = input.nextInt();

    }      

     System.out.println("The number of positives is: " + countPositive);
     System.out.println("The number of negatives is: " + countNegative);
     System.out.println("The total is: " + total);
     System.out.println("The average is : " + average);

     System.out.print("Continue? (y/n) ");
     String s = input.next();




}

}

java if-statement while-loop do-while
1个回答
1
投票

一个解决方案,当然不是一个好方法,只是创建了一个函数,当用户提供y作为响应时,您可以在其中递归调用。

    public static void main(String [] args) {
        request();
    }

    private static void request() {
        System.out.println("Enter an integer, the input ends if it is 0: ");
        //Creating a scanner object
        Scanner input = new Scanner(System.in);
        int userInput = input.nextInt();

        //creating variables that will be used in the program.
        int countPositive = 0;
        int countNegative = 0;
        int total = 0;
        double average = 0;

        while(userInput != 0)
        {

            if(userInput > 0)
            {
                //using a counter to add how many positive numbers are entered.
                countPositive++;
            }
            else
            {
                countNegative++;
            }

            total += userInput;
            //I added 0.0 to avoid integer division. This felt like a small hack.
            average = total / (countPositive + countNegative + 0.0);
            userInput = input.nextInt();

        }

        System.out.println("The number of positives is: " + countPositive);
        System.out.println("The number of negatives is: " + countNegative);
        System.out.println("The total is: " + total);
        System.out.println("The average is : " + average);

        System.out.println("Continue? (y/n) ");

        String response = input.next();

        if (response.equals("y")) {
            request();
        } else {
            System.out.println("Goodbye!");
        }
    }

Java 8解决方案如果您确实想对此有所改善,则可以做一些事情。

  • 使用Scanner#hasNextInt确定扫描仪是否有要使用的Int。
  • 而不是使用多个变量来跟踪负,正和总数,而是维护代表输入的Integer对象的列表。
  • 使用Stream#filter检查正值和负值,然后使用count()告诉我们Stream中与此过滤器匹配的元素数。
  • 使用IntStream#sum和IntStream#average分别确定整数流的和和平均值。
    private static void request(Scanner scanner) {
        System.out.println("Enter an integer, the input ends if it is 0: ");

        List<Integer> values = new ArrayList<>();

        while(scanner.hasNextInt()) {
            int value = scanner.nextInt();

            if (value == 0) {
                break;
            }
            values.add(value);
        }
        System.out.println("The number of positives is: " + values.stream().filter(value -> value > 0).count());
        System.out.println("The number of negatives is: " + values.stream().filter(value -> value < 0).count());
        System.out.println("The total is: " + values.stream().mapToInt(value -> value).sum());
        System.out.println("The average is : " + values.stream().mapToInt(value -> value).average().orElse(0));
        System.out.println("Continue? (y/n) ");

        String response = scanner.next();

        if (response.equals("y")) {
            request(scanner);
        } else {
            System.out.println("Goodbye!");
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.