Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Simple guess game

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple guess game

    Hello,

    im trying to make a simple guessing game in ecclipse

    i have the error "c connot be resolved to a variable.

    import java.util.Random;
    import java.util.Scanner;
     
    public class Game {
     
    	public static void main(String[] args) {
     
    		question();
     
    	}
     
    	static void question() {
    		System.out.println("Guess the number between 1 and 10");
     
    		Random rand = new Random();
    		Scanner myObj = new Scanner(System.in);
     
    		int a = rand.nextInt(10-0)+1;
    		String userGuess = myObj.nextLine();
     
    		String b = userGuess;
    		c = a.toString();
    		System.out.println(userGuess);
     
    		if (b == c)
    		{
    			System.out.println("correct");
    		} else
    		{
    			System.out.println("wrong");
    		}
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple guess game

    Where is the variable: c declared? The compiler can not find its declaration.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple guess game

    ok now i have got this working but when i input the correct number it still says its wrong

    hmmmm

    import java.util.Random;
    import java.util.Scanner;
     
    public class Game {
     
    	public static void main(String[] args) {
     
    		question();
     
    	}
     
    	static void question() {
    		System.out.println("Guess the number between 1 and 10");
     
    		Random rand = new Random();
    		Scanner myObj = new Scanner(System.in);
     
    		int a = rand.nextInt(10-0)+1;
    		System.out.println(a);
    		String userGuess = myObj.nextLine();
     
    		String b = userGuess;
    		String c = Integer.toString(a);
    		System.out.println(userGuess);
     
    		if (b == c)
    		{
    			System.out.println("correct");
    		} else
    		{
    			System.out.println("wrong");
    		}
    	}
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple guess game

    Use the equals method to compare the contents of String objects, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple guess game

    Norm.......you are one clever cookie!

    thanks

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple guess game

    Why use Strings to hold integer values? Why not use int?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple guess game

    good point, i assumed that when a user typed something in consol it would be a string but now your making me think its an integer im going to try change now to use an int

    brb

    --- Update ---

    omg it worked
    haha sorry if my questions seem stupid thankyou for your help

    import java.util.Random;
    import java.util.Scanner;
     
    public class Game {
     
    	public static void main(String[] args) {
     
    		question();
     
    	}
     
    	static void question() {
    		System.out.println("Guess the number between 1 and 10");
     
    		Random rand = new Random();
    		Scanner myObj = new Scanner(System.in);
     
    		int a = rand.nextInt(10-0)+1;
    		System.out.println(a);
    		int userGuess = myObj.nextInt();
     
    		int b = userGuess;
    		System.out.println(userGuess);
     
    		if (a == b)
    		{
    			System.out.println("correct");
    		} else
    		{
    			System.out.println("wrong");
    		}
    	}
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple guess game

    variable names should describe the data they contain. For example
    instead of a use theNumber
    instead of b use userGuess

    That will make the code easier to read and understand. Not too important in a 10 line program. But it will be important in larger programs.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple guess game

    Thankyou will keep this in mind,

    On another note i was going well in eclipse but now was going to try this free MOOC java course and they wanted netbeans and TMCbeans but for some reason on windows it keeps crashing.

    Any tips on this?

    --- Update ---

    hey norm i have asked the question on the appropriate forumn thanks for your advice

Similar Threads

  1. Method error for checking and updating guess in a basic hangman game
    By lotusmind in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 13th, 2013, 10:03 PM
  2. Please help to understand whats the wrong with my guess. - Simple program
    By rayan2004 in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2012, 01:03 AM
  3. Help With a Simple Game
    By phlux in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2012, 05:51 PM
  4. Help with 3 guess Dice game
    By tabmanmatt in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2012, 07:42 PM
  5. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM