2017-08-17 68 views
0
package HW2_Min_Max; 

import java.util.Scanner; 

public class HW2_Min_Max { 

    public static void main(String[] args) { 

     Scanner myScanner = new Scanner(System.in); 

     System.out.println("Please input a positive interger that indicates number of positive intergers "); 

     int number = myScanner.nextInt(); 

     while (number <= 0) { 
      System.out.println("Please input interger"); 
      number = myScanner.nextInt(); 
     } 

     int i=1; //i is to store current iteration 
     int sum=0; //sum is to store sum of the input 
     int x; //x is to store the user input 

     while (i <= number){ 
      System.out.println("Please input a positive interger "); 
      x = myScanner.nextInt(); 
      sum = sum + x; 
      i++; 
     } 
     int average = sum/number; 

     System.out.println("The average is " + average); 

     int max = Integer.MIN_VALUE; 
     int min = Integer.MAX_VALUE; 

     if (number < min){ 
      min = number; 
     } 

     if (number > max) { 
      max = number; 
     } 
     System.out.println("The minimum value is " + min); 
     System.out.print("and the maximum value is" + max); 
     } 
    } 
} 

1. ^這是我得到我的問題,在Netbeans的最後一個大括號我得到一個錯誤,說: 「類,接口或枚舉的預期」,但我不知道爲什麼。請原諒我的無知,因爲我是一個非常新鮮的java初學者,更不用說編程了。錯誤與試圖找到最小和最大時,也發現「n」數量的用戶輸入的平均值

+1

它似乎最後的大括號並不是必需的。請記住,'{'的數量應該等於'}'的數量,您希望確保您打開的每個大括號最終都會關閉。 – dat3450

+0

正確,一致的縮進是你的朋友。 –

+0

好了,以便解決讓我運行代碼的問題,但是我的實際輸出出現了問題?我輸入5個正整數,平均值總是正確的,但最大值和最小值的輸出出錯了,我不知道爲什麼? –

回答

-1

現在工作的很好。我做了一些改變。請考慮一下。我希望這會有所幫助。

Scanner myScanner = new Scanner(System.in); 

    System.out.println("Please input a positive interger that indicates number of positive intergers "); 


    int number = myScanner.nextInt(); 

    while (number <= 0) { 
     System.out.println("Please input interger"); 
     number = myScanner.nextInt(); 
    } 
    int[] arr = new int[number]; // Store values in array 
    int i=0; //i is to store current iteration 
    int sum=0; //sum is to store sum of the input 
    int x; //x is to store the user input 
    int max = 0; 
    int min = 0; 
     for(i=0;i<number;i++){ 
     System.out.println("Please input a positive interger "); 
     arr[i] = myScanner.nextInt(); 
     sum = sum + arr[i]; 

    } 
    int average = sum/number; 

    System.out.println("The average is " + average); 

    // Put initial values in min and max for comparing 
    min =arr[0]; 
    max = arr[0]; 
for(i=1;i<number;i++) 
{ 
    // Compare if values is less than arr[i] 
    if (arr[i] < min){ 
     min = arr[i]; 
    } 
// Compare if values is greater than arr[i] 
    if (arr[i] > max) { 
     max = arr[i]; 
    } 
} 
    System.out.print("The minimum value is " + min); 
    System.out.println(" and the maximum value is " + max); 

} 
} 

這裏是輸出:

enter image description here

0

在第一部分,我會建議你使用do-while循環,以測試號碼的數量。像,

int number; 
do { 
    System.out.println("Please input a positive integer that " 
      + "indicates number of positive integers "); 
    number = myScanner.nextInt(); 
} while (number <= 0); 

然後,你需要設置minmax你的循環(或存儲所有讀取的值)。我寧願Math.maxMath.min。我也會從0算起。像,

int i = 0; // i is to store current iteration 
int sum = 0; // sum is to store sum of the input 
int x; // x is to store the user input 
int max = Integer.MIN_VALUE; // store the max user input 
int min = Integer.MAX_VALUE; // store the min user input 
while (i < number) { 
    System.out.println("Please input a positive integer "); 
    x = myScanner.nextInt(); 
    if (x > 0) { // make sure it's a positive integer 
     min = Math.min(min, x); 
     max = Math.max(max, x); 
     sum += x; 
     i++; 
    } 
} 
int average = sum/number; 
System.out.println("The average is " + average); 
System.out.println("The minimum value is " + min); 
System.out.println("and the maximum value is " + max); 
相關問題