This is my second week in a Intro to Java course and I am finishing up a lab. I got a 10/10 for this but feel like the last 5 lines of code were inefficient (and I’m sure the entire thing is lol). The prompt is to get the product and average from four integers, outputting integer division for the first part. Then the second part asks to convert int to double. Any help and advice is appreciated!
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num1;
int num2;
int num3;
int num4;
/* Type your code here. */
num1 = scnr.nextInt();
num2 = scnr.nextInt();
num3 = scnr.nextInt();
num4 = scnr.nextInt();
product = num1 * num2 * num3 * num4;
average = (num1 + num2 + num3 + num4) / 4;
System.out.println(product + " " + average);
double product2 = (double)num1 * num2 * num3 * num4;
double average2 = ((double)num1 + num2 + num3 + num4) / 4;
System.out.printf("%.3f", product2);
System.out.print(" ");
System.out.printf("%.3fn", average2);
}
}
New contributor
Isaac Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.