Java 编译器错误:<;标识符>;预期。我如何解决这个问题?
我在这项作业中犯了很多错误。我已经做了尽可能多的工作,似乎一切都井然有序,但JGrasp告诉我有49个错误。第30、31、33、36、37、40、41、42、43、47、48、54、55、56和57行出现了错误。根据我的课本,我做的每件事都是对的。我该如何解决这个问题 我在给我带来麻烦的每一行后面都添加了“here”注释 完整程序:Java 编译器错误:<;标识符>;预期。我如何解决这个问题?,java,Java,我在这项作业中犯了很多错误。我已经做了尽可能多的工作,似乎一切都井然有序,但JGrasp告诉我有49个错误。第30、31、33、36、37、40、41、42、43、47、48、54、55、56和57行出现了错误。根据我的课本,我做的每件事都是对的。我该如何解决这个问题 我在给我带来麻烦的每一行后面都添加了“here”注释 完整程序: import java.util.Scanner; public class AverageRainfall { public static void
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
}
import java.util.Scanner;
公共类AverageRainfall{
公共静态void main(字符串[]args){
double monthRain=0;//雨下了一个月
double totalRain=0;//雨量累加器
双倍平均值=0;//平均降雨量
//键盘输入。
扫描仪键盘=新扫描仪(System.in);
//年数。
System.out.print(“输入年份:”);
int years=0;//年数
年份=键盘.nextInt();
//检查输入。
而(年<1){
系统输出打印(“无效。输入1或更大:”);
输入其他年份=keyboard.nextInt();
}
}
最终整数月数=12;//每年的月数
System.out.println(“输入每个月的降雨量,单位为英寸。”);//此处
对于(int y=1;y缩进代码。主要问题如下:
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 (int y = 1; y <= years; y++) {
//rest of code...
}
for(int y=1;y缩进代码。主要问题如下:
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 (int y = 1; y <= years; y++) {
//rest of code...
}
for(int y=1;y缩进代码。主要问题如下:
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 (int y = 1; y <= years; y++) {
//rest of code...
}
for(int y=1;y缩进代码。主要问题如下:
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 (int y = 1; y <= years; y++) {
//rest of code...
}
for(int y=1;y您已经关闭了主块,因此years变量超出了没有方法块的语句的范围
我认为您在某个地方有一个额外的大括号,或者缺少一个。您关闭了主块,因此年份变量超出了奇怪地没有方法块的语句的范围
我认为您在某个地方有一个额外的大括号,或者缺少一个。您关闭了主块,因此年份变量超出了奇怪地没有方法块的语句的范围
我认为您在某个地方有一个额外的大括号,或者缺少一个。您关闭了主块,因此年份变量超出了奇怪地没有方法块的语句的范围
我认为您在某处有一个额外的大括号,或者缺少一个。删除for循环声明后的分号。input anotheryears=keyboard.nextInt();
看起来很奇怪。我想应该是int
吧。也许你应该告诉我们错误是什么。或者,更好的办法是,阅读错误消息,自己找出错误所在。如果你费心阅读的话,这些消息实际上会说些什么。@Thilo:是的,应该。谢谢你指出这一点。删除for循环decl后面的分号aration.input anotheryears=keyboard.nextInt();
看起来很奇怪。我想应该是int
吧。也许你应该告诉我们错误是什么。或者,更好的办法是,阅读错误消息,自己找出错误所在。如果你费心阅读的话,这些消息实际上会说些什么。@Thilo:是的,应该。谢谢你指出这一点。删除for循环decl后面的分号aration.input anotheryears=keyboard.nextInt();
看起来很奇怪。我想应该是int
吧。也许你应该告诉我们错误是什么。或者,更好的办法是,阅读错误消息,自己找出错误所在。如果你费心阅读的话,这些消息实际上会说些什么。@Thilo:是的,应该。谢谢你指出这一点。删除for循环decl后面的分号aration.input anotheryears=keyboard.nextInt()
看起来很奇怪。我想应该是int
吧。也许你应该告诉我们错误是什么。或者,更好的办法是,自己阅读错误消息,找出错误所在。如果你费心去读的话,这些消息实际上说了些什么。@Thilo:是的,应该。谢谢你指出了这一点。这样做了!非常感谢你。这样做了!太感谢了,太感谢了,太感谢了,太感谢了,太感谢了。