1 Replies - 158 Views - Last Post: 17 April 2013 - 11:31 PM Rate Topic: -----

#1 udarijava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-April 13

how to display the correct answer after getting all the tries

Posted 17 April 2013 - 09:12 PM

import javax.swing.JOptionPane;
public class Guess
{
public static void main (String [] args)
{
int randomNumber = (int) (Math.random() * 10) + 1;

char repeat = 'N';

int zero = 0;
String again ;
boolean want = true;

while(want = true){
again = JOptionPane.showInputDialog ("Do you want to continue (y/n)? ");


String firstName = JOptionPane.showInputDialog ("Enter your first name : ");

//get the family name from the user
String familyName = JOptionPane.showInputDialog ("Enter family name : ");



String numberOfGuesses = JOptionPane.showInputDialog ("How many guesses do you want? : ");
int number = Integer.parseInt(numberOfGuesses );


for(int r=0; r<number; r++){

String Number = JOptionPane.showInputDialog ("Guess number" + (r+1)+ "between 1 and 10");
int Num = Integer.parseInt(Number );

if(Num == randomNumber){
r= 59;
// convert first name in to uppercase
String firstNameUpper = firstName.toUpperCase();

//find the first character of the first name
char firstChar = firstNameUpper.charAt(0);

// convert first name in to uppercase
String familyNameUpper = familyName.toUpperCase();


JOptionPane.showMessageDialog (null, firstChar + " " + familyNameUpper + " "+
"You got it!!" +
"\nGood guess!! ");

r = r+number;
}



else if (Num != randomNumber)
{
JOptionPane.showMessageDialog (null, "That is not correct, sorry.");
}



{
JOptionPane.showMessageDialog (null, "The number was" + randomNumber);

}
}
}
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: how to display the correct answer after getting all the tries

#2 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: how to display the correct answer after getting all the tries

Posted 17 April 2013 - 11:31 PM

You could set up a counting variable and set it at 0, and have it go up by one each time they guess. Once that number is the same as the number of guesses, print out the correct answer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1