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


ADVERTISEMENT

Identity Program - Check If Randomly Generated Number Match Index

Apr 14, 2015

I have this program where I'm supposed to fill an array with 1000 indices with 1000 randomly generated numbers between 1 and 1000. The program is supposed to check if any of the numbers match an index that is the same value (so for example, the number 4 is in index 4). How to check for that condition, especially using a binary search (I'm also told to use a binary search).

Right now the variable index isn't initialized because I don't know what to initialize it to exactly. How do I check to see if any numbers match the value of the same index?

import java.util.*;
public class Identity {
public static void main(String[] args) {
int [] integers = new int [1000];
// Fill array with randomly generated numbers
int [] display = GenerateRandom(integers);

[Code] ....

View Replies View Related

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

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 View Related

Guess Number Game - Reveals Answer If User Input Is Wrong

Sep 1, 2014

The problem occurs after the user inputs the guess.... it either runs the for loop if guess = numtoguess and reveals the answer even if the user input is wrong.... or it always runs the first if statement in the while loop if guess!= to numtoguess... heres the code

public static void main(String[] args) {
String[] Answers = {"yes", "Yes", "No", "no"};
String Name = JOptionPane.showInputDialog(null, "Hello, What is your name?","Random Game", JOptionPane.QUESTION_MESSAGE);
String UI = JOptionPane.showInputDialog(null, Name + " do you want to play a game", "Random Game", JOptionPane.QUESTION_MESSAGE);

[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

Randomly Generated Labyrinths

Dec 13, 2014

So i am making this game with randomly generated labyrinths and i get error in array. Here is the code:

import java.util.Random;
public class Labyrinth {
 
//0-walls
//1-path
 
[code]....
 
I've got lost in some things so my code might contain unnecessary code.

View Replies View Related

Unable To Print Out Randomly Generated Array

Nov 19, 2014

i am trying to print out a randomly generated array, but i only get

[I@7852e922

i did some research and the "[" is for arrays, "I" is for the int and "@" is the hash. and the rest is some sort of hex. I need to override this in a way, but i can't seem to find out how.
this is my current code:

import java.util.Random;

public class Oppgave {
public static void main(String[] args){
int myint[] = fyll();
System.out.println(myint);
}
public static int[] fyll() {

[Code]...

View Replies View Related

Passing Parameters - Randomly Generated Numbers Not Appearing

Dec 14, 2014

For some reason, when I test out my code, my randomly generated numbers don't appear. Here is a sample result:

> What do you want to generate, integer, double, or character?

>integer

>What is the upper limit and lower limit of the integers you want to generate?

>1

>10

>How many integers do you want to generate?

>10

>BUILD SUCCESSFUL (total time: 9 seconds)

Is this because my code is not passing my parameters correctly? I'm not sure how to fix this either.

Here is my code for reference (it's not completed at the moment)

import java.util.Scanner;
public class NewNumberCharacter {
/** Main method
* @param args */
public static void main(String[] args) {
int return_int;
double return_double;

[Code] ....

View Replies View Related

Comparing Inputted Guess To Computer Random Sequence - Counter Not Working?

May 27, 2014

I have separated characters into an array that can be accessed from any method, however, when I try to compare the inputed guess to the computers random sequence, the counter always comes back as 0 (the comparison occurs in the checkColour method).

Java Code:

package mastermind;
import java.util.Scanner;
/**
* Program Description: play a game of mastermind versus the computer
*/
public class Mastermind {
private static int block1, block2, block3;
private static String blockColour1, blockColour2, blockColour3;

[Code] ....

The counter is called colourCorrect.. But I think the problem lies more in me converting the strings (input and computerGuess) into char arrays, as if I output the values at position in the array, I get numbers (but the numbers are not character values from the ascii table).

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

ESP Game - Cycle Through Colors After Each Guess Entered

Dec 16, 2014

I'm writing an ESP game code for class. The problem is i cant get it to cycle through the colors after each guess is entered. It stays fixed on the original random number generated.

import java.util.Random;
import java.util.Scanner;
public class Final1 {
String colorInput, computerColor;
int computerNum, right, wrong;

[Code] ....

This repeats 10x but the color never changes from the initial random chosen.

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

Hangman 2 Player Game - User Cannot Guess More Than 6 Times

May 27, 2014

I am creating a hangman code. So the game i am creating is 2 player, first player enters the word and second player guesses. i made the code but i don't know how to restrict the first player from adding random characters into the secret word because for my project the word can only contain letters. so i need making the code so the player 1 can only enter letters.

My code is below:

/**
* Auto Generated Java Class.
*/
import java.util.Scanner;
public class Hangman {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
String player1,player2;
int wrongguessamount = 0; //keeps the count of the amount of wrong guesses
int correctguessamount = 0; //keeps count of the amount of correct guesses
int maxincorrect=6;//makes sure the user cant guess more than 6 times

[Code] .....

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

Swing/AWT/SWT :: Hangman Game - No Graphics Show Up On Eighth Guess (Blank Frame)

Feb 5, 2015

I'm having some problems with the graphics of my hangman game. The graphic that's supposed to show up on the first guess (the hangman pole) doesn't show up until guess number 2. And on the eighth guess, no graphics show up (I just get a blank frame).

import java.awt.*;
import javax.swing.*;
public class HangmanFigure extends JPanel {
private int guesses;
private Image background;
public HangmanFigure() {
super();

[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 Lotto - Randomly Generate Five-digit Number And Prompts User To Enter Five-digit Number

Sep 25, 2014

1. 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.here is my code. I have tried replacing the && statements with || and it always returns either case 1 or case default.

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]...

View Replies View Related







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