2

我正在开发一个程序,我几乎完成了它。我唯一不知道的是如何作为用户输入 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

}

}

4

4 回答 4

1

您只能nextBoolean()令牌字面上为 "true" 或 "false"时使用。否则,它会抛出 InputMismatchException。答案“y”和“n”不算数。您需要提示输入文字值“true”或“false”,或者将 y/n 响应作为字符串读取并自己转换为布尔值,例如:

String yOrN = kb.next();
if ("y".equals(yOrN)) customer1.setMailingList(true);

更新:我现在看到您的setMailingList()方法中已经有了“if answer == 'y'”逻辑。但是,您尝试将布尔值传递给该方法,然后根据字段的值更改该布尔值的值,该answ字段从未分配过值。您也需要先处理这部分代码,然后它才能工作。我会说您将布尔值传递给该方法是正确的,因此“y 或 n”逻辑应该移出它到您的主要方法。

于 2013-10-22T03:18:58.560 回答
1

y并且n无效Booleans。您需要输入truefalse使其工作。

你最好的选择是做这样的事情:

if (kb.nextLine().equals("y")) {
    customer1.setMailingList(true);
} else {
    customer1.setMailingList(false);
}
于 2013-10-22T03:20:36.203 回答
1

您要求用户输入字符输入“y”或“n”,但您的代码实际上期望“真”或“假”

customer1.setMailingList(kb.nextBoolean());

您可能希望将其更改为从用户那里获取字符串输入并检查

于 2013-10-22T03:33:50.853 回答
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());

在这里,您要求用户输入字符yn. 当你打电话nextBoolean时,你要求一个布尔输入。要么y要么n不是布尔值。Aboolean要么true要么false。相反,提示用户输入trueorfalse而不是yor n。或者显式地 make y= trueand n= false;

于 2013-10-22T03:19:10.713 回答