If statement always giving the same answer

前端 未结 7 538
-上瘾入骨i
-上瘾入骨i 2020-12-12 03:26
import java.util.Scanner;

class Practice {

public static void main(String args[]) {

    System.out.println(\"Enter the number of treats you have:\");
    Scanner          


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 04:10

    In java, the '=' operator assigns a value to a variable. In this case,

    if (enoughTreats = true)
    

    assigns the value 'true' to 'enoughTreats' and then checks if 'enoughTreats' is true (which it always will be).

    Instead, you want to put

    if (enoughTreats == true)
    

    so that it will check if enoughTreats is true or false.

提交回复
热议问题