Pat6578
Pat6578

Reputation: 1

Compilation Error in a very silly program

import java.util.Scanner;

public class KekOrCringe {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String userGuess = "";
        boolean Continue = true;
        boolean ProperResponse = true;
        boolean IsCorrect = true;
        boolean YesNo = true;

            while (Continue)
            {
                int secretAnswer = (int)(Math.random() * 2 + 1);
                kekOrCringe(secretAnswer);
                while (!IsCorrect)
                {
                    System.out.println("Kek or Cringe?");
                    ProperResponse = false;
                    while (!ProperResponse) {
                        userGuess = scan.nextLine();
                        if (userGuess != "Kek") 
                            System.out.println("Your entry is invalid, please try again!");
                        else if (userGuess != "Cringe")
                            System.out.println("Your entry is invalid, please try again!");
                        else 
                            ProperResponse = true;
                    }
                    for (int guessCount = 0; guessCount < 1; guessCount++) {
                        if (userGuess = "Cringe" && userGuess != secretAnswer) {
                            System.out.println("It's KeK!"); 
                            guessCount++; }
                        else if (userGuess = "Kek" && userGuess != secretAnswer) { 
                            System.out.println("It's CrInGe!"); 
                            guessCount++; }
                        else
                            System.out.println("Mr. Morgan, you got it right my boy!");
                            IsCorrect = true;
                        }
                    }
                }
                YesNo = false;
                while(!YesNo) {
                    System.out.println("Would you like to play again? Yes/No");
                    String answer = scan.nextLine();
                    if (answer.equals("No")) {
                        Continue = false;
                        YesNo = true;
                        System.out.println("Fine. You were Cringe anyway!");
                    }
                    else if (answer.equals("Yes")) {
                        YesNo = true;
                        Continue = true;
                        IsCorrect = false;
                    }
                }
            }
        

                            
    public static String kekOrCringe(int secretAnswer) {
        if (secretAnswer = 1) { return "Kek";}
        if (secretAnswer = 2) { return "Cringe";}
    }
} 

Probably an overly complex way to do something unnecessary, but this is my first year in college learning to code, and I was asked to give this a try. I think it's funny, and will probably be funnier if it work, along with being good practice. I'm having trouble converting the int secretAnswer to a returned string, and then comparing the userGuess to the return type. Getting compilation errors on line 32 and 35. Any tips would be appreciated.

P.S. I realize it's silly. Trying to use this silly code as a learning opportunity.

Upvotes: 0

Views: 114

Answers (3)

sn-
sn-

Reputation: 488

You are trying to compare int to string which is wrong

userGuess != secretAnswer

Also, instead of comparing you are assigning values inside if condition.

    if (secretAnswer = 1) { return "Kek";}
    if (secretAnswer = 2) { return "Cringe";}

It should be:

    if (secretAnswer == 1) { return "Kek";}
    if (secretAnswer == 2) { return "Cringe";}

Upvotes: 1

harundemir918
harundemir918

Reputation: 432

I can't add a comment so I am writing here.

userGuess is String but secretAnswer is int, and you are trying to check if they are equal (userGuess != secretAnswer).

You can use a new variable like secretGuess, assign kekOrCringe(secretAnswer) to secretGuess and check if userGuess is equal to secretGuess.

Like this:

String secretGuess = kekOrCringe(secretAnswer);
if (userGuess != secretGuess) {
  //...
}

Upvotes: 1

R4yY
R4yY

Reputation: 132

Im guessing line 32 and 35 are the two ifs. userGuess != secretAnswer doesn't work since one is a String, the other an Integer. Your static method kekOrCringe(secretAnswer); returns the String you want, you just need to save it in a variable and then compare it to the userGuess.

Also please use lowercase variable names.

Upvotes: 1

Related Questions