【发布时间】:2016-01-22 02:05:56
【问题描述】:
我有一个班级项目,我们有两个数组(高温和低温),我们将存储一个月内每天的高温和低温。我们需要一个不同寻常的数字,因此如果将信息输入数组的人忘记了一天的温度,那将是默认数字(我选择了 510)。所以我目前拥有的是一个打印出当天的程序,但所有温度都是 510 度。有人可以向我解释一下我需要对 while 循环做什么才能让输入的信息进入正确的高低数组吗?还有没有办法不输入任何内容(如果记录温度的人忘记测量高温)并保持在 510 度?
import java.util.Scanner;
导入 javax.swing.JOptionPane;
公共类天气{
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] high = new int[30];
int [] low = new int[30];
//switch to 32
Init (high);
Init(low);
Report(low,high);
LoadData(low,high);
Report(low, high);
}
public static void Init(int A[])
{
for(int i = 0; i < A.length; i++)
{
A[i] = 510;
}
}
public static void Report(int[] H, int[] L)
{
System.out.println("Day High Low");
for(int i = 0; i < H.length; i++)
{
System.out.println(i + " " + H[i] + " " + L[i]);
}
}
public static void LoadData(int[] H, int[] L)
{
int day = 0;
while(day <= 30)
{
int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));
int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));
H[day] = high;
H[day] = low;
day++;
}
}
}
【问题讨论】:
-
它并没有在我身上跳出来,但我可以建议在 Integer.parseInt() 行之后添加一些调试行吗?尝试类似:
int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));System.out.println("Day: " + day + " high: " +high + " low: " + low);看看打印出来的内容
标签: java arrays methods while-loop user-input