Lottery Game - Number Guessing Program

Sep 6, 2014

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();

[Code] .....

View Replies


ADVERTISEMENT

Guessing Game Always Choose Same Number

Jan 4, 2015

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;

[Code] ....

View Replies View Related

Tracking Number Of Games Within Guessing Game

Aug 22, 2014

/*
/ GuessGame.java
*/

import java.util.Scanner;
import java.util.Random;
public class GuessGame {
public static void main(String[] args) {

[code]....

tracking the number of games played, and also when running the code, I have to press "1" twice for it to actually load up the game again.

View Replies View Related

Broken Loop - Number Guessing Game

Jun 3, 2014

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.

View Replies View Related

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();

[Code] .....

View Replies View Related

Guessing Game GUI - Comparing Guess With Randomly Generated Number

Apr 13, 2014

package guess.the.numbers;
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
ublic class GuessTheNumbers extends JFrame{
private JButton guessBtn;
private JButton restartBtn;

[Code] ....

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)

[Code] .....

View Replies View Related

Guessing Game Program

Feb 10, 2015

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;

[code]....

View Replies View Related

Number Guessing Program - Computer Guesses A Number That User Is Thinking Of Within Certain Range

Mar 28, 2015

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;

[Code] ....

View Replies View Related

Guessing Game - How To Reset Random Number When User Reset Game

Oct 2, 2014

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";

[Code] ......

View Replies View Related

Java Program That Randomly Generates Five-digit Lottery Number And Prompts User For Input

Sep 25, 2014

Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:

• The user wins the first prize if the user input matches all five digits in the lottery number in exact order.
• The user wins the second prize if the user input matches any four digits in the lottery number in exact positions.
• The user wins the third prize if the user input matches any three digits in the lottery number in its exact position.
• The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.
• The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.

and here is my code. I cant get it to print the right statements.

import java.util.Scanner;
import java.util.Random;
class Hw5 {
static int getPrize(int g1, int g2, int g3, int g4, int g5,
int u1, int u2, int u3, int u4, int u5) {
//code for determining the prize comparing (g1, g2, g3, g4, g5) to (u1, u2, u3, u4, u5)
if (u1 == g1 && u2 == g2 && u3 == g3 && u4 == g4 && u5 == g5)

[code]....

View Replies View Related

Java Program With Random Number Guessing?

Jun 16, 2014

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.");
^

How do I fix this?

View Replies View Related

Java Application That Functions As Lottery Game

Nov 23, 2014

I am writing a java application that will function as a lottery game, using arrays it prompts the user to enter 5 digits and matches them against the random lottery numbers generated.

import java.util.Random;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
int NUM_DIGITS = 5;

[Code] ....

View Replies View Related

Lottery Game Matching Numbers Error

Dec 11, 2014

import java.util.Scanner;
import java.util.Arrays;
public class LotteryTester {
public static void main(String[] args) { 
LotteryApplication lottery = new LotteryApplication();
int lotteryNumbersCount = lottery.getLotteryNumbers().length;
 
 [Code] ....

Everything works fine but when I run it the matching numbers tends to be off. For example I will type Enter in 1,1,1,1,1 for my 5 numbers. The lottery numbers will be 5,3,4,5,8 and it will say 1 number matched.

View Replies View Related

Guessing Game - Won't Add Buttons To Frame

Aug 6, 2014

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.

View Replies View Related

Develop A Guessing Game Application

Nov 6, 2014

Here is my instangable class

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(){

View Replies View Related

Creating A Guessing Game In Java

Nov 30, 2014

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.

[code]...

View Replies View Related

Design And Implement Word Guessing Game

Dec 29, 2013

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.

View Replies View Related

Guessing Game Demo - How To Add DoAgain Loop

Apr 8, 2014

Ok, here's what I got.
 
import java.util.Random;
import java.util.Scanner;
 
/*
* 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?"

View Replies View Related

Guessing Game (numbers Between 1 And 100) - Getting Results To Give Correct Int

Feb 10, 2015

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;

[Code] .....

View Replies View Related

Lottery Odds Program - Using For And While Loops With Calculations

Oct 13, 2014

/**
* In this program, use for and while loops with calculations to get the lottery odds.
*/
import java.util.Scanner;
import java.util.Random;
public class Lottery
{
public static void main (String []args)

[Code] .....

I need getting my numbers matching. When, I run the program each time no matter what number it keeps saying sorry no matches. However, I do need the program to have matches.

View Replies View Related

Quiz Game Program - Random Number Generator

Nov 18, 2014

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.

View Replies View Related

Tic Tac Toe Game - Random Number Generator And Two Dimensional Arrays For Game Board

May 9, 2015

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;
 
[Code] ....

View Replies View Related

ArrayIndexOutOfBoundsException In Lottery Application

Jul 25, 2014

I have tried changing the bounds of the arrays but I still don't see where the exception is occurring or how to resolve this issue. Am I missing something blatantly obvious? Below are the two class files for the lottery application that I've been working on for a school assignment.

package com.lottery.test;
import com.lottery.Alarcon.*;
import java.util.Scanner;
public class PlayLottery {
public static void main(String [] args){
System.out.println("CIS-132. Lab 8. Problem #9 - Lottery. Faber Alarcon.");
Lottery lottery = new Lottery();

[Code] ....

I forgot to post the error message I was getting, here it is.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at com.lottery.Alarcon.Lottery.compareNumbers(Lottery.java:26)
at com.lottery.test.PlayLottery.main(PlayLottery.java:27)
Java Result: 1

View Replies View Related

Program That Takes A User Inputted Number And Converts It To A Binary Number

May 5, 2014

trying to write a program that takes a user inputted number and converts it to a binary number.

Here's what I have:

package com.java2novice.algos;
import java.util.Scanner;
public class Converter {
static Scanner console = new Scanner(System.in);
public void printBinaryFormat(int number){
int binary = console.nextInt();

[Code]...

Bugs on:

Line 13
Line 17
Line 23

View Replies View Related

Program Is To Accept Number And Display New Number After Removing All Zeros

Dec 29, 2014

ex
Sample input:2340980
Sample Output:23498

this is the program i have tried but is dosent work

import java.util.*;
class zero
{
public static void main(String args[])
{

[Code]....

View Replies View Related

Lottery - Sorting Numbers From Smallest To Biggest Without Repetition

Oct 29, 2014

I have problem with sorting my numbers from smallest to biggest and i need to not have repeating numbers, I am stuck. I tried using Arrays.sorts(lottery) but didn't work. and i don't know to but make the numbers not repeat in the same line.

package lottonumbers;

public class LottoNumbers {
public static void main(String[] args) {
int[] lottery = new int[6];
for (int numoftimes=0;numoftimes<5;numoftimes++){

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved