编写基本的Java,数组和For循环

时间:2013-12-13 11:17:12

标签: java arrays eclipse

HeyI想知道是否有人可以帮我这个?我基本上试图编写一个程序,读取一周的温度和降雨数据,将数据存储在一个数组中,然后以几种不同的形式输出数据。我完成了(或者我认为),但由于一些奇怪的原因,它只是不起作用。程序运行正常,获取信息,但所有数据字段输出为“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);



    }

}

4 个答案:

答案 0 :(得分:3)

在此设置tempInputhighestTemphighestTemp始终为零

  //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变得比以前更大

答案 1 :(得分:2)

             tempInput = tempList[i];

应该是

            tempList[i] = tempInput;

你在其他几个地方也犯了同样的错误,你的作业错误。

答案 2 :(得分:1)

//stores the input as just entered temp
tempInput = myScanner.nextInt();
//Assigns the temp to its correct place in the array
tempInput = tempList[i];

&lt; mythbusters voice&gt;那是你的问题! &lt; / mythbusters voice&gt;

答案 3 :(得分:1)

        rainInput = myScanner.nextInt();
        //Assigns the rain to its correct place in the array
        rainInput = rainList[i];

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