2013-05-01 110 views
0

我正在研究一个基本上只显示我知道如何使用超类的小程序。除了我的布尔值之外,一切都很好。它应该要求使用他们想要注册的邮件列表。如果输入“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. "); 
     } 
    } 
} 
+0

如果(邮件= FALSE)是一个错误的表达。它应该是(mail == false),在改变这个之后试试 – 2013-05-01 01:52:29

回答

4

你不是做一个对比,你正在做的作业...

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

待办事项if(!mail)此相反,这相当于if (!true)(或者你也可以做if(mail == false)如果你真的想)

+0

这样做。谢谢!对不起,这样一个简单的问题! – 2013-05-01 02:03:01

+0

很高兴能有一些帮助:D – MadProgrammer 2013-05-01 02:27:43

1

您的阅读代码没有错误。错误是您尝试打印的位置。条件if(mail = false)是不正确。应该if(mail==false)

+0

这样做。谢谢!对不起,这样一个简单的问题! – 2013-05-01 02:04:45

2

看看这个行 如果(邮件= FALSE),这应该是如果(!邮件)

+0

这样做。谢谢!对不起,这样一个简单的问题! – 2013-05-01 02:04:12