Issue
I am trying to make code that averages 3 inputted tests scores with decimals. When i put in the double command it gives me an error. it works well without the double command but when i try to add it it just doesnt work, ive tried to look online but cant find anything. Please help
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 errors
Tool completed with exit code 1
Solution
Firstly, the variable NUM_TEST
is declared as int double
; simply writing double
is sufficient.
Also, in the last print statement, there is an unmatched left parenthesis; it is also enough to reference the variable name NUM_TEST
instead of writing double(NUM_TEST)
The following code is slightly fixed and should work:
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));
:)
Answered By - ML72 Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.