Exercise C: Write a java console program T hreeChoices . It’s output should be l
ID: 3922545 • Letter: E
Question
Exercise C:
Write a java console program ThreeChoices. It’s output should be like the following.
Here is the sample output:
What you want to do? Please choose one option
1) Sum of three numbers.
2) Average of three numbers.
3) Smallest of three numbers.
Enter your choice: 1
First number: 2
Second number: 3
Third number: 6
Sum is: 11
Do you want to continue? :yes
What you want to do? Please choose one option
1) Sum of three numbers.
2) Average of three numbers.
3) Smallest of three numbers.
Enter your choice: 2
First number: 3
Second number: 6
Third number: 9
Average is: 6
Do you want to continue? :YES
What you want to do? Please choose one option
1) Sum of three numbers.
2) Average of three numbers.
3) Smallest of three numbers.
Enter your choice: 3
First number: 2
Second number: 6
Third number: 1
Smallest is: 1
Do you want to continue? :NO
Thanks for using this program!
Note:
Must use switch statement.
Must use three methods (sum, average, smallest) calling inside the switch statement.
It should loop until user enters NO/no/No
For the choices of yes/no, it should not be case sensitive.
Yes/YES/yes/y/Y //All should be treated same
No/NO/no/n/N
It shouldn’t loop just 2 times, but it should loop until user enters.. no/NO/No/N/n
Text written in red are entered by the user.
Explanation / Answer
import java.util.*;
public class cheggexam {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("sum of three numbers");
System.out.println("Average of three numbers");
System.out.println("Smallest of three numbers");
System.out.println("please enter your choice:");
int ch=sc.nextInt();
String co="";
switch(ch){
case 1:sum();
System.out.println("do you want to continue:");
co=sc.next();
if(co.equalsIgnoreCase("no"))
System.exit(0);
else
break;
case 2:average();
System.out.println("do you want to continue:");
co=sc.next();
if(co.equalsIgnoreCase("no"))
System.exit(0);
else
break;
case 3:smallest();
System.out.println("do you want to continue:");
co=sc.next();
if(co.equalsIgnoreCase("no"))
System.exit(0);
else
break;
}
}
}
private static void smallest() {
System.out.println("First number:");
int first=sc.nextInt();
System.out.println("Second number:");
int second =sc.nextInt();
System.out.println("Third number:");
int third=sc.nextInt();
int small=0;
if(first<second &&first<third)
small=first;
else if(second<third)
small=second;
else
small=third;
System.out.println("smallest is:"+small);
}
private static void average() {
System.out.println("First number:");
int first=sc.nextInt();
System.out.println("Second number:");
int second =sc.nextInt();
System.out.println("Third number:");
int third=sc.nextInt();
int avg=first+second+third/3;
System.out.println("Avg is:"+avg);
}
private static void sum() {
System.out.println("First number:");
int first=sc.nextInt();
System.out.println("Second number:");
int second =sc.nextInt();
System.out.println("Third number:");
int third=sc.nextInt();
int sum=first+second+third;
System.out.println("sum is:"+sum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.