Java 1: Code a for loop with the switch structure from 1d and allow 4 attempts.
ID: 3811152 • Letter: J
Question
Java 1:Code a for loop with the switch structure from 1d and allow 4 attempts. Prompt the user to enter the qualified value. When the number entered is in the correct range exit the loop (not the program) after one of the messages in 1d is printed. Print the message “You’re out of attempts!” when it is the last attempt. Java 1:
Code a for loop with the switch structure from 1d and allow 4 attempts. Prompt the user to enter the qualified value. When the number entered is in the correct range exit the loop (not the program) after one of the messages in 1d is printed. Print the message “You’re out of attempts!” when it is the last attempt.
Code a for loop with the switch structure from 1d and allow 4 attempts. Prompt the user to enter the qualified value. When the number entered is in the correct range exit the loop (not the program) after one of the messages in 1d is printed. Print the message “You’re out of attempts!” when it is the last attempt.
Explanation / Answer
package chegg;
import java.util.Scanner;
public class SwitchExample {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int value=0;
int low = 1; // range 1-10
int high=10;
int i,k=0;
for(i=1;i<=4;i++)
{
System.out.println("Enter the qualified value attempt ("+i+")");
value= sc.nextInt();
if((value>=low) && (value<=high))
{
break;
}
else
{
switch(i)
{
case 1 :
case 2 :
case 3 : break;
case 4 : System.out.println("“You’re out of attempts!”" );
break;
}
}
}
if((value>=low) && (value<=high))
System.out.println("1d is printed! Enter value "+value+" in range "+ low +"-"+high);
}
}
-----------------------------------------------
Output sample 1:
Enter the qualified value attempt (1)
1
1d is printed! Enter value 1 in range 1-10
----------------------------------------------------------
Output sample 2:
Enter the qualified value attempt (1)
11
Enter the qualified value attempt (2)
23
Enter the qualified value attempt (3)
6
1d is printed! Enter value 6 in range 1-10
------------------------------------------------------------
Output sample 3:
Enter the qualified value attempt (1)
51
Enter the qualified value attempt (2)
14
Enter the qualified value attempt (3)
68
Enter the qualified value attempt (4)
43
“You’re out of attempts!”
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.