-4

I'm trying to create a program in which the user thinks of a number between 1 and 10, and the computer repeatedly tries to guess it by guessing random numbers. (It's ok to guess the same number more than once) At the end of the game, the program reports how many guesses it made. For example:

enter image description here

I have written the following code:

import java.util.*;

public class Pick {
    public static void main(String[] args){
        System.out.println("This program has you, the user, choose a number");
        System.out.println("between 1 and 10. Then, I, the computer, will try");
        System.out.println("my best to guess it.");

        Scanner console = new Scanner(System.in);
        Random r = new Random();

        int result = -1;
        int count = 0;

        for (i = 1; i <= number; i++){
            while(result != number){
            result = r.nextInt(10) + 1;
            System.out.println("Is it " + r + "? (y/n)");
            String yn = console.next();
            if(String yn = "y"){
                System.out.println("I got your number of " + result + " in " + i + " guesses.");
            } else if (String yn = "n") {
                count++;

            } else {
                System.out.println("I got your number of " + result + " in " + i + " guesses.");
            }
        }
        System.out.println();
    }
}

I'm confused as to why my program doesn't work? It won't compile and has 7 errors. I think I went wrong in the if/else statements. Should I have used a while loop? Thanks.

compilation errors by text:

Pick.java:20: error: ')' expected
            if(String yn = "y"){
                     ^
Pick.java:20: error: ';' expected
            if(String yn = "y"){
                              ^
Pick.java:22: error: ')' expected
            } else if (String yn = "n") {
                             ^
Pick.java:22: error: ';' expected
            } else if (String yn = "n") {
                                      ^
Pick.java:22: error: 'else' without 'if'
            } else if (String yn = "n") {
              ^
Pick.java:25: error: 'else' without 'if'
            } else {
              ^
Pick.java:31: error: reached end of file while parsing
}
 ^
7 errors
Tiny
  • 27,221
  • 105
  • 339
  • 599
  • so it won't compile? depending on your IDE, you can click on the red error marks and it will give you suggestions – Don Cheadle Oct 17 '14 at 19:10
  • I'm not using an IDE. I want to learn to find the errors myself first. – flyingseacows Oct 17 '14 at 19:11
  • **Won't compile won't get you any help!** Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. [See: How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) –  Oct 17 '14 at 19:11
  • Whenever you ask for help, it's important to list all the errors. Otherwise, you're doing the equivalent of saying "Doctor, it hurts!" while refusing to say where and how it hurts and expecting the doctor to magically just know what you mean. – MarsAtomic Oct 17 '14 at 19:12
  • 3
    @flyingseacows that is what an IDE helps beginners do. You can't walk without crawling. – jgr208 Oct 17 '14 at 19:12
  • You have a *lot* of really fundamental syntax errors here. You need to go back and read about how/where to declare local variable. Also, don't compare `String`s in `Java` with "==" (which won't work), and especially not with "=" (which is the assignment operator). – azurefrog Oct 17 '14 at 19:13
  • There are several problems in your code, what is `number` supposed to be? – Elliott Frisch Oct 17 '14 at 19:13
  • 3
    *I'm not using an IDE. I want to learn to find the errors myself first.* , then find them yourself, why are you asking here with no more information that is requested. –  Oct 17 '14 at 19:14
  • 2
    **Someone close this as a duplicate! [How to compare Strings](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)** –  Oct 17 '14 at 19:15
  • There's at least half a dozen errors in this. Most are compiler errors, but there is also a logical error. The ultimate goal of any Java program is to get the thing to compile, then fix the logical errors. If we're stuck at logical, we're going to be stuck there until those things are fixed. – Compass Oct 17 '14 at 19:15
  • Format right, it is hurting my eyes – jgr208 Oct 17 '14 at 19:16
  • @flyingseacows Please post the compilation errors as text, not as a screenshot. If I want to refer to one of those errors, I need to copy by hand ;) – m0skit0 Oct 17 '14 at 19:21
  • @JarrodRoberson OP has much more problems than just how to compare Strings. – m0skit0 Oct 17 '14 at 19:24
  • @m0skit0 it is not, code and such is like this `hello` not in a screen shot with a blue background – jgr208 Oct 17 '14 at 19:25
  • Remove images and leave only text. I formatted your error text, don't forget to format it ;) – m0skit0 Oct 17 '14 at 19:29

3 Answers3

1

You are trying to use the variable number at for (i = 1; i <= number; i++){ but it doesn't exist. You need to initialize number.

Grice
  • 1,345
  • 11
  • 24
1

For one thing,

if(String yn = "y") {

}

is not what you think it is in Java. String yn = "y" is an assignment statement, not a comparison. To compare strings in Java you want to use String's equals() method.

There's are more syntax issues and obvious compiler errors. I suggest you try out one of the many tutorials online for beginning Java.

Don Cheadle
  • 5,224
  • 5
  • 39
  • 54
1
String yn = console.next();
if(String yn = "y"){
  • You are declaring yn twice. That's not allowed. Just use yn, no need to speficy it's a String again, you already did.
  • You're using assignement operator = instead of comparison operator ==. In any case, Strings in Java are not compared using == but using equals method if(yn.equals("y"))

    for (i = 1; i <= number; i++)

  • I don't see the error message for this, but still number is not declared anywhere.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • Probs because you me and JGrice are answering a hopefully fraught question, a question so fraught we should probably not be answering it – Don Cheadle Oct 17 '14 at 19:16
  • I don't see how the question is wrong. It has small code and the question is legit, it also includes the compilation errors now. – m0skit0 Oct 17 '14 at 19:17
  • 1
    I don't know why I'm getting down voted ))))): This is a legit question, I'm a beginner and I actually tried.... – flyingseacows Oct 17 '14 at 19:20
  • @flyingseacows they were from when it wasn't that good of a question it has improved. But the blue and giant text is annoying please fix. – jgr208 Oct 17 '14 at 19:21
  • I fixed the string yn error, how should I declare number? – flyingseacows Oct 17 '14 at 19:25
  • @flyingseacows Well, welcome to SO xD Don't worry, but please listen to the comments on how to improve your question. Please do not put images when text is enough. – m0skit0 Oct 17 '14 at 19:26
  • I don't know what you want to do with this `number`. If you did give it a more meaningful name, maybe I could guess, but *number* is too generic. Does the computer have a limited number of tries to guess your number? – m0skit0 Oct 17 '14 at 19:27
  • @m0skit0 I want to set number equal to whatever number the user was thinking. So, the number that the computer has printed at random when the user enters "y" meaning that was their number. – flyingseacows Oct 17 '14 at 19:32
  • Your whole logic is wrong, there's no way to *fix* it. Now that you know the compilation errors, I think you should re-think how to do it. Sorry but I won't do it for you :) Just persevere, you'll get it. Good luck! – m0skit0 Oct 17 '14 at 19:45