亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

讓數組存儲用戶輸入,然后打印最大值

讓數組存儲用戶輸入,然后打印最大值

白衣非少年 2023-04-13 15:47:44
我正在創建一個程序,讓用戶輸入 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);    }}
查看完整描述

4 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

您沒有將用戶輸入存儲在for循環中的數組中。同樣在 while 循環中,您再次要求用戶輸入。所以刪除你的 for 循環。此外,無需存儲輸入即可找到最大值。只有一個變量就足夠了。這是用于查找最大值的未經測試的代碼。


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);


    }

}


查看完整回答
反對 回復 2023-04-13
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

分數數組列表為空。您忘記在數組中插入值。


    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!");

                } 

            }


查看完整回答
反對 回復 2023-04-13
?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

這不需要兩個循環。在 for 循環中,您只需讀取值。所以你可以簡單地刪除它。像這樣嘗試


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);


  }

}


查看完整回答
反對 回復 2023-04-13
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

問題似乎出在這里,您沒有在 for 循環中將輸入值添加到 ArrayList 分數中。這意味著第五個輸入僅添加到列表中并被考慮。所以對于這段代碼,沒有打印出最大值。只有最后一個值作為輸入。


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

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

    scores.add(scan.nextInt());

}


查看完整回答
反對 回復 2023-04-13
  • 4 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號