Java - 程序在不应该接受负数时接受负数

时间:2011-09-28 21:15:50

标签: java

我必须制作一个程序,使用嵌套循环来收集数据并计算一段时间内的平均降雨量。首先,该计划应询问年数。 (外部循环将每年迭代一次。)内部循环将迭代12次,每个月一次。内循环的每次迭代都会询问用户该月的降雨量。在所有迭代之后,程序应显示整个时期的月数,总降雨量和每月平均降雨量。

输入验证:请勿接受年数小于1的数字。不接受每月降雨量的负数。

我已经完成了这个程序,但是有一个小问题:当它要求英寸时,如果我今年输入1,它会要求我输入英寸12次。如果我先输入一个负数,它会告诉我数字无效然后再循环并要求我输入一个等于或大于零的数字。现在,如果我输入一系列> = 0然后输入负数的数字,它不会告诉我我不能输入负数。它根本不应该接受负数,但是当前两个或更多个数>> = 0时它会这样做。

import java.util.Scanner;

public class Averagerainfallteller {
   /**
    * This program tells asks the user to enter an amount of years and then asks
    * the user to given the amount of inches that fell for every month during
    * those years and then give the average rain fall, total inches of rain and
    * total months.
    */
   public static void main(String[] args) {

      double totalInches = 0; 
      double totalMonths = 0; 
      double avgInches = 0; 
      double inches = 0; 
      Scanner kb = new Scanner(System.in);
      System.out.println(" PLease enter the number of years");
      int numYears = kb.nextInt();
      while (!(numYears >= 1)) {
         System.out.println(" PLease enter the number that is more than or equal to one.");
         numYears = kb.nextInt();
      }

      for (int years = 1; years <= numYears; years++) {

         for (int months = 1; months <= 12; months++) {
            System.out.println("How many inches fell for Year: " + years
                  + ", during Month: " + months + "? ");
            inches += kb.nextDouble();
            while (!(inches >= 0)) {
               System.out.println("PLease enter the number that is more than or equal to zero.");
               System.out.println("How many inches fell for Year: " + years
                     + ", during Month: " + months + "? ");
               inches += kb.nextDouble();
            }
         }
      }

      totalMonths = 12 * numYears;
      avgInches = totalInches / totalMonths;
      System.out.println(".....HERE ARE THE RESULTS.....");
      System.out.println("");
      try {
         Thread.currentThread().sleep(1000);
      } catch (Exception e) {
      }
      System.out.println("    Total inches is " + totalInches);
      System.out.println("");
      try {
         Thread.currentThread().sleep(1000);
      } catch (Exception e) {
      }
      System.out.println("    Average Inches is " + avgInches);
      System.out.println("");
      try {
         Thread.currentThread().sleep(1000);
      } catch (Exception e) {
      }
      System.out.println("    Total months is " + totalMonths);
   }
}

3 个答案:

答案 0 :(得分:2)

不要inches +=kb.nextDouble();执行input = kb.nextDouble(); if input > 0 then inches += input; otherwise loop。这可以帮助您在算术逻辑之前过滤掉负数。

答案 1 :(得分:0)

问题在于:

inches += kb.nextDouble();

而是试试这个:

System.out.println("How many inches fell for Year: "+years+", during Month: "+months+ "? ");
inches = kb.nextDouble();    //HERE

while(!(inches>=0))
{


    System.out.println("PLease enter the number that is more than or equal to zero.");
    System.out.println("How many inches fell for Year: "+years+", during Month: "+months+ "? ");
    inches =kb.nextDouble();    //HERE

}

totalInches += inches;    //HERE

基本上,您继续将用户输入的数字连续添加到同一变量inches。此外,您似乎使用totalInches而未初始化它,因此它始终为零。

答案 2 :(得分:0)

确保输入inches(非否定)的新值,然后将其添加到totalInches

for (int months = 1; months <= 12; months++) {

    do {
        System.out.println("How many inches fell for Year: " + years + ", during Month: " + months + "? ");
        inches = kb.nextDouble();
    }while(inches < 0);

    totalInches += inches;
}