Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—当程序不应';T_Java - Fatal编程技术网

Java—当程序不应';T

Java—当程序不应';T,java,Java,我必须制作一个程序,使用嵌套循环来收集数据并计算多年的平均降雨量。首先,该计划应要求提供年数。(外部循环将每年迭代一次。)内部循环将迭代12次,每月迭代一次。内部循环的每次迭代都会询问用户该月的降雨量。在所有迭代之后,程序应显示整个期间的月数、总降雨量和每月平均降雨量 输入验证:不接受年数小于1的数字。月降雨量不接受负数 我已经完成了这个程序,但有一个小问题:当它要求输入英寸时,如果我在今年输入1,它将要求我输入12次英寸。如果我先输入一个负数,它会告诉我这个数字无效,然后再次循环,让我输入一个

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

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

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

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);
   }
}
import java.util.Scanner;
公共类平均值FallTeller{
/**
*该程序告诉用户输入年数,然后询问
*用户需要给出在测试期间每个月下降的英寸数
*然后给出这些年的平均降雨量,总降雨量和
*总月份数。
*/
公共静态void main(字符串[]args){
双总英寸=0;
双倍总月数=0;
双avgInches=0;
双英寸=0;
扫描仪kb=新扫描仪(System.in);
System.out.println(“请输入年数”);
int numYears=kb.nextInt();
而(!(numYears>=1)){
System.out.println(“请输入大于或等于1的数字”);
numYears=kb.nextInt();
}

对于(int years=1;years不要做
inches+=kb.nextDouble();
而是做
input=kb.nextDouble();如果input>0,那么inches+=input;否则循环
。这应该有助于在算术逻辑之前过滤掉负数。

问题在于:

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
,因此它将始终为零。

确保
inches
有一个新值(非负)输入,然后将其添加到
totalInches


用于(int months=1;months哇。所有这些空白真的让人很难阅读。有没有可能重新格式化,使其更整洁,更易于阅读,而无需滚动?删除那些try/catch,什么都不做。只需声明
会对你的方法引发异常
。我自己做了一些整理。我是新来的,嗯,我该如何删除空白?啊hh所以不断的添加是问题所在。:D谢谢你…我怎么没有看到lol…。我猜在编码后你希望它能工作,挫折让你错过了东西。我对java和编程是新手,所以谢谢你的帮助。