We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,924 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Guess a number 1-1000

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.

6
Contributors
6
Replies
1 Week
Discussion Span
5 Months Ago
Last Updated
9
Views
joe.crook.9
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

jalpesh_007
Posting Whiz
336 posts since Sep 2010
Reputation Points: 4
Solved Threads: 36
Skill Endorsements: 4

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.

amrita111
Junior Poster in Training
59 posts since Aug 2010
Reputation Points: 14
Solved Threads: 6
Skill Endorsements: 1

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

jalpesh_007
Posting Whiz
336 posts since Sep 2010
Reputation Points: 4
Solved Threads: 36
Skill Endorsements: 4

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.

subramanya.vl
Junior Poster in Training
81 posts since Oct 2012
Reputation Points: 0
Solved Threads: 10
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1111 seconds using 2.71MB