Welcome to Dream.In.Code
Become a Java Expert!

Join 150,366 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,771 people online right now. Registration is fast and FREE... Join Now!




Fixed amount of guesses user can enter

 
Reply to this topicStart new topic

Fixed amount of guesses user can enter, A guessing game between 1 and 1000

jaden165
4 Feb, 2008 - 03:43 AM
Post #1

New D.I.C Head
*

Joined: 19 Jan, 2008
Posts: 4

The program below works fine, its just asking the user to guess a number between 1 and 1000. What I want to know is how to give the user only a fixed amount of guesses.

CODE

import javax.swing.*;
public class GuessingGame {
    
    public static void main( String args[]) {
        int value=1,num=0;
        String a;
        
        value = 1 + (int) (Math.random() * 1000 );    
        
        while (num != value) {
        
        a= JOptionPane.showInputDialog( "Product number");
           num = Integer.parseInt( a );

        if(num < value) {
        JOptionPane.showMessageDialog(null,"Too low!");
      }
        else
        if(num > value) {
        JOptionPane.showMessageDialog(null,"Too High!");    
      }
         else
        if(num == value) {
        JOptionPane.showMessageDialog(null,"Correct!");    
      }
        
}    
    }    
        
}

User is offlineProfile CardPM
+Quote Post

KYA
RE: Fixed Amount Of Guesses User Can Enter
4 Feb, 2008 - 04:09 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,941



Thanked: 159 times
Dream Kudos: 1375
My Contributions
QUOTE(jaden165 @ 4 Feb, 2008 - 05:43 AM) *

The program below works fine, its just asking the user to guess a number between 1 and 1000. What I want to know is how to give the user only a fixed amount of guesses.

CODE

import javax.swing.*;
public class GuessingGame {
    
    public static void main( String args[]) {
        int value=1,num=0, maxGuesses = 3;
        String a;
        
        value = 1 + (int) (Math.random() * 1000 );    
     for (int i = 0; i < maxGuesses; i++)        
           while (num != value) {
        a= JOptionPane.showInputDialog( "Product number");
           num = Integer.parseInt( a );

        if(num < value) {
        JOptionPane.showMessageDialog(null,"Too low!");
      }
        else
        if(num > value) {
        JOptionPane.showMessageDialog(null,"Too High!");    
      }
         else
        if(num == value) {
        JOptionPane.showMessageDialog(null,"Correct!");    
      }
        
}    
    }    
        
}




You could define an int such as int maxGuesses = 3; or something. Then have it increment each guess and have it exit the logic loop after the amount of guesses has been reached. Could even have a message that displays to the user how many guesses are left. There are several ways to do this I added a possible route to achieve this in your code quote:

CODE

int maxGuesses = 3;

//code
for (int i = 0; i < maxGuesses; i++)
//code....


--KYA

This post has been edited by KYA: 4 Feb, 2008 - 04:15 AM
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month