简体   繁体   English

如何在方法中正确使用布尔值并打印结果

[英]How to properly use a boolean in a method and print result

I am working on a program and I am almost finished with it. 我正在开发一个程序,几乎完成了。 The only thing I can't figure out is how to as the user for ay or n and then display the boolean in the program to be true or false. 我唯一不知道的是如何以ay或n为用户,然后在程序中显示布尔值是true还是false。 It keeps giving me an error. 它一直给我一个错误。 Here is the code that I am working with. 这是我正在使用的代码。 I just want it to display true or false when the last print statement in the driver executes. 我只希望它在驱动程序中的最后一个打印语句执行时显示true或false。 Thank you 谢谢

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 + "]";
}

And then the driver 然后是司机

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

}

} }

You can only use nextBoolean() when the token is literally "true" or "false" . 仅当标记字面意义上为“ true”或“ false”时,才可以使用nextBoolean() Otherwise, it'll throw an InputMismatchException. 否则,它将引发InputMismatchException。 The answers "y" and "n" don't count. 答案“ y”和“ n”不计算在内。 You either need to prompt for the literal values "true" or "false" or else read the y/n response as a String and convert to a boolean yourself, like: 您要么需要提示输入文字值“ true”或“ false”,要么将y / n响应读取为String并自己转换为布尔值,例如:

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

Update: I see now that you already have the "if answer == 'y'" logic in your setMailingList() method. 更新:我现在看到您在setMailingList()方法中已经具有“ if answer =='y'”逻辑。 However, you're trying to pass a boolean to that method and then change the value of that boolean based on the value of the answ field, which is never assigned a value. 但是,您尝试将布尔值传递给该方法,然后根据answ字段的值更改该布尔值,该字段从未分配值。 You need to work on that part of the code before it'll work, too. 您还需要先处理代码的这一部分,然后才能起作用。 I'd say you're correct to be passing a boolean to that method, so the "y or n" logic should move out of it to your main method. 我会说您将布尔值传递给该方法是正确的,因此“ y或n”逻辑应该移出它到您的main方法。

y and n are not valid Booleans . yn是无效的Booleans You would need to input true or false for it to work. 您需要输入truefalse才能使其正常工作。

Your best bet would be to do something like this: 最好的选择是执行以下操作:

if (kb.nextLine().equals("y")) {
    customer1.setMailingList(true);
} else {
    customer1.setMailingList(false);
}

you are asking user to enter char input 'y' or 'n', but your code is actually expecting 'true' or 'false' 您正在要求用户输入字符输入“ y”或“ n”,但是您的代码实际上期望的是“ true”或“ false”

customer1.setMailingList(kb.nextBoolean()); customer1.setMailingList(kb.nextBoolean());

you may want to change it to take string input from user and check 您可能需要更改它以接受用户的字符串输入并检查

System.out.println("Does customer wish to be on mailing list? \n" + 
    "Enter 'y' for yes and 'n' for no: ");
    customer1.setMailingList(kb.nextBoolean());

Here you're asking user to enter a character y or n . 在这里,您要求用户输入字符yn When you're calling nextBoolean , you're requesting a boolean input. 调用nextBoolean ,您需要一个布尔输入。 Neither y or n is a boolean. yn都不是布尔值。 A boolean is either true or false . booleantrue还是false Instead, prompt the user to enter true or false instead of y or n . 而是提示用户输入truefalse而不是yn Or explicitly make y = true and n = false; 或者明确地使y = truen = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM