I am making a program which asks the user to input a number and ifthat number co
ID: 3613914 • Letter: I
Question
I am making a program which asks the user to input a number and ifthat number corresponds to my integer stored in the final variablethe user is declared a winner. However if they don't win the firsttime l would like them to have two more chances. I am stuck on howto make a loop which gives the user two more chances if they don'tget the number correctly.Here is my code :
import javax.swing.*;
public class luckynumber1
{
public static void main (String []args)
{
final int LUCKY_NUMBER =50;
double i = Double.parseDouble(JOptionPane.showInputDialog(" Enter a lucky number"));
if(LUCKY_NUMBER/i==1)System.out.println("We got a winner");
if(LUCKY_NUMBER/i!=1)System.out.println(" Sorry try again");
}
}
Explanation / Answer
please rate - thanks 1. How did you the loop going ? Did you use "while(! luck)? YES 2. How do l get the loop to terminate after three attempts ? see changes in red below-I added acounter import javax.swing.*; import java.io.*; public class luckynumber1 { public static void main (String []args) { final int LUCKY_NUMBER =50; intcount=0; boolean luck = false; double i; while(!luck&&countRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.