如何理解输入序列中最大数的位置?

hi3rlvi2  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(236)

我不明白如何从用户输入的一系列数字中找出最大的数字。
我创建了一个maxstinel.java来提示用户逐个输入数字。maxsentinel在检测到一个负数时会中断其循环。
我需要修改前面的代码并创建maxstinel2.java。
手头的任务是检查第一个条目是否有效。然后允许用户输入所需数量的数字。
如果输入的号码无效,请打印“invalid entry”(无效输入)。再次提示用户,直到输入有效号码。
一旦检测到负数,程序就会停止并打印输入的最大数字。

import java.util.*;

public class MaxSentinel2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("This program allows you to enter a series of numbers.");
        System.out.println("Enter -1 when you are finished entering your numbers and");
        System.out.println("the program will print the largest number entered.");
        System.out.println("Enter your first number, please.");
        System.out.print("Next integer (-1 to quit)? ");
        input.nextInt();
        while (!input.hasNextInt()) {
            System.out.println("You have entered an invalid response.");
            System.out.println("Enter your first number, please.");
            input.nextInt();
        }
        int number = input.nextInt();
        int sum = 0;

        while (number != -1) {
            sum += number;
            while (!input.hasNextInt()) {
                System.out.println("You have entered an invalid response.");
                System.out.println("Enter your first number, please.");
                input.nextInt();
            }
            number = input.nextInt();
        }
        int max = 0;
        if (max < number) {
            max++;
        }
        System.out.println("The largest number entered was: " + max);
    }
}
dxxyhpgq

dxxyhpgq1#

要存储所有输入的值,您需要创建一个arraylist来查找max input number,并且要检查输入是否正确及其编号,您需要添加一个 try-catch 块,通过使用流,您可以找到输入的最大值。
注意:您还可以通过读取以下链接使用for循环来查找最大值:
java–在数组中查找最小值和最大值

Scanner input = new Scanner(System.in);

System.out.println("This program allows you to enter a series of numbers.");
System.out.println("Enter -1 when you are finished entering your numbers and");
System.out.println("the program will print the largest number entered.");
System.out.println("Enter your first number, please.");

ArrayList<Integer> numbers = new ArrayList<Integer>();

String data = "";
while (!data.equals("-1")) {
    System.out.print("Next integer (-1 to quit)? ");
    data = input.next();

    try {
        int number = Integer.parseInt(data); 
        numbers.add(number);
    } catch(Exception e) {
        System.out.println("You have entered an invalid response.");
    }
}
int max = numbers.stream().mapToInt(v -> v).max().getAsInt();
System.out.println("The largest number entered was: " + max);

您也可以简单地解决问题,而不必使用 ArrayList ,和 stream :

Scanner input = new Scanner(System.in);

System.out.println("This program allows you to enter a series of numbers.");
System.out.println("Enter -1 when you are finished entering your numbers and");
System.out.println("the program will print the largest number entered.");
System.out.println("Enter your first number, please.");

String data = "";
int max = 0;
while (!data.equals("-1")) {
    System.out.print("Next integer (-1 to quit)? ");
    data = input.next();

    try {
        int number = Integer.parseInt(data);
        if(number > max) {
            max = number;
        }
    } catch (Exception e) {
        System.out.println("You have entered an invalid response.");
    }
}

System.out.println("The largest number entered was: " + max);
4jb9z9bj

4jb9z9bj2#

下面的部分没有任何意义。您可以删除此部分:

input.nextInt();
    while (!input.hasNextInt()) ;
    {
        System.out.println("You have entered an invalid response.");
        System.out.println("Enter your first number, please.");
        input.nextInt();
    }

还有,为什么要求所有数字的总和?我想这是抄写错误。您可以删除 sum 变量也是。

int max = 0;
if (max < number) 
{
  max++;  
}

以上部分可以简单地用 Math.max() 功能

System.out.println("The largest number entered was: " + Math.max(max, number));

如果要标识整个数字序列中最大的数字,则需要遍历整个数字序列才能标识最大的数字。这意味着,只有在完成循环(在while循环之外)时才能打印最大的数字。

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // How to use program
    System.out.println("This program allows you to enter a series of numbers.\n" +
            "Enter -1 when you are finished entering your numbers and \n" +
            "the program will print the largest number entered. \n" +
            "Please enter your numbers(-1 to quit) : ");
    int max = 0;
    try {
        int userInput = input.nextInt();

        while (userInput != -1) {
            max = Math.max(max, userInput);
            userInput = input.nextInt();
        }
    } catch (InputMismatchException e) {
        System.out.println("Program exiting as input was invalid!");
    }
    System.out.println("The largest number entered was: " + max);
}

相关问题