Random Number Guessing Game With Three Tries (Loops)
Mar 8, 2014
I was told to write a program which generates a random number between 0 to 5 *including 5* and give the user 3 chances to guess this number:
-If the user enters a wrong number within 0 to 5, the user has lost one opportunity.
-If the user enters a number out of range, the program should prompt the user to enter a number within the range
-If the user has 3 unsuccessful attempts, the program will print the number.
I was also instructed to use :
Random randomNumber=new Random();
int i= randomNumber.nextInt(6)
I have attempted it and this is what i have so far.
import java.util.Random;
import java.util.Scanner;
public class Exercise1
{
public static void main(String[] args)
{
Random randomNumber=new Random();
I tried this program, its guessing game. Where program decide 1 number and user will guess number. I am getting 1 problem. When user want to play game again.the random number remain same. So where i can put reset random in code..? And 1 more question if I want to write driver code for this. What should i transfer to driver code.
import java.util.Random; import java.util.Scanner; public class GuessGame { public static void main(String args[]) { String choice="y";
Write I program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.
public class NumberGuess { public static void main(String[] args) { // Create a Random object. final static Random rand = new Random(); // max number is the upward bound number final int MAXNUMBER = 10; /** * Method should return a random number within the upward bound MAX_NUMBER.
[code]....
I get compiler error message
NumberGuess.java:25: error: ';' expected int getRandomNumber (); ^ NumberGuess.java:63: error: reached end of file while parsing System.out.println("You got it right in " + numberOfGuesses + " guesses."); ^
I am using eclipse for this project. Every time I run this program the choose method will always choose 0. Ignore the Sleep.sleep() method, there is another class in the program that performs it.
import java.util.Scanner; public class gamestart { public static int num; public static String name; public static String diff; public static String choice;
What's wrong with my code. It says 'you win' even when I guess an incorrect number. And then the 'win' message keeps repeating. I'm thinking I need a way to generate a new input? And then maybe take the 'win' message out of the loop, but that breaks it too.
I wrote the following code for a lottery game. The problem comes when I run it. If one number of a 3 digit number matches, it's supposed to print "you win 1000" But regardless if a number matches or not, it says "sorry, no matches" ...
import java.util.Scanner; public class Lottery { public static void main(String[] args){ //generate lottery number int lottery = (int)(Math.random() * 1000); //Prompt the user to enter numbers Scanner input = new Scanner (System.in); System.out.print("Enter your numbers (3 digits): "); int guess = input.nextInt();
I am getting a strange error and it almost seems like its not comparing it to the random generated number just the guess that i entered before. Here are my error messages.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at guess.the.numbers.GuessTheNumbers$ButtonHandler.actionPerformed(GuessTheNumbers.java:119) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
Im trying to make a tic tac toe game that you play against the computer using a random number generator and two dimensional arrays for the game board. Im not trying to make a GUI, the assignment is to have the board in the console, which I have done. I have run into a few problems with trying to get the computer player to correctly generate 2 integers and have those two integers be a place on the game board. Here is my code so far.
import java.util.Random; import java.util.Scanner; public class TicTacToe { private static Scanner keyboard = new Scanner(System.in); private static char[][] board = new char[3][3]; public static int row, col;
I've run into some problems with my Quiz-game program. What I basicly want it to do is generate a random number that is tied to the other windows I have for the program (I want a new random question to be generated once I have guessed on the first one). I've read about a few different ways to approach this but everything I get is a pure number generator.
I imported
import java.util.Random;
into my program but I don't know where to go from there.
I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.
-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:
Welcome to this first-ever mind-guessing program!
Please pick a range (higher than 1 and no larger than 50): 32
You will choose a number between 1 and 32... and I will try to guess it.
With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.
-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:
My fourth guess is 17: h My guess is too high?
My fifth guess is 16: h *** I am unlucky this round. I give up.
Let's play!
My first guess is 10: etc..
import java.util.*; import java.lang.Math; public class numguessprac1 { // Declaring variables public static String input; public static int quit; public static int guess; public static int wins;
I'm in an introduction CSE class and we're working with Java. Basically I need to create a program using scanner, while loops, and pseudorandom numbers. My program is a guessing game and I will post below the code that I already have. Basically I can get the game to work and get it to ask if you want to play another game and it keeps going until you say no. But my issue is getting the statistics of the game down. I need a separate method not in main to produce this at the end of the game once the user decides to stop playing:
Overall results: total games = int total guesses = int guesses/games = double best game = int
here's my code:
import java.util.*; public class Guess { public static void main(String[] args) { intro(100); Scanner console = new Scanner(System.in); game(console, 100); int numGuesses = 0; int numGames = 1; int end = 0;
I have a guessing game that im working on but it wont add my buttons to the frame, why?
// GuessGame.java // Guess the number import javax.swing.JFrame; public class GuessGame extends JFrame { public static void main(String args[]) { GuessGame panel = new GuessGame(); panel.setSize(400,350);
[Code] ...
The window pull up and even in the design view it shows everything on there but when i run its just a blank window.
The application should generate a random number between 1 and 10 and then ask the user to enter one single number between 1 and 10 to guess what the secret number is
public class Guess{
//Variables private int Num; private int rNo; private String message; //Constructors public Guess(){ rNo = 0; message = "";
[Code] .....
Two errors are
Guess.java:43: error: illegal start of expression public String getMessage(){
uess.java:43: error: ';' expected public String getMessage(){
I am creating a Guessing game program in java code. I am having an issue with the guess class and main/tester class running. The instructions for the game are The computer generates a random # and the user must guess that number in 7 or fewer guesses. If the guesses exceed 7, then the game is over and the user is asked if they want to 'play again?'These are things I need to incorporate into my code:
If Statement
A Loop of some kind At least three imported methods At least two methods you create Obtains input from the user At least two instance variables At least two local variables Some form of concatenation At least two calculations
import java.util.Random; public class Guess { int computersNumber; // A random number picked by the computer. int usersGuess = 0; // A number entered by user as a guess. int guessCount = 0; // Number of guesses the user has made.
I have a project about programing a game in java . In this project you are required to design and implement a word guessing game. In the game, user selects one of the available dictionaries (animals, plants, technical, names, etc. ), and the program randomly chooses one word from that dictionary and displays it by scrambling the letters. The user tries to guess the word while a guess counter starts to count down (lets assume it starts from 5).
While counter counts towards zero, the program chooses -randomly- two misplaced letters and puts them into correct positions to reveal the word more and give the user a hint. However, giving a new hint reduces the total value of the question. Lets say if the initial value of the question is 500 when all letters are scrambled, it reduces to 400 after 1st incorrect guess and hint, then after every incorrect guess it reduces more. You can determine the value of the question by the length of the word. The other details of the game is as follows:
1.You must design at least three classes (for example game, scrambledword, and dictionary).
2.Dictionaries are written in text files. If you design a class for dictionary, it must be constructed by a user chosen category ((animals, plants, technical, names, etc). In the constructor, the dictionary text file corresponding to this category must be opened and read; and words in that dictionary must be read into memory (e.g. String array). You must submit your dictionaries with your code. Do not share dictionaries between friends.
3.The game will record your total points and name in a text file where a hall of fame which has different users and their total points in 10 questions. You can design and implement a class for this.
4.Bonus+5%: Put user interface modules textboxes, buttons, etc Or
a.Use class hierarchy
b.Use of thread and timers and reducing the question value by timer instead of guesses.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
public class demoGuessGame { public static void main(String[] args) { Random any = new Random(); Scanner input = new Scanner (System.in); try { int maxNum, maxTries, guess; boolean win = false;
[Code] .....
I want to make it loop again when asked, "Do you want to play again?"
I've been working on a program where you can interact with the computer and play a guessing game of numbers between 1 and 100. It's mainly finished, I just need to add in my comments and stuff. My one problem about it that I cannot figure out is trying to get the results to give the correct int. At the end of the game, it's supposed to return the user information about how well they did like this:
Overall results: total games = int total guesses = int guesses/game = double best game = int
best game should give you the least amount of guesses you had during a particular round of the game. Here is what I have so far:
import java.util.*; public class Guess { public static final int MAX = 100; public static void main(String[] args) { intro(MAX); Scanner console = new Scanner(System.in); int tempBestGame = 0;
The program I'm supposed to create generates a random number between one to ten. Then the program is to ask me if I wish to cross the road. If you choose to cross, the outcomes for 0-2 are "You crossed safely." For 3-5, 75% of the time it should say "RIP you got run over", and 35% of the time it should say "You crossed the street." For 6-8, 60% of the time it should say you made it.", and 40% of the time it should say "You died". For 9-10, it should say "RIP".
So far I have gotten the random number generation part working. I have up to here:
import java.util.Random; public class test4 { public static void main(String[] args) { Random random = new Random(); for(int i =0; i < 1; i++){ int num = random.nextInt(10) + 1; System.out.println("The number of cars on the street are: " + num + "Do you wish to cross the road?"); } } }
I am working on a little nothing project, but I wanted to create a random number generator for a silly game where the user guesses the number.I have used google, but they are using LOG statements, what it does.
I am new to java, and now i have an assignment. I have to parse a phone number like this--> (656) 345 6544 to 6563456544. I've writen the following code that works great with a while loop. Now i would like to know how can i make it work with a for loop.
and the code is import java.io.*; public class Tema { public static String removeChar(String str, int i) { String first= str.substring(0,i);
import java.util.Random; import java.util.Scanner; public class DiceGame6 { public static void main(String[] args){ Scanner stdIn = new Scanner(System.in); System.out.println("Welcome to Computer Dice!"); System.out.println("-------------------------------"); System.out.println("You will first roll your dice
[code]....
How would I get the code to count the number of times something occurred. In the end it's suppose to count how many times I played the game and how many times I've won vs lost.
the program basically has a random number generating, and I want to ask the user to try to guess the number, and keep guessing until the number is right. In addition to this, I need to put in extra conditions for too high or too low by 10. So for example, if they user guesses a number and its off by more than 10, then it prints that they guessed too high, and if its below 10 they guessed too low.
import java.util.*; public class RandomNum { public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
[code].....
Modify the program to keep the user trying to guess the number till he/she gets it right, and stop once you guess the right number.Too high, high, too low, too low( If it's off by more than 10 = way too high, if its less than 10, way too low)
How would I get java to generate a random number from 0-2? all I've been able to find on random numbers is math.random which gives you from .0 to .10. you can multiply this by 10 to get 1-10 or by 100 to get 1-100 but how to make it so java will simply let me tell it the rand of numbers I want or get a random number in a range that is not a multiple of 10. and I can't seem to find anything about it anywhere.