简体   繁体   English

如何使用while循环将用户输入输入数组,同时保持某些数字不变

[英]How to use a while loop to enter user input into an array while keeping certain numbers unchanged

I have a project for class where we have two arrays (high and low) that we are going to store high and low temperatures for each day during a month. 我有一个课堂项目,其中有两个阵列(高和低),我们将在一个月中每天存储高温和低温。 We need to have a number that is out of the ordinary so if the person entering information into the arrays forgot to take a temp for a day it would be that default number (510 is what I chose). 我们需要一个与众不同的数字,因此,如果将信息输入阵列的人忘记了一天的工作时间,那将是该默认数字(我选择的是510)。 So what I currently have is a program that prints out the day but all the temperatures are 510. Can someone explain to me what I need to do to the while loop to get the info entered to go into the correct high and low arrays? 因此,我目前拥有的程序可以打印出当天的信息,但所有温度都为510。有人可以向我解释我需要对while循环进行什么操作,以便输入正确的高低数组信息。 Also is there a way to enter nothing (if the person recording temperatures forgot to take a temperature for the high) and have it remain 510 degrees? 还有没有办法什么都不输入(如果记录温度的人忘记将温度升高了,并且保持在510度)?

    import java.util.Scanner;

import javax.swing.JOptionPane; 导入javax.swing.JOptionPane;

public class Weather { 公共课天气{

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++;
        }
}

} }

I tried your code and it almost works fine. 我尝试了您的代码,它几乎可以正常工作。

You have two mistakes here: 您在这里有两个错误:

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;
    L[day] = low;
    day++;
}

Your int[] array was initialized as int[30] so it only holds 30 elements, since the condition of your loop is day <= 30, it will actually try to input a 31th element, which will raise and exception. 您的int []数组已初始化为int[30]因此它仅包含30个元素,因为循环的条件是day <= 30,它实际上将尝试输入第31个元素,该元素将引发并且异常。 If I change the condition to day < 30 , your program reports the temperatures properly. 如果我将条件更改为day < 30 ,则您的程序会正确报告温度。

Your other mistake is that you are assigning both low and high to the same array, just change H[day] = low; 另一个错误是您将低和高同时分配给同一数组,只需更改H[day] = low; to L[day] = low; L[day] = low;

For the 'blank input' requirement, I recommend changing the loop to a for-loop: 对于“空白输入”要求,我建议将循环更改为for循环:

for(int day = 0; day < H.length; day++) {
  String low, high;

  high = JOptionPane.showInputDialog("please enter the high");
  low = JOptionPane.showInputDialog(" Please enter the low");

  if(high.equals("") || low.equals(""))
    continue;

  H[day] = Integer.parseInt(high);
  L[day] = Integer.parseInt(low);

}

What this code does is, if the user inputs a blank string, it skips the rest of the loop and goes to the next cycle, effectively leaving the default value untouched. 该代码的作用是,如果用户输入一个空白字符串,它将跳过循环的其余部分并转到下一个周期,从而有效地保持默认值不变。

Using try and catch, here's an example of what you can do: 使用try and catch,这是您可以做什么的示例:

while(day < 30)
{
   int highTemp = 0;
   int lowTemp = 0;
   String high = JOptionPane.showInputDialog("please enter the high");
   String low = JOptionPane.showInputDialog(" Please enter the low");
   try {
       highTemp = Integer.parseInt(high);
   } catch(NumberFormatException e){
       highTemp = 510;
   }
   try {
       lowTemp = Integer.parseInt(low);
   } catch(NumberFormatException e){
       lowTemp = 510;
   }
   H[day] = highTemp;
   L[day] = lowTemp;
   day++;
}

Basically what happens here is the input is first taken in as a string, then the program attempts to parse it. 基本上,这里发生的是首先将输入作为字符串输入,然后程序尝试解析它。 If there is a failure (eg empty input), then it will make the value 510 by default. 如果出现故障(例如,空输入),则默认情况下将使值510。

Please do note the errors in your code for the while condition: 请注意while条件中代码中的错误:

Before: 之前:

while(day <= 30)

After

while(day < 30)

If you make it '<=", there will be an array out of bounds exception, as your temperature arrays only have a length of 30 (meaning the last indexed value is at index 29) 如果将其设置为“ <=”,则会出现数组超出范围的异常,因为温度数组的长度仅为30(这意味着最后一个索引值位于索引29)

Also, you didn't change the array the temperature values are assigned to here: 另外,您也没有更改温度值分配到的数组:

Before: 之前:

H[day] = highTemp;
H[day] = lowTemp;

After: 后:

H[day] = highTemp;
L[day] = lowTemp;

You are just over-riding your array value assignment if you assign the low and high temp both to the high temp array. 如果将低温和高温都分配给高温数组,那么您将超越数组的值分配。

Also, in your 'public static void main(String[]args){}' 另外,在您的'public static void main(String [] args){}'中

Before: 之前:

Report(low, high);
LoadData(low, high);
Report(low, high);

After: 后:

Report(high, low);
LoadData(high, low);
Report(high, low);

Your methods call for the high temperature array first, not the low one. 您的方法首先需要高温阵列,而不是低温阵列。 In your code you are assigning the low temperature to the high temperature. 在您的代码中,您将低温分配给高温。

you wrote 你写了

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++;
    }

look you have no try/catches to handle exceptions, and you wrote H[day] = high; 看起来您没有尝试/捕捉来处理异常,并且您写H[day] = high; and H[day] = low; H[day] = low; so low array will remain 510 for all elements , all time. 因此低位数组将始终保持所有元素的510。
you can fix it simply using try catches . 您可以简单地使用try catch来解决它。

note: that when length of your array is 30 , you can access to it's elements with indexes between 0 to 29 so the condition of your while loop while(day <= 30) is incorrect. 注意:当数组的长度为30 ,可以访问索引在0 to 29之间的元素,因此while循环while(day <= 30)条件不正确。

use this code: 使用此代码:

    int day = 0;
    while (day < 30) {

        try {
            int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));
            H[day] = high;
        } catch (HeadlessException | NumberFormatException | NullPointerException e) {
        }

        try {

            int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));
            L[day] = low;

        } catch (HeadlessException | NumberFormatException | NullPointerException e) {
        }

        day++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM