-4

I'm working on a hangman assignment, but I am stuck in one part of it;

A player (user) is playing a game of words with the computer. The users plays the game couple of times. When they stop the overall score of the user is displayed . Each play the user sees a menu.

       String S;

       int s2;
       System.out.println("WELCOME TO WORD GAME V.12.01.16");
       System.out.println("****MENU****");
       System.out.println("FIRST YOUR NUMBER AND PRESS 'ENTER' ");
       System.out.printf("0:Stop \n 1:Name \n 2:City \n 3:Animal \n 4:Thing ");
       s=input.nextInt();
       if(s==0) {
           System.out.println("GOODBYE");
        }


        for(int i=0;i<category.length;i++) {
            if(s==i){

                System.out.println("GUESS FOR 1,CHARACTER FOR 2");
                s2=input.nextInt();
                if(s2==1){
                System.out.println("ENTER YOUR GUESS");
                S=input.nextLine();
                boolean result=correct(S);
                    if(result==true) {
                        System.out.println("Congrats");


                    }


                }


            }
        }


    }

        public static boolean correct(String X) {

            for(int i=1;i<5;i++){
              for(int j=0;j<10;j++){
                if (category[i][j].equals(X)) {

                }
              }
            }
            return true;




            }


   }
  • Your if statement is wrong. `if ( logic) { print(); }else { print(); }` Currently yours is `if(logic);{ print(); } { print(); }` – Eric G Jan 04 '16 at 19:52
  • You have a semi-colon (`;`) at the end of your if clause `if (category[s][j]==S);` making the whole if clause essentially useless. – Kayaman Jan 04 '16 at 19:52
  • 2
    That's also [the wrong way to compare `String`s in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – resueman Jan 04 '16 at 19:53

1 Answers1

1

Your if statement if(category[s][j]==S); is done the wrong way

Also noted by @resueman in his comment that comparing strings was done the wrong way as well. You want to use .equals() to compare strings.

S=input.nextLine();
System.out.println("Please Enter : ")
for(int j=0;j<11;j++){
    if (category[s][j].equals(S)){
        System.out.println("CONGRATS");
    }else {
        System.out.println("FAIL");
    }
}

Edit - New code from OPs edit

public static void main(String args[]) {
    String string;
    Scanner input = new Scanner(System.in);
    int s, s2;

    System.out.println("WELCOME TO WORD GAME V.12.01.16");
    System.out.println(
            "****MENU****");
    System.out.println(
            "FIRST YOUR NUMBER AND PRESS 'ENTER' ");
    System.out.printf(
            "0:Stop \n 1:Name \n 2:City \n 3:Animal \n 4:Thing ");
    s = input.nextInt();
    if (s == 0) {
        System.out.println("GOODBYE");
    }

    for (int i = 0; i < category.length; i++) {
        if (s == i) {

            System.out.println("GUESS FOR 1,CHARACTER FOR 2");
            s2 = input.nextInt();
            if (s2 == 1) {
                System.out.println("ENTER YOUR GUESS");
                string = input.nextLine();
                // Dont need to assign the boolean to a value
                // if(booleanVariable == true) is the same thing as writing if(boolean)
                // If it is true, it will execute, if false it will not
                if (correct(string)) {
                    System.out.println("Congrats");
                }

            } // if(s2==1)

        }// if (s == i)
    }// for
}// run

public static boolean correct(String X) {

    for (int i = 1; i < 5; i++) {
        for (int j = 0; j < 10; j++) {
            if (category[i][j].equals(X)) {
                // the guess was right
                return true;
            }
        }
    }
    // Nothing equaled the guess
    return false;

}
Community
  • 1
  • 1
Eric G
  • 928
  • 1
  • 9
  • 29
  • yes but still problem exists because user cannot choose,just print both situations 'FAIL' and 'CONGRATS' – Çağatay Çevik Jan 04 '16 at 21:17
  • @ÇağatayÇevik i dont understand what you mean. You still need to add your own code for the `else if(s2 == 2)` condition – Eric G Jan 04 '16 at 21:41
  • @ÇağatayÇevik you still havent asked a question or clarified what your problem is. I've fixed up your code to show you some better usages of variables and things. – Eric G Jan 05 '16 at 14:05