PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How to add double commands so it gives decimal average

 August 11, 2022     decimal, java     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing