简体   繁体   English

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

[英]Have array store user input and then print the largest value

I am creating a program that has the user input 5 values between 0-100 and store them in an array, so that the highest value can be printed.我正在创建一个程序,让用户输入 0-100 之间的 5 个值并将它们存储在一个数组中,以便可以打印最高值。 The problem is that after the program asks the user to input the 5 values, it does not output anything after that.问题是程序要求用户输入 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);

    }
}

Scores array list is empty.分数数组列表为空。 You forgot to insert values inside array.您忘记在数组中插入值。

    for (int i=0; i<5; i++) {
                 System.out.print("Enter a grade (between 0 and 100): ");
                  int temp = scan.nextInt();         
                if (input <= 100 && input >= 00) {
                  if( temp > greatest )
                      greatest = temp;
                 }
               else{
            System.out.println("Error: Make sure the grade is between 0 and 
                100!\nEnter a new grade!");
                } 
            }

You are not storing the user inputs in an array in your for loop.您没有将用户输入存储在for循环中的数组中。 Also in while loop, you are again asking for user inputs.同样在 while 循环中,您再次要求用户输入。 So remove your for loop.所以删除你的 for 循环。 Also, there is no need to store inputs in order to just find the maximum.此外,不需要为了找到最大值而存储输入。 Only one variable is enough.只有一个变量就足够了。 Here is untested code for finding maximum.这是用于查找最大值的未经测试的代码。

import java.util.ArrayList;
import java.util.Scanner;

public class HighestGrade {

    public static void main(String[] args){
        Scanner scan =  new Scanner(System.in);
        int greatest = -1;
        int count = 0;
        while (count<5) {
            ++count;
            System.out.print("Enter a number: ");
            int input = scan.nextInt();
            if (input <= 100 && input >= 00) {
                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);

    }
}

This is not required two loops.这不需要两个循环。 In the for loop you just read values.在 for 循环中,您只需读取值。 so you can simply remove it.所以你可以简单地删除它。 And try like this像这样尝试

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;

    while (scores.size()<5) {
      System.out.print("Enter a grade (between 0 and 100): ");
      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);

  }
}

The problem seems to be here is you are not adding the input values to the ArrayList scores at the for loop.问题似乎在这里是您没有将输入值添加到 for 循环中的 ArrayList 分数。 That means the fifth input only added to the list and considered.这意味着第五个输入只添加到列表中并被考虑。 So for this piece of code the greatest value is not printed.所以对于这段代码,不打印最大值。 Only having last value as the input.仅将最后一个值作为输入。

for (int i=0; i<5; i++) {
    System.out.print("Enter a grade (between 0 and 100): ");
    scores.add(scan.nextInt());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM