如何添加双精度命令,以便给出十进制平均值



我正在尝试编写代码,使 3 个输入的测试分数与小数平均。当我输入双重命令时,它给了我一个错误。它在没有双重命令的情况下运行良好,但是当我尝试添加它时,它不起作用,我试图在线查找但找不到任何东西。请帮忙

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:UsersGuesttDesktopCSEFirstLab.java:18: error: not a statement
int double NUM_TEST= 3;
^
C:UsersGuesttDesktopCSEFirstLab.java:18: error: ';' expected
int double NUM_TEST= 3;
^
C:UsersGuesttDesktopCSEFirstLab.java:30: error: '.class' expected
(test3 + test2 + test1) / double(NUM_TEST);
^
C:UsersGuesttDesktopCSEFirstLab.java:30: error: ';' expected
(test3 + test2 + test1) / double(NUM_TEST);
^
C:UsersGuesttDesktopCSEFirstLab.java:30: error: illegal start of expression
(test3 + test2 + test1) / double(NUM_TEST);
^
5 errors
Tool completed with exit code 1

首先,变量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));

:)

最新更新