Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何理解输入序列中最大数字的位置?_Java - Fatal编程技术网

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

Java 如何理解输入序列中最大数字的位置?,java,Java,我不明白如何从用户输入的一系列数字中找到最大的数字 我创建了一个MaxSentinel.java,提示用户逐个输入数字。MaxSentinel在检测到负循环时会中断其循环 我需要修改前面的代码并创建maxstinel2.java 手头的任务是检查第一个条目是否有效。然后允许用户输入所需数量的数字 如果输入的数字无效,请打印“无效输入” 再次提示用户,直到输入有效数字 一旦检测到负数,程序将停止并打印输入的最大数字 import java.util.*; 公共类MaxSentine2{ 公共静态

我不明白如何从用户输入的一系列数字中找到最大的数字

我创建了一个MaxSentinel.java,提示用户逐个输入数字。MaxSentinel在检测到负循环时会中断其循环

我需要修改前面的代码并创建maxstinel2.java

手头的任务是检查第一个条目是否有效。然后允许用户输入所需数量的数字

如果输入的数字无效,请打印“无效输入” 再次提示用户,直到输入有效数字

一旦检测到负数,程序将停止并打印输入的最大数字


import java.util.*;
公共类MaxSentine2{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“此程序允许您输入一系列数字。”);
System.out.println(“输入完数字后输入-1”和“);
System.out.println(“程序将打印输入的最大数字”);
System.out.println(“请输入您的第一个号码”);
System.out.print(“下一个整数(-1)退出)?”;
input.nextInt();
而(!input.hasNextInt()){
System.out.println(“您输入了无效的响应。”);
System.out.println(“请输入您的第一个号码”);
input.nextInt();
}
int number=input.nextInt();
整数和=0;
while(数字!=-1){
总和+=数字;
而(!input.hasNextInt()){
System.out.println(“您输入了无效的响应。”);
System.out.println(“请输入您的第一个号码”);
input.nextInt();
}
number=input.nextInt();
}
int max=0;
如果(最大值<数量){
max++;
}
System.out.println(“输入的最大数字为:“+max”);
}
}

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

    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++;  
}
如果要标识整个数字序列中的最大数字,则需要遍历整个数字序列以标识最大数字。这意味着,只有在完成循环时(在while循环之外),才能打印最大的数字


要存储所有输入的值,您需要创建一个arrayList以查找最大输入编号,要检查输入是否正确及其编号,您需要添加一个
try catch
块,并使用stream查找输入的最大值

注意:您还可以通过读取以下链接,使用for循环查找最大值:


您需要维护一个
max
变量。当数字出现时,如果输入的数字大于
max
那么更新该
max
值,为什么不在
循环中跟踪输入的最大数字呢?我有一种感觉,流API对于OP来说可能有点高级。而且,我不认为需要保存用户输入的所有数字。@Abra是的,它还可以跟踪for循环中的最大值,您是对的,它也很简单,不使用流。@Abra编辑答案只是为了处理
输入不匹配异常。程序失败的原因可能有很多,但OP只对程序工作流感兴趣,因为它是一项家庭作业。问题中的一句话:如果输入了无效数字,请打印“无效输入”。再次提示用户,直到输入了有效数字。您的代码不会再次提示用户,它只是退出。
System.out.println("The largest number entered was: " + Math.max(max, number));
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);
}
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);
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);