在Java中使用循环内部的变量和if语句

时间:2015-10-18 08:01:31

标签: java variables

我必须完成一个问题。

问题是:编写一个程序,该程序将每天的日照时数(每天日照的输入)按天数给定(天数输入)。输出具有最高日照时数的日和具有最低日照时数的日。

我已使用列表完成了此问题,但我的老师说她希望我使用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);
    }
}

我的问题是:如何比较迭代次数,以找出日照量最高和最低的日照量? 换句话说,如何比较循环中不同迭代的输入数据?

4 个答案:

答案 0 :(得分: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小时范围内。

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

答案 2 :(得分:0)

你的问题将依赖于没有足够的范围。在循环内部创建的任何变量只有在循环的迭代中才有效,当您传递到下一次迭代时,您刚刚分配给新创建的整数值的所有信息都会被复制整数值。此外,在使用Scanner模块进行输入的应用程序中,我通常倾向于使用System.out.print(&#34;输入到此处:&#34;),以便用户(或教师)可以输入同一行而不是下一行。

答案 3 :(得分:0)

请注意。 Java命名约定使您可以使用大写字符开始类名。如果不这样做,则不会编译或运行您的代码。这只是一个很好的编程实践。