1.11M Members

Guess a number 1-1000

 
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.

 
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

 
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.

 
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.

 
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.

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

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article