2012-12-15 141 views
0

我是新來的Java,我試圖讓一個程序,允許用戶輸入100個數字,如果用戶寫'0',那麼該程序是假設打印最小,最大,總和和所有數字。我得到了所有的工作,但不是退出並打印所有。我的老師說了一些關於使用while循環的內容,但是當你有一個for循環時怎麼可能呢?Combine for和while循環(Java)

問候

public static void main(String[] args) { 
    int[] list = new int[100]; 
    int min = 0; 
    int max = 0; 
    int sum = 0; 
    boolean first = true; 

    Scanner scan = new Scanner(System.in); 
    while(list[i] != 0) { 
     for (int i = 0; i < list.length; i++) { 

      System.out.print("Enter number (0 to exit) " + (1 + i) + ":"); 
      list[i] = scan.nextInt(); 
     } 

     for (int i = 0; i < list.length; i++) { 

      if (first == true) { 

      min = list[i]; 
      first = false; 
     } 

     if (list[i] < min) { 

      min = list[i]; 
     } 

     else if (list[i] > max) { 

      max = list[i]; 
     } 

     sum = list[i] + sum; 

    } 

    if (list[i] == 0) { 

    System.out.print("Numbers are: " + list[0] + ", "); 

    for (int i = 1; i < list.length; i++) 

    System.out.print(list[i] + ", "); 
    System.out.println(); 

    System.out.println("Smallest number is: " + min); 
    System.out.println("Largest numeber is: " + min); 
    System.out.println("Sum is: " + sum); 
    } 
    } 
} 

} 
+0

好那麼也許'for'循環是不恰當的?你可以使用'for'循環並在裏面有一個'if',或者你有一個'while'循環並且有一個變量作爲計數器。 – ApplePie

+1

「for循環」是「while」循環的專門化(或者,就此而言,「do/while」循環)。你可以在while循環中做所有事情,你可以在for循環中進行。你也可以嵌套循環,一個在另一個之內。 – paulsm4

+0

從while下降,從布爾遞歸下降。 – Airhead

回答

0

下面是方法的主體,它會做你所問過的。我沒有使用while循環(但實際上,for循環是內部的一種while循環)。

int size = 100; // Set the number of numbers to input. 
int[] list = new int[size]; // Create an array with 'size' elements. 
int min = Integer.MAX_VALUE; // Set the highest possible integer as start value. 
int max = 0; // Set the minimum to zero, assuming that the user won't input negative numbers. 
int sum = 0; // Initialize the sum of the numbers in the list. 

Scanner scan = new Scanner(System.in); 

for (int i = 0; i < size; i++) { // Run 'size' times the process of inputting a number. 
    System.out.print("Enter number (0 to exit) " + (i + 1) + ": "); 
    int number = scan.nextInt(); 
    if (number == 0) { // Quit program if input equals '0' 
     System.out.println("Exiting..."); 
     break; 
    } 
    list[i] = number; // Add the current number to the list 
    sum += number; // Add the number to the total 
    if (number < min) { // If the number is smaller than the previous one, set this number as the smallest 
     min = number; 
    } 
    if (number > max) { // If the number is greater than the previous smallest number, set this number as the greatest 
     max = number; 
    } 
} 

// Output all numbers in the list 
for (int i = 0; i < list.length; i++) { 
    if (list[i] != 0) { 
     System.out.print((i == 0 ? "" : ", ") + list[i]); 
    } 
} 

// You see the snippet (i == 0 ? "" : ", ") 
// That is a shorthand if-else statement: 
// If i equals 0, then "" (empty string), else ", " (comma and space). 
// The statement 
//  System.out.print((i == 0 ? "" : ", ") + list[i]) 
// is the same as 
//  if (i == 0) { 
//   System.out.println("") + list[i]; 
//  } 
//  else { 
//   System.out.println(", ") + list[i]; 
//  } 

System.out.println("Smallest number is: " + min); 
System.out.println("Largest numeber is: " + max); 
System.out.println("Sum is: " + sum); 
+0

哦,謝謝。對不起,我制定的任務如此糟糕,但它不只是假設退出,它必須打印數字,最小的最大等等也在 – user1905426

+0

已更新。只需使用'break'而不是'System.exit(0)'。中斷將退出當前循環並繼續執行代碼。 –

+0

行動,沒有正確閱讀它。非常感謝你!你現在不知道如何在退出時擺脫零點? – user1905426

0

你糊塗代碼。更好地使用這樣的模式:

while (true) { 
    // read next 
    if (input == 0) 
     break; 
} 
+0

他還需要記錄到目前爲止的條目數量,並在100處停止。 –

+0

但是,我是怎麼假設用while循環打印出所有數字的呢? – user1905426

2

你只需要一個while循環來做到這一點,另外一個for循環只是,如果你想打印數組:

Scanner scan = new Scanner(System.in); 
int i = 0; 
int sum = 0; 
int maxValue = Integer.MIN_VALUE; 
int[] history = new int[100]; 
System.out.println("INPUT:"); 
int option = scan.nextInt(); 
while (option != 0 && i <= 100) 
{ 
    if (option > maxValue) 
     maxValue=option; 
    sum += option; 
    history[i] = option; 
    option = scan.nextInt(); 
    i++; 
} 
System.out.println("OUTPUT: \n" + "SUM: " + sum + "\n MAX VALUE: " + maxValue); 
for (int x : history) 
    System.out.print(x + ""); 
+0

謝謝!但是,我如何擺脫零?任務是讓它打印「數字是:1,3,7」 – user1905426

+0

得到它,謝謝 – user1905426

+0

@ user1905426如果你想擺脫所有的零,即增量式添加到數組,你需要使用動態結構像一個arrayList。 – Ivo