0

我正在开发一个小程序,基本上只是表明我知道如何使用超类。除了我的布尔值外,一切都很好。它应该询问使用他们想注册的邮件列表。如果输入“是”,则布尔值应为真,如果输入“否”,则应为假。我的问题是,即使我输入“否”,它仍然注册为“是”,使布尔值变为真。请帮忙?(我希望这有点道理。)

 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. "); 
        }
    }
}
4

3 回答 3

4

你不是在做比较,你是在做作业……

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

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

于 2013-05-01T01:51:19.903 回答
2

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

于 2013-05-01T01:56:28.637 回答
1

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

于 2013-05-01T01:55:24.577 回答