Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 7
There are 1 users currently browsing forums.
Articles, Tutorials, and Guides Here is a collection of tutorials. You may submit tutorials and read them as you please.

Reply
  #1  
Old 10-27-2009
Stealther's Avatar
Stuck in the Code....
 
Join Date: Sep 2009
Location: In the code in the US
Age: 15
Posts: 82
Rep Power: 1
Stealther is on a distinguished road
A Simple Guessing Game Tutorial

Hello there fellow coders...my first attempt to make a tutorial will be on a simple Guessing Game!

Code:
//import the Scanner and Random classes for future use
import java.util.*;
public class GuessingGame 
{
    //make it a void because we are not returning a number...
    public static void playGame()
    {
        //Make a new Scanner for user input
        Scanner scan  = new Scanner(System.in);

        String another = "y";

        //Allows for multiple prompts
        while(another.equalsIgnoreCase("y"))
        {
            //The max a number for the user input for the game should be ten
            final int MAX = 10;
            int guess;

            //Chooses a random number from 1 - 10
            int answer = (int)(Math.random() * 9 + 1);

            System.out.print("Please choose a number from 1 to " + MAX + ": ");
            guess = scan.nextInt();

            //This makes sure that the user will only imput numbers from 0 to 10
            while(guess <= 0 || guess > 10)
            {
                System.out.print("Please enter a valid number: ");
                guess = scan.nextInt();
            }
            
            if(answer == guess)
                System.out.println("Hey...that's what I was thinking...good job!");
            else
                System.out.println("Sorry but the answer was: " + answer + ".");
                
            
            //A new scanner because when I used the old one I got an error
            Scanner playGame = new Scanner(System.in);

            //A prompt so the user can play again
            System.out.print("Want to go again?(y/n).....");

            another = playGame.nextLine();
        }
        
        
    }
    public static void main (String[] args) 
    {
        //Plays the game!
        playGame();
    }
    
    
}
Please if you can...give me constructive criticism because I wish to upload more files!
Reply With Quote
  #2  
Old 10-29-2009
AlbertoPL's Avatar
Java Expert
 
Join Date: Oct 2009
Age: 22
Posts: 2
Rep Power: 0
AlbertoPL is on a distinguished road
Thumbs up Re: A Simple Guessing Game Tutorial

Pretty good code. Perhaps you can even expand on the game and tell the user how many tries it took to get the answer.
Reply With Quote
  #3  
Old 10-29-2009
Stealther's Avatar
Stuck in the Code....
 
Join Date: Sep 2009
Location: In the code in the US
Age: 15
Posts: 82
Rep Power: 1
Stealther is on a distinguished road
Re: A Simple Guessing Game Tutorial

Oh okay....I'll do that(pretty simple) and when I'm done I'll post the code. I'm assuming to write the amount of tries and the amount of actual wins so I'm going to do what I assumed.
Reply With Quote
  #4  
Old 10-29-2009
Stealther's Avatar
Stuck in the Code....
 
Join Date: Sep 2009
Location: In the code in the US
Age: 15
Posts: 82
Rep Power: 1
Stealther is on a distinguished road
Re: A Simple Guessing Game Tutorial

Okay so I got the new version(EXTREMELY similar to the first). It shows you the amount of correct guesses, your amount of ties, and your total percentage of wins. I have not put comments on this one because if you read the first post then you should know already!

NOTE ONLY IN PHP CODE TO MAKE IT FLASHY!

PHP Code:
import java.util.*;
public class 
GuessingGame 
{
    public static 
void playGame()
    {
        
Scanner scan  = new Scanner(System.in);
        
String another "y";
        
int tries 0correctGuess 0;
        while(
another.equalsIgnoreCase("y"))
        {
            
            final 
int MAX 10;
            
int guess;
            
int answer = (int)(Math.random() * 1);
            
System.out.print("Please choose a number from 1 to " MAX ": ");
            
guess scan.nextInt();
            while(
guess <= || guess 10)
            {
                
System.out.print("Please enter a valid number: ");
                
guess scan.nextInt();
            }
            
            if(
answer == guess)
            {
                
System.out.println("Hey...that's what I was thinking...good job!");
                
correctGuess++;
                
tries++;                
            }
                
            else
            {
                
System.out.println("Sorry but the answer was: " answer ".");
                
tries++;
            }
            
Scanner playGame = new Scanner(System.in);
            
System.out.print("Want to go again?(y/n).....");
            
another playGame.nextLine();
        }
        
System.out.println("Your amount of tries: " tries);
        
System.out.println("Your amount of correct tries: " correctGuess);
        
System.out.println("Your winning percentage(approx): " + ((double)correctGuess tries) + "%");
        
        
    }
    public static 
void main (String[] args
    {
        
playGame();
    }
    
    

NOTE ONLY IN PHP CODE TO MAKE IT FLASHY!
Reply With Quote
  #5  
Old 10-30-2009
Teenager
 
Join Date: Jun 2008
Posts: 118
Rep Power: 3
tossy is on a distinguished road
Re: A Simple Guessing Game Tutorial

Good code....its very useful for me
Reply With Quote
  #6  
Old 10-31-2009
Stealther's Avatar
Stuck in the Code....
 
Join Date: Sep 2009
Location: In the code in the US
Age: 15
Posts: 82
Rep Power: 1
Stealther is on a distinguished road
Re: A Simple Guessing Game Tutorial

Quote:
tossy originally posted: View Post
Good code....its very useful for me

W00t I feel awesome that I've helped someone with my code!
Reply With Quote
  #7  
Old 10-31-2009
Stealther's Avatar
Stuck in the Code....
 
Join Date: Sep 2009
Location: In the code in the US
Age: 15
Posts: 82
Rep Power: 1
Stealther is on a distinguished road
Re: A Simple Guessing Game Tutorial

Sorry but I also forgot to mention the output of the game.
Here it is:
--------------------Configuration: <Default>--------------------
Please choose a number from 1 to 10: 5
Hey...that's what I was thinking...good job!
Want to go again?(y/n).....y
Please choose a number from 1 to 10: 3
Sorry but the answer was: 9.
Want to go again?(y/n).....y
Please choose a number from 1 to 10: 1
Sorry but the answer was: 7.
Want to go again?(y/n).....y
Please choose a number from 1 to 10: 4
Sorry but the answer was: 3.
Want to go again?(y/n).....n
Your amount of tries: 4
Your amount of correct tries: 1
Your winning percentage(approx): 0.25%

Process completed. *
*smiley does not occur
__________________
AP Computer Science should be a foreign language class
Reply With Quote


Reply

Tags
code, game, guessing


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Create a simple login script using ASP.NET bfsog .Net Programming 1 04-02-2010 01:49 PM
Help with Guessing Game Stealther Java Programming 6 10-10-2009 01:48 PM
Simple tutorial How to create an effect depending on your choices wizard Graphic Design Articles 1 01-26-2005 04:14 AM
Simple tutorial How to create an effect depending on your choices wizard Graphic Design Articles 0 01-02-2005 02:56 AM