我正在尝试编写代码,对 3 个输入的带小数的测试分数进行平均。当我输入 double 命令时,它给了我一个错误。它在没有双重命令的情况下运行良好,但是当我尝试添加它时它不起作用,我试图在线查看但找不到任何东西。请帮忙import java.util.Scanner;class FirstLab{ public static void main(String[] args) //header of the main method { Scanner in = new Scanner(System.in); int test1 , test2 , test3; int double NUM_TEST= 3; System.out.print("Input first test: "); // user prompt test1 = in.nextInt(); // read in the next integer System.out.print("Input second test: "); // user prompt test2 = in.nextInt(); System.out.print("Input third test: "); // user prompt test3 = in.nextInt(); System.out.println("Average test score is: " + (test3 + test2 + test1) / double(NUM_TEST); }}this is the error message:C:\Users\Guestt\Desktop\CSE\FirstLab.java:18: error: not a statement int double NUM_TEST= 3; ^C:\Users\Guestt\Desktop\CSE\FirstLab.java:18: error: ';' expected int double NUM_TEST= 3; ^C:\Users\Guestt\Desktop\CSE\FirstLab.java:30: error: '.class' expected (test3 + test2 + test1) / double(NUM_TEST); ^C:\Users\Guestt\Desktop\CSE\FirstLab.java:30: error: ';' expected (test3 + test2 + test1) / double(NUM_TEST); ^C:\Users\Guestt\Desktop\CSE\FirstLab.java:30: error: illegal start of expression (test3 + test2 + test1) / double(NUM_TEST); ^5 errorsTool completed with exit code 1
1 回答
呼如林
TA贡献1798条经验 获得超3个赞
首先,变量NUM_TEST声明为int double;只需书写double就足够了。
另外,在最后的 print 语句中,有一个不匹配的左括号;引用变量名也可以,NUM_TEST不用写double(NUM_TEST)
以下代码稍作修改,应该可以正常工作:
Scanner in = new Scanner(System.in);
int test1 , test2 , test3;
double NUM_TEST= 3;
System.out.print("Input first test: "); // user prompt
test1 = in.nextInt(); // read in the next integer
System.out.print("Input second test: "); // user prompt
test2 = in.nextInt();
System.out.print("Input third test: "); // user prompt
test3 = in.nextInt();
System.out.println("Average test score is: " + (test3 + test2 + test1) / (NUM_TEST));
:)
添加回答
举报
0/150
提交
取消