2015-10-18 43 views
3

我必須完成一個問題。在循環內使用變量和Java中的if語句

現在的問題是:寫一個程序,每天的日照時間(每天的陽光輸入)爲給定的天數(輸入天數)。輸出日照時數最多的日子和日照時數最少的日子。

我已經完成了使用列表這個問題,但我的老師說,她要我用一個nested loop一個loopif聲明。

到目前爲止,我輸入天數並運行while loop向用戶詢問每天的日照時數。問題在於比較每個循環迭代的輸入(找出一天中最高和最低的陽光總量)。

CODE:

import java.util.Scanner; 
    public class temp { 

     public static void main(String[] args) { 

      Scanner new_scan = new Scanner(System.in); 
       System.out.println("Enter the amount of days you wish to enter the daylight hours for: "); 
        int loop_num = new_scan.nextInt(); 

      for(int x=0; x<=loop_num;x++){ 

       //Here I ask the user to input the number of sunlight hours for each day, I don't know how to compare separate input iterations (to find the highest and lowest days of sunlight). 

       System.out.println("Enter the number of daylight hrs for day "+ x); 
        int next_num = new_scan.nextInt(); 
        int c = 0; 
        int old_num = c + next_num; 

          if(next_num>old_num){ 
           int highest_num = next_num; 
          } else{ int highest_num = old_num; } 


          if(next_num<old_num){ 
           int lowest_num = next_num; 
          } else{ int lowest_num = old_num; } 

     } 
      System.out.println("The lowest amount of sunlight of the days given was: "+ lowest_num); 
      System.out.println("The highest amount of sunlight of the days given was: "+ highest_num); 
    } 
} 

我的問題是:我如何比較迭代找到與陽光的最高和最低量天的日照量? 換句話說,如何比較循環中不同迭代的輸入數據?

回答

3

該變量應該定義在循環之外以保持持久性,否則將在每次迭代時重新創建。 你需要類似的東西:

int min = Integer.MAX_VALUE; 
int max = -1; 
for(int x=0; x<=loop_num;x++){ 

    System.out.println("Enter the number of daylight hrs for day "+ x); 
    int next_num = new_scan.nextInt(); 

    if(next_num>max){ 
     max = next_num; 
    } 
    if (nex_num <min) { 
     min = nex_num; 
    } 

} 
System.out.println("The lowest amount of sunlight of the days given was: "+ min); 
System.out.println("The highest amount of sunlight of the days given was: "+ max); 

我建議在這裏添加輸入完整性檢查以來,數量應該介於0 - 24小時範圍。

+0

'int min = 0',應該用不同的值初始化(可能是'Integer.MAX_VALUE')。 – Zereges

+0

作爲最小值,你應該使用無窮大而不是0。但最好的辦法是使用第一次加值(與最小值相同)。 – dey

+0

謝謝。如果那裏幾乎沒有負數的小時數(或24小時內的數字)-1就足夠了。無論如何,這應該是輸入健全性檢查的一部分,而不是進一步計算。 –

1
Scanner new_scan = new Scanner(System.in); 
System.out.println("Enter the amount of days you wish to enter the daylight hours for: "); 
int loop_num = new_scan.nextInt(); 
int highest_num=Integer.MIN_VALUE; 
int lowest_num=Integer.MAX_VALUE; 
for(int x=0; x<=loop_num;x++){ 
    System.out.println("Enter the number of daylight hrs for day "+ x); 
    int next_num = new_scan.nextInt(); 
     if(next_num > highest_num){ 
     highest_num = next_num; 
     } 
     if(next_num < lowest_num){ 
     lowest_num = next_num; 
     } 
} 
System.out.println("The lowest amount of sunlight of the days given was: "+ lowest_num); 
System.out.println("The highest amount of sunlight of the days given was: "+ highest_num); 
0

你的問題將依靠沒有足夠的範圍。在循環內部創建的任何變量只在循環的迭代中才有效,當您傳遞到下一次迭代時,您剛分配給新創建的整數值的所有信息都將被複制一個新的整數值。此外,在使用掃描儀模塊進行輸入的應用程序中,我通常傾向於使用System.out.print(「Input goes here:」),以便用戶(或講師)可以在同一行輸入輸入,而不是下一個線。

0

請注意。 Java命名約定讓你用大寫字符開始類名。不是說你的代碼不會編譯或運行,如果你沒有。這只是一個很好的編程習慣。