In: Computer Science
Can you fix the errors in this code?
import java.util.Scanner;
public class Errors6 {
public static void main(String[] args) {
System.out.println("This program
will ask the user for three sets of two numbers and will calculate
the average of each set.");
Scanner input = new Scanner(System.in);
int n1, n2;
System.out.print("Please enter
the first number: ");
n1 = input.nextInt();
System.out.print("Please enter
the second number: ");
n2 = input.nextInt();
int average;
average = (n1+n2)/2;
System.out.print("The average of
the numbers is " + average);
Scanner input = new Scanner(System.in);
float n1, n2;
System.out.print("Please enter
the first number:");
n1 = input.nextFloat();
System.out.print("Please enter
the second number: ");
n2 = input.nextFloat();
float average;
average = (n1+n2)/2;
System.out.print("The average of
the numbers is " + average);
short s1, s2;
System.out.print("Please enter
the first number: ");
s1 = input.nextShort();
System.out.print("Please enter
the second number: ");
s2 = input.nextShort();
short shortAvg;
shortAvg = (short)(n1+n2)/2;
System.out.print("The average of
the numbers is " + shortAvg);
}
}
Program:
Explanation:
Line 1: //Header file for scanner object
Line 2: //Here taking class name as Errors6
Line 3: //Start of main()
Line 4: //Printing the statement
Line 5:// Here creating the scanner object called input
Line 6://Here decalring the integer varaibles called n1 &
n2
Line 7://Here asking the user to enter first number
Line 8://Here scanning the user input for n1
Line 9://Here asking the user to enter second number
Line 10://Here scanning the user input for n2
Line 11://Here decalring the integer varaibles called average
Line 12://Calculating the average value
Line 13://Here printing the value of average
Line 14://Here decalring the float varaibles called n3 &
n4
Line 15://Here asking the user to enter first number
Line 16://Here scanning the user input for n3
Line 17://Here asking the user to enter second number
Line 18://Here scanning the user input for n4
Line 19://Here decalring the float varaibles called FloatAvg
Line 20://Calculating the FloatAvg value
Line 21://Here printing the value of FloatAvg
Line 22://Here decalring the short varaibles called s1 &
s2
Line 23://Here asking the user to enter first number
Line 24://Here scanning the user input for s1
Line 25://Here asking the user to enter second number
Line 26://Here scanning the user input for s2
Line 27://Here decalring the short varaibles called shortAvg
Line 28://Calculating the shortAvg value
Line 29://Here printing the value of shortAvg
Line 35: //End of main()
Line 36://End of class Errors6
Program:
import java.util.Scanner;
class Errors6 {
public static void main(String[] args) {
System.out.println("This program will ask the user for three sets
of two numbers and will calculate the average of each set.");
Scanner input = new Scanner(System.in);
int n1, n2;
System.out.print("Please enter the first number: ");
n1 = input.nextInt();
System.out.print("Please enter the second number: ");
n2 = input.nextInt();
int average;
average = (n1+n2)/2;
System.out.print("The average of the numbers is " + average);
float n3, n4;
System.out.print("Please enter the first number:");
n3 = input.nextFloat();
System.out.print("Please enter the second number: ");
n4 = input.nextFloat();
float FloatAvg;
FloatAvg = (n3+n4)/2;
System.out.print("The average of the numbers is " +
FloatAvg);
short s1, s2;
System.out.print("Please enter the first number: ");
s1 = input.nextShort();
System.out.print("Please enter the second number: ");
s2 = input.nextShort();
short shortAvg;
shortAvg = (short)((s1+s2)/2);
System.out.print("The average of the numbers is " +
shortAvg);
}
}
Output: