1,105,214 Community Members

Guess a number 1-1000

Member Avatar
joe.crook.9
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

In this code, I am prompting the user to pick a number from 1-1000 and having the user keep it in his/her head.
Then, in 10 tries or less I am trying to have the computer guess the user's number.
This is what I have so far...

import java.util.*;

public class Guessnumber{
    public static void main(String[] args){ 
        System.out.println();
        System.out.println("Think of a number from 1-1000 and do not tell me,");
        System.out.println("I will try to guess the number.");
        System.out.println();
        int g=500;
        int numguesses=0;
        System.out.println("For too low enter 1.");
        System.out.println("For too high enter -1.");
        System.out.println("For correct enter 0.");
        System.out.println();
        System.out.println(g);
        int guessparam=parameter(g);
    }   



    public static int parameter(int g){

        Scanner console=new Scanner(System.in);
        int hint=console.nextInt();


            do{
                System.out.println("Is this too high, too low, or correct?");
                System.out.println(g);

                if(hint==1){
                    g=(g/2)+g;
                    return g;
                }else if(hint==-1){
                    g=g/2;
                    return g;
                }else
                    g=g;
                    return g;
            } while (hint!=0);
    }
}

I know the math is wrong, but when I run it the calculations aren't even performed. I am new to java and any guidance in the write direction would be much appreciated.

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

but when I run it the calculations aren't even performed

yes they are performed but you can't see the results since you don't print the variable you saved the value to after the calculation.

also remember that when you return a value from the method it returns the control to the calling method making your loop useless. Instead I suggest you use a loop at main

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Look at the position of {}s in the parameter() method. Some are missing that will effect how the code executes.
Indenting code does not make it be included within an if or else statement.

Member Avatar
jalpesh_007
Posting Whiz
385 posts since Sep 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 6 [?]
 
0
 

you can also return the value after do..while() loop.
because you want only one number as final.when you return in do...while loop it only executed inly once.

so modify it.write the return statement after do...while() loop.

thanks.

Member Avatar
amrita111
Junior Poster in Training
79 posts since Aug 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 1 [?]
 
0
 

Your loop is interating only once.If you write return statement after while loop in your program then it will be an infinite loop.Better make the function "parameter" recursive.

Member Avatar
jalpesh_007
Posting Whiz
385 posts since Sep 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 6 [?]
 
0
 

no..it will iterate until you found your guessed number...or unitl you give 0 as input....every time it will return difeerent value og g....just modify it and check it out...

It works perfectly in my pc......

Member Avatar
subramanya.vl
Junior Poster in Training
82 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 3 [?]
 
0
 

I obeserve that the code does not find the number under guess.
if user is trying to use binary search for finding the number under guess then this is not the correct implementation of binary search.
I dont even find even if recursion is being used here.
In any case the while loop will iterate only once.
I dont know how the value being printed is related to number under guess.
I dont even see the guessed number is stored some where in the program to be compared later in the program.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article