让数组存储用户输入,然后打印最大值

我正在创建一个程序,让用户输入 5 个介于 0-100 之间的值并将它们存储在一个数组中,以便可以打印最高值。问题是程序要求用户输入 5 个值后,它不会输出任何内容。


import java.util.ArrayList;

import java.util.Scanner;


public class HighestGrade {


    public static void main(String[] args){

        Scanner scan =  new Scanner(System.in);

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

        int greatest = -1;




        for (int i=0; i<5; i++) {

            System.out.print("Enter a grade (between 0 and 100): ");

            scan.nextInt();

        }


        while (scores.size()<5) {

            int input = scan.nextInt();

            if (input <= 100 && input >= 00) {

                scores.add(input);

                if(input >= greatest)

                    greatest = input;


            }

            else{

                System.out.println("Error: Make sure the grade is between 0 and 100!\nEnter a new grade!");

            }

        }


        System.out.println("\nHighest grade: "+greatest);


    }

}


白衣非少年
浏览 138回答 4
4回答

叮当猫咪

您没有将用户输入存储在for循环中的数组中。同样在 while 循环中,您再次要求用户输入。所以删除你的 for 循环。此外,无需存储输入即可找到最大值。只有一个变量就足够了。这是用于查找最大值的未经测试的代码。import java.util.ArrayList;import java.util.Scanner;public class HighestGrade {&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; Scanner scan =&nbsp; new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int greatest = -1;&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (count<5) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter a number: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int input = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input <= 100 && input >= 00) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(input >= greatest)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greatest = input;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error: Make sure the grade is between 0 and 100!\nEnter a new grade!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nHighest grade: "+greatest);&nbsp; &nbsp; }}

Qyouu

分数数组列表为空。您忘记在数组中插入值。&nbsp; &nbsp; for (int i=0; i<5; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print("Enter a grade (between 0 and 100): ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int temp = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input <= 100 && input >= 00) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( temp > greatest )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greatest = temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error: Make sure the grade is between 0 and&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100!\nEnter a new grade!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

一只名叫tom的猫

这不需要两个循环。在 for 循环中,您只需读取值。所以你可以简单地删除它。像这样尝试import java.util.ArrayList;import java.util.Scanner;public class HighestGrade {&nbsp; public static void main(String[] args){&nbsp; &nbsp; Scanner scan =&nbsp; new Scanner(System.in);&nbsp; &nbsp; ArrayList<Integer> scores = new ArrayList<Integer>();&nbsp; &nbsp; int greatest = -1;&nbsp; &nbsp; while (scores.size()<5) {&nbsp; &nbsp; &nbsp; System.out.print("Enter a grade (between 0 and 100): ");&nbsp; &nbsp; &nbsp; int input = scan.nextInt();&nbsp; &nbsp; &nbsp; if (input <= 100 && input >= 00) {&nbsp; &nbsp; &nbsp; &nbsp; scores.add(input);&nbsp; &nbsp; &nbsp; &nbsp; if(input >= greatest)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greatest = input;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error: Make sure the grade is between 0 and 100!\nEnter a new grade!");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("\nHighest grade: "+greatest);&nbsp; }}

哔哔one

问题似乎出在这里,您没有在 for 循环中将输入值添加到 ArrayList 分数中。这意味着第五个输入仅添加到列表中并被考虑。所以对于这段代码,没有打印出最大值。只有最后一个值作为输入。for (int i=0; i<5; i++) {&nbsp; &nbsp; System.out.print("Enter a grade (between 0 and 100): ");&nbsp; &nbsp; scores.add(scan.nextInt());}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java