【发布时间】: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 可能会抛出一些关于变量永远不会被读取的警告
-
创建一个包来放入你的代码。无论多么简单,都不应该将默认包用于任何代码。