gpt4 book ai didi

java - 编译器错误 : expected. 如何解决此问题?

转载 作者:行者123 更新时间:2023-12-01 22:36:00 25 4
gpt4 key购买 nike

我在这项作业中遇到了很多错误。我已经尽可能多地完成了,并且似乎一切都井井有条,但 JGrasp 告诉我有 49 个错误。我在第 30,31, 33, 36, 37, 40, 41, 42, 43, 47, 48, 54, 55, 56, 和 57 行遇到错误。根据我的教科书,我已经做对了一切。我该如何解决这个问题?

我在给我带来麻烦的每一行后面添加了“此处”注释。

完整程序:

import java.util.Scanner;

public class AverageRainfall {
public static void main(String[] args) {
double monthRain = 0; // Rain for a month
double totalRain = 0; // Rainfall accumulator
double average = 0; // Average rainfall
// Keyboard input.
Scanner keyboard = new Scanner(System.in);
// Number of years.
System.out.print("Enter the number of years: ");
int years = 0; // Number of years
years = keyboard.nextInt();
// Checks input.
while (years < 1) {
System.out.print("Invalid. Enter 1 or greater: ");
input anotheryears = keyboard.nextInt();
}
}

final int NUM_MONTHS = 12; // Months per year

System.out.println("Enter the rainfall, in inches, for each month."); //here
for (int y = 1; y <= years; y++); //here
{
for (int m = 1; m <= NUM_MONTHS; m++); //here
{
// Gets rainfall for each month.
System.out.print("Year " + y + " month " + m + ": "); //here
monthRain = keyboard.nextDouble(); //here

// Checks input.
while (monthRain < 0) //here
{ //here
System.out.print("Invalid. Enter 0 or greater: "); //here
monthRain = keyboard.nextDouble(); //here
}

// Accumulates rainfall.
totalRain += monthRain; //here
} //here
}
// Calculates average rainfall.
average = totalRain / (years * NUM_MONTHS);
// Displays statistics.
System.out.println("\nNumber of months: " + (years * NUM_MONTHS)); //here
System.out.println("Total rainfall: " + totalRain + " inches"); //here
System.out.println("Average monthly rainfall: " + average + " inches"); //here
}

最佳答案

缩进代码。主要问题在这里:

public static void main(String[] args) {
double monthRain = 0;
//more code...
while (years < 1) {
System.out.print("Invalid. Enter 1 or greater: ");
input anotheryears = keyboard.nextInt();
}
} //<--- here
//you're closing the main method
//and then you have more code outside of it
final int NUM_MONTHS = 12; // Months per year

System.out.println("Enter the rainfall, in inches, for each month."); //here
for (int y = 1; y <= years; y++); //here
//rest of code

除此之外,删除 for 语句后面的分号:

for (int y = 1; y <= years; y++); //<-- remove this

应该是这样的:

for (int y = 1; y <= years; y++) {
//rest of code...
}

关于java - 编译器错误 : <identifier> expected. 如何解决此问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26856507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com