简体   繁体   English

布尔表达式— Java

[英]Boolean expression — Java

I am writing a Java code with displays a histogram of rainfall (in millimeter) on days 0 to 6, ie. 我正在编写Java代码,其中显示了第0到6天的降雨量直方图(以毫米为单位)。 day0 is Monday and day6 is Sunday. day0是星期一,day6是星期日。 The code is I have written for the average rainfall over 7 days is the following: 我为以下7天的平均降雨量编写的代码如下:

                 public float averageRain() {
                   int d;                                  
                   float total = 0.0f;                         
                   for (d = 0; d < days; d++)
                     total=total + rain[d];                 
                   return(total / days); 
                 }

But I am stymied on how to solve the following: If you enter a day number which is less than 0 or greater than 6, you get an error message. 但是,我受困于如何解决以下问题:如果您输入的天数小于0或大于6,则会收到错误消息。 My task is to modify a method called "addRain" to check whether the day is in range (ie 0 or greater, but less than constant days). 我的任务是修改一个名为“ addRain”的方法,以检查日期是否在范围内(即0或更大,但小于固定天数)。 If the day is valid, add RAINFALL to the total for the day and return true. 如果当天有效,则将RAINFALL添加到当天的总计中并返回true。 Otherwise return false. 否则返回false。 By doing so, when I run the program, the invalid days are ignored. 这样,当我运行该程序时,无效天将被忽略。 Also, I have to modify the code so that NEGATIVE rainfall values (eg. -3 mm) are NOT accepted. 另外,我必须修改代码,以使负降雨值(例如-3 mm)不被接受。 Could anyone tell me how to solve this matter? 谁能告诉我如何解决这个问题? All I have done so far is this: 到目前为止,我所做的就是:

                public boolean addRain(int day, float rainfall) {
                  rain[day] += rainfall;                      
                  return(true);  
                }

I assume days is a constant with value 7. Replace it with: 我假设days是一个常量,值为7。将其替换为:

private static final int DAYS_IN_WEEK = 7;

Do you want to simply ignore invalid day numbers or rain values? 您是否只想忽略无效的日数或降雨值? Is this an interactive program? 这是一个互动程序吗? And, do you want to let people enter rain values twice for a single day? 而且,您是否要让人们一天输入两次降雨值? I can see someone entering 10mm for Wednesday and then trying to correct it and enter 5mm . 我可以看到有人在星期三输入10mm ,然后尝试更正并输入5mm Currently, there is no way to do so. 当前,尚无办法。 If you know how to use exceptions, you can have your method throw java.lang.IllegalArgumentException and report it to the user so he can try again: 如果您知道如何使用异常,则可以让您的方法抛出java.lang.IllegalArgumentException并将其报告给用户,以便他可以重试:

public void setRainForDay(int day, float rainfall) {
    if (day < 0 || day >= DAYS_IN_WEEK) {
        // Throw an IAE.
    }
    if (rainfall < 0.0f) {
        // Throw an IAE.
    }
    rain[day] = rainfall;  // Set, not add.
}

The calling routine catches the IAE, reports it to the user, and the user can try again. 调用例程捕获IAE,并将其报告给用户,然后用户可以重试。

Swing 摇摆

I had not realized that the application was supposed to be a Swing application. 我还没有意识到该应用程序应该是Swing应用程序。 In that case, throwing exceptions is not the appropriate method. 在这种情况下,抛出异常不是适当的方法。 Instead, one wants to have a model class , one that holds the data, and one wants to have the view classes , and one wants to have controller classes that do the actual work. 取而代之的是,一个想要拥有一个模型类 ,一个用于保存数据,一个想要拥有视图类 ,一个想要拥有执行实际工作的控制器类 And, one wants to use PropertyChangeListener s and VetoableChangeListener s. 而且,人们想使用PropertyChangeListenerVetoableChangeListener When you type -10 into the rainfall JFormattedTextField , it sends a property change event to the controller, whig must then refuse to set the model class, instead vetoing the change. 在降雨JFormattedTextField输入-10 ,它将向控制器发送属性更改事件,然后辉格必须拒绝设置模型类,而否决了更改。 Actually, it will be simpler to write and apply an InputValidator . 实际上,编写和应用InputValidator会更简单。 It's best not to give the user the opportunity to err. 最好不要给用户错误的机会。

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

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