布尔值不会改变

时间:2013-05-01 01:49:17

标签: java boolean

我正在开发一个小程序,基本上只是表明我知道如何使用超类。除了我的布尔值,一切都很好。它应该要求使用他们想要注册的邮件列表。如果输入“yes”,则布尔值应为true,如果输入“no”则应为false。我的问题是,即使我输入“no”,它仍然会注册为“yes”,使布尔值为true。请帮忙? (我希望这有点似乎有意义。)

 import java.util.Scanner;

public class CustomerDemo 
{
    public static void main(String[] args) 
    {
        String name;
        String address;
        String telephone;
        int customerNumber;
        String input;
        boolean mail = false;

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter your name: ");
        name = keyboard.nextLine();

        System.out.print("Enter your address: ");
        address = keyboard.nextLine();

        System.out.print("Enter your phone number: ");
        telephone = keyboard.nextLine(); 

        System.out.print("Enter your customer number: ");
        customerNumber = keyboard.nextInt();

        keyboard.nextLine();

        System.out.print("Do you want to be on the mailing list (Yes or No): ");
        input = keyboard.nextLine();

        if (input.equalsIgnoreCase("yes")) {
            mail = true;
        }

        Customer cust = new Customer(name, address, telephone, customerNumber, mail);

        System.out.println("Hello " + cust.getName() + "!");

        System.out.println("You are customer " + cust.getCustomerNumber() + ".");


        if(mail = false){
            System.out.println("You are not on the mailing list. ");
        }

        else {
            System.out.println("You are on the mailing list. "); 
        }
    }
}

3 个答案:

答案 0 :(得分:4)

你没有进行比较,你正在做任务......

if(mail = false)等同于if (false),因为false已分配回mail ...

请改为if(!mail),这相当于if (!true)(如果你真的真的想做,你也可以做if(mail == false)

答案 1 :(得分:2)

看看这一行 if(mail = false)这应该是if(!mail)

答案 2 :(得分:1)

您的阅读代码没有任何问题。错误是您尝试打印的地方。条件if(mail = false)不正确。应为if(mail==false)