执行程序时,额外输入命令导致逻辑错误java
when executing program, extra input command causes logical errors java
我正在尝试执行一个名为 AverageRainfall 的程序。大多数输入工作正常(我一开始的 while 语句很好),但是变量 monthRain 下有多个月份,而 monthRain 的 while 语句对各个月份不起作用,只有初始输入命令,它正在服务没有目的。
预计到达时间:发布完整代码进行测试
import java.util.Scanner; //for Scanner class
public class AverageRainfall
{
public static void main(String[] args)
{
final int NUM_MONTHS = 12; //Months per year
int years; //Number of years
double monthRain; //Rain for a month
double totalRain = 0; //Rainfall accumulator
double average; //Average rainfall
Scanner keyboard = new Scanner(System.in);
{
System.out.print("Enter the number of years: ");
years = keyboard.nextInt();
while (years < 1)
{
System.out.print("Invalid. Enter 1 or greater: ");
years = keyboard.nextInt();
}
}
{
System.out.println("Enter the rainfall, in inches, for each month. ");
monthRain = keyboard.nextDouble();
for(int y = 1; y <= years; y++){
for(int m = 1; m <= NUM_MONTHS; m++){
System.out.print("Year" + y + "month" + m + ": ");
monthRain = keyboard.nextDouble();
}
}
while (monthRain < 0)
{
System.out.print("Invalid. Enter 0 or greater: ");
monthRain = keyboard.nextDouble();
}
}
{
totalRain += monthRain;
average = totalRain / (years * NUM_MONTHS);
System.out.println("\nNumber of months: " + (years * NUM_MONTHS) );
System.out.println("Total rainfall: " + totalRain + " inches");
System.out.println("Average monthly rainfall: " + average + " inches");
}
}
}
这是完整的代码。
您可以做的是在用户每次输入一个月的雨时添加到总雨量中。然后他输入完数据你就可以做平均了
`导入java.util.Scanner;
public class 测试 {
public static void main(String[]args){
double monthRain=0;
double totalRain=0;
Scanner keyboard = new Scanner(System.in);
int years = 1;
int NUM_MONTHS = 12;
System.out.println("Enter the rainfall, in inches, for each month. ");
for(int y = 1; y <= years; y++){
for(int m = 1; m <= NUM_MONTHS; m++){
System.out.print("Year" + y + "month" + m + ": ");
monthRain = keyboard.nextDouble();
totalRain+=monthRain;
}
}
int totalMonth = years*NUM_MONTHS;
System.out.println("\nNumber of months: " + totalMonth );
System.out.println("Total Rain: "+totalRain+" inches");
double average = totalRain / totalMonth;
System.out.println("Average monthly rainfall: " + average + " inches");
}
}
`
您使用了不必要的牙套。此外,您的代码中还存在一些逻辑缺陷。我已经修复了你的代码。请参考以下代码:
import java.util.Scanner; //for Scanner class
public class AverageRainfall {
public static void main(String[] args) {
final int NUM_MONTHS = 12; // Months per year
int years; // Number of years
double monthRain=0; // Rain for a month
double totalRain = 0; // Rainfall accumulator
double average; // Average rainfall
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of years: ");
years = keyboard.nextInt();
while (years < 1) {
System.out.print("Invalid. Enter 1 or greater: ");
years = keyboard.nextInt();
}
System.out.println("Enter the rainfall, in inches, for each month. ");
for (int y = 1; y <= years; y++) {
for (int m = 1; m <= NUM_MONTHS; m++) {
System.out.print("Year" + y + "month" + m + ": ");
monthRain = keyboard.nextDouble();
while (monthRain < 0) {
System.out.print("Invalid. Enter 0 or greater: ");
monthRain = keyboard.nextDouble();
}
totalRain += monthRain;
}
}
average = totalRain / (years * NUM_MONTHS);
System.out.println("\nNumber of months: " + (years * NUM_MONTHS));
System.out.println("Total rainfall: " + totalRain + " inches");
System.out.println("Average monthly rainfall: " + average
+ " inches");
}
}
我正在尝试执行一个名为 AverageRainfall 的程序。大多数输入工作正常(我一开始的 while 语句很好),但是变量 monthRain 下有多个月份,而 monthRain 的 while 语句对各个月份不起作用,只有初始输入命令,它正在服务没有目的。
预计到达时间:发布完整代码进行测试
import java.util.Scanner; //for Scanner class
public class AverageRainfall
{
public static void main(String[] args)
{
final int NUM_MONTHS = 12; //Months per year
int years; //Number of years
double monthRain; //Rain for a month
double totalRain = 0; //Rainfall accumulator
double average; //Average rainfall
Scanner keyboard = new Scanner(System.in);
{
System.out.print("Enter the number of years: ");
years = keyboard.nextInt();
while (years < 1)
{
System.out.print("Invalid. Enter 1 or greater: ");
years = keyboard.nextInt();
}
}
{
System.out.println("Enter the rainfall, in inches, for each month. ");
monthRain = keyboard.nextDouble();
for(int y = 1; y <= years; y++){
for(int m = 1; m <= NUM_MONTHS; m++){
System.out.print("Year" + y + "month" + m + ": ");
monthRain = keyboard.nextDouble();
}
}
while (monthRain < 0)
{
System.out.print("Invalid. Enter 0 or greater: ");
monthRain = keyboard.nextDouble();
}
}
{
totalRain += monthRain;
average = totalRain / (years * NUM_MONTHS);
System.out.println("\nNumber of months: " + (years * NUM_MONTHS) );
System.out.println("Total rainfall: " + totalRain + " inches");
System.out.println("Average monthly rainfall: " + average + " inches");
}
}
}
这是完整的代码。
您可以做的是在用户每次输入一个月的雨时添加到总雨量中。然后他输入完数据你就可以做平均了
`导入java.util.Scanner; public class 测试 {
public static void main(String[]args){
double monthRain=0;
double totalRain=0;
Scanner keyboard = new Scanner(System.in);
int years = 1;
int NUM_MONTHS = 12;
System.out.println("Enter the rainfall, in inches, for each month. ");
for(int y = 1; y <= years; y++){
for(int m = 1; m <= NUM_MONTHS; m++){
System.out.print("Year" + y + "month" + m + ": ");
monthRain = keyboard.nextDouble();
totalRain+=monthRain;
}
}
int totalMonth = years*NUM_MONTHS;
System.out.println("\nNumber of months: " + totalMonth );
System.out.println("Total Rain: "+totalRain+" inches");
double average = totalRain / totalMonth;
System.out.println("Average monthly rainfall: " + average + " inches");
}
} `
您使用了不必要的牙套。此外,您的代码中还存在一些逻辑缺陷。我已经修复了你的代码。请参考以下代码:
import java.util.Scanner; //for Scanner class
public class AverageRainfall {
public static void main(String[] args) {
final int NUM_MONTHS = 12; // Months per year
int years; // Number of years
double monthRain=0; // Rain for a month
double totalRain = 0; // Rainfall accumulator
double average; // Average rainfall
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of years: ");
years = keyboard.nextInt();
while (years < 1) {
System.out.print("Invalid. Enter 1 or greater: ");
years = keyboard.nextInt();
}
System.out.println("Enter the rainfall, in inches, for each month. ");
for (int y = 1; y <= years; y++) {
for (int m = 1; m <= NUM_MONTHS; m++) {
System.out.print("Year" + y + "month" + m + ": ");
monthRain = keyboard.nextDouble();
while (monthRain < 0) {
System.out.print("Invalid. Enter 0 or greater: ");
monthRain = keyboard.nextDouble();
}
totalRain += monthRain;
}
}
average = totalRain / (years * NUM_MONTHS);
System.out.println("\nNumber of months: " + (years * NUM_MONTHS));
System.out.println("Total rainfall: " + totalRain + " inches");
System.out.println("Average monthly rainfall: " + average
+ " inches");
}
}