我正在开发一个程序,我几乎完成了它。我唯一不知道的是如何作为用户输入 ay 或 n,然后在程序中显示布尔值是真还是假。它一直给我一个错误。这是我正在使用的代码。我只是希望它在驱动程序中的最后一个打印语句执行时显示真或假。谢谢
public class Customer extends Person {
protected int customerNum;
protected boolean mailingList;
protected char answ;
public Customer() {
super();
// TODO Auto-generated constructor stub
}
public Customer(String name, String address, double telephone,
int customerNum, boolean mailingList) {
super(name, address, telephone);
this.customerNum = customerNum;
this.mailingList = mailingList;
}
/**
* @return the customerNum
*/
public int getCustomerNum() {
return customerNum;
}
/**
* @param customerNum the customerNum to set
*/
public void setCustomerNum(int customerNum) {
this.customerNum = customerNum;
}
/**
* @return the mailingList
*/
public boolean isMailingList() {
return mailingList;
}
/**
* @param mailingList the mailingList to set
*/
public void setMailingList(boolean mailingList) {
try {
if(answ == 'y'){
mailingList = true;
}
else if(answ == 'n')
mailingList = false;
this.mailingList = mailingList;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Customer [customerNum=" + customerNum + ", mailingList="
+ mailingList + "]";
}
然后司机
import java.util.Scanner;
public class CustomerDriver extends PreferredCustomer {
/**
* @param args
*/
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
PreferredCustomer customer1 = new PreferredCustomer();
System.out.println("Enter Customer Name: ");
customer1.setName(kb.nextLine());
System.out.println("Enter Customer Address: ");
customer1.setAddress(kb.nextLine());
System.out.println("Enter Telephone Number: ");
customer1.setTelephone(kb.nextDouble());
System.out.println("Enter the Customer Number: ");
customer1.setCustomerNum(kb.nextInt());
System.out.println("Does customer wish to be on mailing list? \n" +
"Enter 'y' for yes and 'n' for no: ");
customer1.setMailingList(kb.nextBoolean());
System.out.println("Enter amount of Customer's Purchase: ");
customer1.setPurchases(kb.nextDouble());
System.out.println("Customer's Discount is as follows: " + customer1.getDiscountLevel() + "\n");
System.out.println("Customers Name: " + customer1.getName() + "\nCustomers Address: " + customer1.getAddress() + "\nCustomers Phone" + customer1.getTelephone() +
"\nCustomer Number: " + customer1.getCustomerNum() + "\nMailing List Preferrence: " + customer1.isMailingList() + "\nCustomer's Purchase Amount " +
customer1.getPurchases() + "\nCustomers Discount Rate (if any) :" + customer1.getDiscountLevel());
// TODO Auto-generated method stub
}
}