2013-05-01 147 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