简体   繁体   中英

I'm getting an error with <= saying that it's an invalid AssignmentOperator + my code looks wack I think

So I got stuck with a whole lot of this:

package test;

import javax.swing.*;
import java.util.Random;
import java.util.Scanner;

public class GuessGame {
public static void main(String[] args) 
{
    Scanner keys = new Scanner(System.in);
    System.out.println("Hello! Would you like to play?");
    String choice = keys.next();
    for (choice.equals("y");  choice.equals("yes");)
    {
        System.out.println("Awesome!");
        choice = "";

        Random bill = new Random();
        int j;
        j = bill.nextInt(50);

        System.out.println("Guess what the number I'm thinking is ");
            int number;
            number = keys.nextInt();
                for (number <= (j + 10); number >= (j - 10);)
                    {
                        System.out.println("Warm!");
                        number = 0;
                        number = keys.nextInt();
                    }
                for (number = (j + 5); number == (j - 10);)
                    {
                        System.out.println("Hot!!!");
                        number = 0;
                        number = keys.nextInt();
                    }


    }
    for (choice.equals("n"); choice.equals("no");)
    {
        System.out.println("okay");
        keys.close();
        System.exit(0);
    }
}
}

On the line with " for (number <= (j + 10); number >= (j - 10);)", I'm getting an error on the "<=", and I've got no idea how to make amends on it. As well, I'm not sure if I should be using the for statement for this. Please help me understand my mistake, and if there is a better alternate than for.

Thank you!

That is because the first parameter of the for statement is used for initialization of the variable, thus giving you an error.

documentation:

for (initialization; termination;increment) {
   statement(s)
}

problem:

for (number <= (j + 10); number >= (j - 10);)

solution:

use an if statement if you are going to check both variable

  if(number <= (j + 10) &&  number >= (j - 10))

Try replacing each of your for loops with if statements :

if (choice.equals("y") || choice.equals("yes") { 

...

    if (number <= (j + 5) && number >= (j - 5)) { ... } // I assume this is what you meant here
    else if (number <= (j + 10) && number >= (j - 10)) { ... }

...

}
else {
    System.out.println("okay");
    keys.close();
    System.exit(0);
}

Note that you want your most restrictive if statement first, so I changed the order for the part where you are checking if it is within certain ranges. This way it checks whether you are "hot" before checking whether you are "warm" (since hot is contained in warm).

Also note that the else if statement for checking "warm" means that if you are hot, it will not bother checking if you are warm and updating your output twice.

I also replaced the "no" case with just an else statement, which will catch any answer that is not "y" or "yes".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM