【问题标题】:Writing basic Java, Arrays and For Loop编写基本的 Java、数组和 For 循环
【发布时间】:2013-12-13 11:17:12
【问题描述】:

嘿,我想知道是否有人可以帮助我解决这个问题?我基本上是在尝试编写一个程序来读取一周的温度和降雨数据,将数据存储在一个数组中,然后以几种不同的形式输出数据。我已经完成了(或者我是这么认为的),但由于某些奇怪的原因它只是无法正常工作。程序运行良好,获取信息,但所有数据字段输出为“0”。可以用一双新鲜的眼睛在很大程度上:)任何帮助很高兴

import java.util.Scanner;
public class rainFallProject {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

            //Creates scanner object
            Scanner myScanner=new Scanner(System.in);

            //declare array for temperature
            int [] tempList = new int[7];

            //declare array for rainfall
            int [] rainList = new int[7];

            //NOTE all arrays need only be 7 places long, as there will only ever be 7 entries

            //declare a variable for the just entered temp
            int tempInput;

            //declare a variable for the just entered rainfall
            int rainInput;

            //declare variable for highest temp
            int highestTemp=0;

            //declare variable for lowest rainfall amount
            int lowestRain=0;



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

                //Prompt the user to enter a temperature
                System.out.format("Please enter the temperature for day %d:", i+1);
                //stores the input as just entered temp
                tempInput = myScanner.nextInt();
                //Assigns the temp to its correct place in the array
                tempInput = tempList[i];
                    //updates the highest temp, if necessary
                    if(tempInput>highestTemp){
                        tempInput=highestTemp;
                    }


                //Prompt the user to enter the rainfall amount
                System.out.format("Please enter the rainfall for day %d:", i+1);
                //stores the input as just entered rainfall
                rainInput = myScanner.nextInt();
                //Assigns the rain to its correct place in the array
                rainInput = rainList[i];
                    //updates the lowest rain, if necessary
                    if(rainInput<lowestRain){
                        rainInput=lowestRain;
                    }

            }
            //Declare the total temperature
            int tempSum = (tempList[0]+tempList[1]+tempList[2]+tempList[3]+tempList[4]+tempList[5]+tempList[6]);

            //Declares the total rainfall
            int rainSum = (rainList[0]+rainList[1]+rainList[2]+rainList[3]+rainList[4]+rainList[5]+rainList[6]);
            //Output the results:

            //-Weekly average temperature ((sum of all temps)/7)
            System.out.format("The weekly average temperature was %d degrees celsius. %n", (tempSum/7));
            //-Highest temperature of the week
            System.out.format("The highest temperature for the week was %d degrees celsius.%n", highestTemp);
            //-Total weekly rainfall amount (sum of all rainfalls)
            System.out.format("The total weekly rainfall amount for the week was %d mm. %n", rainSum);
            //-Lowest daily rainfall amount
            System.out.format("The lowest amount of rainfall for the week was %d mm. %n", lowestRain);



    }

}

【问题讨论】:

  • 你真的应该尝试在调试器中运行你的代码并逐步完成它正在做的事情。这样做问题会变得相当明显......
  • 一个 IDE 可能会抛出一些关于变量永远不会被读取的警告
  • 创建一个包来放入你的代码。无论多么简单,都不应该将默认包用于任何代码。

标签: java arrays eclipse


【解决方案1】:

在这里你将tempInput 设置为highestTemphighestTemp 始终为零

  //updates the highest temp, if necessary
  if(tempInput>highestTemp){
        tempInput=highestTemp;
  }

您似乎在整个程序中都这样做,左边的东西等于右边的东西,而不是相反。记住它总是:

variableToBeUpdated=newVariableValue

代码行是命令而不是方程式

这种混淆可能来自于 java 的行看起来像方程式,但事实并非如此。下面是一行完全有效的java代码,但是一个无效的等式

a=a+1;

现在作为一个方程有明显的问题,但作为一个命令它是有意义的

"Make (thing on left) equal to (thing on right) [by changing (thing on left)]"  
"Make (a) equal to (a+1) [by changing (a)]"

所以a 比以前大了一个

【讨论】:

  • 是的.. 他必须做最高温度 = tempInput。他正在做相反的事情...... +1 提到这一点......
【解决方案2】:
             tempInput = tempList[i];

应该是

            tempList[i] = tempInput;

你在其他几个地方也有同样的错误,你的任务是错误的。

【讨论】:

    【解决方案3】:
    //stores the input as just entered temp
    tempInput = myScanner.nextInt();
    //Assigns the temp to its correct place in the array
    tempInput = tempList[i];
    

    是你的问题! 神话终结者之声>

    【讨论】:

      【解决方案4】:
              rainInput = myScanner.nextInt();
              //Assigns the rain to its correct place in the array
              rainInput = rainList[i];
      

      在这里,您正在用 0 覆盖您从用户那里读取的值。(tempList[i] 为 0)。这是你的问题...

      【讨论】:

        猜你喜欢
        • 2019-01-31
        • 1970-01-01
        • 1970-01-01
        • 2020-05-12
        • 1970-01-01
        • 1970-01-01
        • 2015-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多