In: Computer Science
Understanding if-elseStatements
Summary
In this lab, you will complete a prewritten Java program that computes the largest and smallest of three integer values. The three values are –50, 53, and 78.
Instructions
The largest value is 78 The smallest value is −50
Grading
When you have completed your program, click the Submit button to record your score.
// LargeSmall.java - This program calculates the largest and smallest of three integer values.
public class LargeSmall
{
public static void main(String args[])
{
// This is the work done in the housekeeping() method
// Declare and initialize variables here.
int largest; // Largest of the three values.
int smallest; // Smallest of the three values.
// This is the work done in the detailLoop() method
//Write assignment, if, or if else statements here as appropriate.
// This is the work done in the endOfJob() method
// Output largest and smallest number.
System.out.println("The largest value is " + largest);
System.out.println("The smallest value is " + smallest);
}
}
import java.util.Scanner; public class LargeSmall { public static void main(String args[]) { // This is the work done in the housekeeping() method // Declare and initialize variables here. Scanner in = new Scanner(System.in); int n1, n2, n3; int largest; // Largest of the three values. int smallest; // Smallest of the three values. // This is the work done in the detailLoop() method System.out.print("Enter three integers: "); n1 = in.nextInt(); n2 = in.nextInt(); n3 = in.nextInt(); largest = n1; if (n2 > largest) largest = n2; if (n3 > largest) largest = n3; smallest = n1; if (n2 < smallest) smallest = n2; if (n3 < smallest) smallest = n3; // This is the work done in the endOfJob() method // Output largest and smallest number. System.out.println("The largest value is " + largest); System.out.println("The smallest value is " + smallest); } }