Modify before asshignment so that we can check input errors, etc.. Please refer
ID: 3933843 • Letter: M
Question
Modify before asshignment so that we can check input errors, etc.. Please refer to the following Java code.
You should add switch, run your programing untill you choose figure==0 (using while loop), cheak input error.
import java.util.*;
public class AreaTestt {
public static void main (String[] args)
{
int choice1 = menu(1);
int choice2 = menu(2)
}
// get user choice
public static int menu(int n)
{
int choice = 0;
Scanner scanner = new Scanner(System.in);
if(n == 1)
{
System.out.print("Information for figure 1: Type 1) rectangle 2) triangle 3) circle: ");
choice = scanner.nextInt();
}
else if(n == 2)
{
System.out.print("Information for figure 2: Type 1) rectangle 2) triangle 3) circle: ");
choice = scanner.nextInt();
}
return choice;
}
}
Explanation / Answer
import java.util.*;
public class AreaTestt {
public static void main(String[] args) {
menu();
}
// get user choice
public static void menu() {
int choice = 0;
Scanner scanner = new Scanner(System.in);
do {
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out
.print("Information for figure 1: Type 1) rectangle 2) triangle 3) circle: ");
break;
case 2:
System.out
.print("Information for figure 2: Type 1) rectangle 2) triangle 3) circle: ");
break;
default:
break;
}
} while (choice != 0);
}
}
OUTPUT:
2
Information for figure 2: Type 1) rectangle 2) triangle 3) circle: 3
1
Information for figure 1: Type 1) rectangle 2) triangle 3) circle: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.