Hello guys. I am rather new to Java and so I decided to test my knowledge and write a simple number averaging program. When I compile it though I get the following errors:
NumberAverager. java:14: cannot find symbol
symbol : variable n1
location: class NumberAverager
int area = (n1 + n2 + n3) / 3;
^
NumberAverager. java:14: cannot find symbol
symbol : variable n2
location: class NumberAverager
int area = (n1 + n2 + n3) / 3;
^
NumberAverager. java:14: cannot find symbol
symbol : variable n3
location: class NumberAverager
int area = (n1 + n2 + n3) / 3;
^
NumberAverager. java:14: operator / cannot be applied to java.lang.Strin g,int
int area = (n1 + n2 + n3) / 3;
^
4 errors
Here is my source code. I tried to add a lot of comments.
NumberAverager. java:14: cannot find symbol
symbol : variable n1
location: class NumberAverager
int area = (n1 + n2 + n3) / 3;
^
NumberAverager. java:14: cannot find symbol
symbol : variable n2
location: class NumberAverager
int area = (n1 + n2 + n3) / 3;
^
NumberAverager. java:14: cannot find symbol
symbol : variable n3
location: class NumberAverager
int area = (n1 + n2 + n3) / 3;
^
NumberAverager. java:14: operator / cannot be applied to java.lang.Strin g,int
int area = (n1 + n2 + n3) / 3;
^
4 errors
Here is my source code. I tried to add a lot of comments.
Code:
/* Number Averaging Program Written by Kid Programmer */ import java.util.Scanner; class NumberAverager { //define the class public static void main(String[] args) { //make the main function Scanner scan = new Scanner (System.in); //create a scanner System.out.println("This program will average 3 numbers."); //explain the program System.out.println("Please enter the three numbers: "); //ask the user to enter the numbers while ( scan.hasNextInt() ) { int n1 = scan.nextInt(); //prompt the user for the first number int n2 = scan.nextInt(); //prompt the user for the second number int n3 = scan.nextInt(); //prompt the user for the third number } int area = (n1 + n2 + n3) / 3; //calculate the area System.out.println("The area is: " +area); //print out the area } }
Comment