编写名为Temperature的完整java程序,其中包含一个for循环结构,提示用户输入5个温度值。程序应将循环中输入的每个温度相加,并将其存储在名为 Total 的变量中。使用系统语句,输出总温度值和平均温度值。使用下面的示例输出作为指导: 总温度= 平均气温=
我的答案是下面的语句,但我仍然收到错误:-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner temp = new Scanner (System.in);
double total = 0;
for(int=1;int<=5;int++);
{
System.out.println ("Enter temperature #" + temp +":");
double temperature = temp.nextDouble ();
total = temperature;
}
System.out.println("The total temperature =" +total);
System.out.println("The average temperature=" +(double)(total/5));
}
}
最佳答案
OP 没有说明您到底遇到了什么错误,但我可以猜测其中一些错误来自:
for(int=1;int<=5;int++);
你不能像这样使用int
。它是一种类型,应按如下方式使用:
for(int itr = 1; itr <= 5; itr++) { ...
此外,for 循环中的分号不需要在那里。
关于java - 编写脚本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61635774/