我正在制作一个程序,我差不多完成了它。我唯一想不通的是如何作为y或n的用户,然后在程序中显示布尔值为true或false。它一直给我一个错误。这是我正在使用的代码。我只是希望它在驱动程序中的最后一个print语句执行时显示true或false。 谢谢
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
}
}
答案 0 :(得分:2)
您只能在token is literally "true" or "false"时使用nextBoolean()
。否则,它将抛出一个InputMismatchException。答案“y”和“n”不计算在内。你需要提示文字值“true”或“false”或者read the y/n response as a String并自己转换为布尔值,如:
String yOrN = kb.next();
if ("y".equals(yOrN)) customer1.setMailingList(true);
更新:我现在看到您的setMailingList()
方法中已有“if answer =='y'”逻辑。但是,您尝试将布尔值传递给该方法,然后根据answ
字段的值更改该布尔值,该字段永远不会赋值。在工作之前,您还需要处理代码的这一部分。我会说你将布尔值传递给该方法是正确的,所以“y或n”逻辑应该从它移出到你的main方法。
答案 1 :(得分:2)
y
和n
无效Booleans
。您需要输入true
或false
才能生效。
你最好的选择是做这样的事情:
if (kb.nextLine().equals("y")) {
customer1.setMailingList(true);
} else {
customer1.setMailingList(false);
}
答案 2 :(得分:1)
您要求用户输入char输入'y'或'n',但您的代码实际上是期望'true'或'false'
customer1.setMailingList(kb.nextBoolean());
你可能想要改变它以从用户那里获取字符串输入并检查
答案 3 :(得分:0)
System.out.println("Does customer wish to be on mailing list? \n" +
"Enter 'y' for yes and 'n' for no: ");
customer1.setMailingList(kb.nextBoolean());
在这里,您要求用户输入字符y
或n
。当你调用nextBoolean
时,你正在请求一个布尔输入。 y
或n
都不是布尔值。 boolean
是true
或false
。相反,提示用户输入true
或false
,而不是y
或n
。或明确指出y
= true
和n
= false;