簡體   English   中英

在Java中使用循環內部的變量和if語句

[英]Using variables inside loops and if-statements in Java

我必須完成一個問題。

問題是:

我已經使用列表完成了這個問題,但我的老師說她希望我使用nested loop loopif語句。

到目前為止,我接受了幾天的輸入並運行一個while loop來詢問用戶每天的日照時數。 問題是比較每個循環迭代的輸入(找到一天的最高和最低總日照量)。

碼:

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

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

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

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小時范圍內。

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

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

請注意。 Java命名約定使您可以使用大寫字符開始類名。 如果不這樣做,則不會編譯或運行您的代碼。 這只是一個很好的編程習慣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM