我刚开始学习,我需要一些练习帮助。
我需要最终用户输入每个月的降雨量。 然后我需要把平均降雨量,最高月份和最低月份以及降雨量高于平均水平的月份排除在外。
我保持最高和最低的相同数字,我不知道为什么。我正在认真地拔掉头发。任何帮助将不胜感激。
这是我到目前为止所做的:
public class rainfall {
/**
* @param args
*/
public static void main(String[] args)
{
int[] numgroup;
numgroup = new int [13];
ConsoleReader console = new ConsoleReader();
int highest;
int lowest;
int index;
int tempVal;
int minMonth;
int minIndex;
int maxMonth;
int maxIndex;
System.out.println("Welcome to Rainfall");
for(index = 1; index < 13; index = index + 1)
{
System.out.println("Please enter the rainfall for month " + index);
tempVal = console.readInt();
while (tempVal>100 || tempVal<0)
{
System.out.println("The rating must be within 0...100. Try again");
tempVal = console.readInt();
}
numgroup[index] = tempVal;
}
lowest = numgroup[0];
for(minIndex = 0; minIndex < numgroup.length; minIndex = minIndex + 1);
{
if (numgroup[0] < lowest)
{
lowest = numgroup[0];
minMonth = minIndex;
}
}
highest = numgroup[1];
for(maxIndex = 0; maxIndex < numgroup.length; maxIndex = maxIndex + 1);
{
if (numgroup[1] > highest)
{
highest = numgroup[1];
maxMonth = maxIndex;
}
}
System.out.println("The average monthly rainfall was ");
System.out.println("The lowest monthly rainfall was month " + minIndex);
System.out.println("The highest monthly rainfall was month " + maxIndex);
System.out.println("Thank you for using Rainfall");
}
private static ConsoleReader ConsoleReader() {
return null;
}
}
谢谢,
艾米丽
答案 0 :(得分:2)
首先,由于这是你的作业,你不应该在stackoverflow.com上问它
现在让我们来看看您的代码
- 醇>
lowest = numgroup[0];
为什么呢?您似乎尝试使用此算法来查找min:
1.1假设第一个数字(您认为是
numgroup[0]
)是min(在您的代码中命名为lowest
)
1.2。将其与所有其他数字进行比较,如果任何数字较小,则替换为min(即lowest
)。
但是,numgroup[0]
不是你的第一个号码!你就像这样开始了你的第一个循环
for(index = 1;...
因此,您的第一个号码是numgroup[1]
。
接下来,你的第二个循环就像
一样
for(minIndex = 0;
而索引0处的元素甚至不打算供您使用(我猜)
接下来,找出当前迭代中的数字是否小于lowest
的条件是
if (numgroup[0] < lowest)
始终将索引0处的元素与lowest
进行比较(我猜)不是您的意图。
答案 1 :(得分:1)
而不是
if (numgroup[0] < lowest)
你必须写
if (numgroup[minIndex] < lowest)
同样适用于
if (numgroup[1] > highest)
应该是
if (numgroup[maxIndex] > highest)
答案 2 :(得分:1)
你做
lowest = numgroup[0]
然后
if (numgroup[0] < lowest)
永远不会是“真”,因为numgroup [0]总是等于最低。相反,你的if子句应该类似于if (numgroup[minIndex] < lowest)
。同样的事情适用于最高。
答案 3 :(得分:0)
public static void main(String[] args)
{
int[] numgroup = new int [12]; // 12 months - 12 elements
ConsoleReader console = new ConsoleReader();
int highest;
int lowest;
int index;
int tempVal;
int minIndex;
int maxIndex;
System.out.println("Welcome to Rainfall");
// Input (index now 0-based)
for(index = 0; index < 12; index = index + 1)
{
System.out.println("Please enter the rainfall for month " + index + 1);
tempVal = console.readInt();
while (tempVal>100 || tempVal<0)
{
System.out.println("The rating must be within 0...100. Try again");
tempVal = console.readInt();
}
numgroup[index] = tempVal;
}
lowest = numgroup[0];
highest = numgroup[0];
int total = 0.0;
// Loop over data (using 1 loop)
for(index = 0; index < 12; index = index + 1)
{
int curr = numgroup[index];
if (curr < lowest) {
lowest = curr;
minIndex = index;
}
if (curr > highest) {
highest = curr;
maxIndex = index;
}
total += curr;
}
float avg = (float)total / numgroup.length;
System.out.println("The average monthly rainfall was " + agv);
// +1 to go from 0-based index to 1-based month
System.out.println("The lowest monthly rainfall was month " + minIndex + 1);
System.out.println("The highest monthly rainfall was month " + maxIndex + 1);
System.out.println("Thank you for using Rainfall");
}
答案 4 :(得分:0)
如果您的作业没有限制,只需尝试使用最小/最大的集合。
Vector<Integer> rainfallData = new Vector<Integer>();
int avg = 0;
for(index = 1; index < 13; index = index + 1)
{
System.out.println("Please enter the rainfall for month " + index);
tempVal = console.readInt();
while (tempVal>100 || tempVal<0)
{
System.out.println("The rating must be within 0...100. Try again");
tempVal = console.readInt();
}
rainfallData.add(tempVal);
avg+=tempVal;
}
avg /= rainfallData.size();
int min = Collections.min(rainfallData);
int max = Collections.max(rainfallData);
否则min / max应如下所示:
public int min(int[] vals) {
if( vals==null || vals.length==0} {
throw new IllegalArgumentException();
} else if( vals.length == 1 ) {
return vals[0];
}
int min = vals[0]; // Dont initialize with Integer.MAX_VALUE or so
for(int i = 1; i < vals.length; ++i ) {
if( vals[i] < min ) {
min = vals[i];
}
}
return min;
}
答案 5 :(得分:0)
for (index = 0; index < 12; index++) {
}
更改第一个for循环和以下
lowest = numgroup[0];
for (minIndex = 0; minIndex < numgroup.length; minIndex = minIndex + 1)
{
if (numgroup[minIndex] < lowest) {
lowest = numgroup[minIndex];
}
}
highest = numgroup[0];
for (maxIndex = 0; maxIndex < numgroup.length; maxIndex = maxIndex + 1)
{
if (numgroup[maxIndex] > highest) {
highest = numgroup[maxIndex];
}
}