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

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

如何修復我的java代碼,不工作

如何修復我的java代碼,不工作

長風秋雁 2023-10-19 21:25:13
接受整數并將它們存儲在數組中并輸出其中最大數的程序。但以下代碼不起作用導入java.util.Scanner;公共類測試2 {public static void main(String[] args){    System.out.print("input the array'size : ");Scanner sc = new Scanner(System.in);int size = sc.nextInt();int arr[]=new int[size];System.out.print("input the array'value :");for(int i=0; i<size; i++){    int var = sc.nextInt();    arr[i] = var; }System.out.print("inputed value: ");for(int i=0; i<size; i++) {System.out.print(arr[i]+ " ");}int max = 0;for(int j=0; j<size; j++){    if(max<arr[j+1]){    max = arr[j];       }    else {    continue;    }}System.out.println("the largest number in array :"+ max);}}
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

您不需要再次循環數組來查找最大值。通過獲取輸入本身,我們可以通過與每個輸入進行比較來獲得最大數量。


public static void main(String[] args){

    System.out.print("input the array'size : ");

    Scanner sc = new Scanner(System.in);

    int size = sc.nextInt();

    int arr[]=new int[size];

    System.out.println("input the array'value :");

    int max = 0;

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

      int var = sc.nextInt();

      arr[i] = var; 

      if(var > max) {

          max = var;

      }

    }

    System.out.print("Input Array: "+Arrays.toString(arr));

    System.out.println("The largest number in array :"+ max);

 }


查看完整回答
反對 回復 2023-10-19
  • 1 回答
  • 0 關注
  • 99 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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