因此,在我的课堂上,我们正在制作简单的程序,并介绍继承。 我已经使一切正常工作,但是我无法获得这一点输出正常工作。 这是我要输入内容的时间,但是它跳过了我的输入内容之一,转到了第二个输入的请求。 当我不需要其他所有注释时,它不会跳过第一个输入。 我不明白为什么会这样,如果有人可以帮助我解密为什么会非常感谢。 在此先感谢您,以下是我的两节课,随后是测试人员:

人类:

    public class Person{
    private String name, address, phoneNumber;
    public Person(){
        name="none";
        address="none";
        phoneNumber="none";
    }
    public Person(String inName, String inAddress, String inPhoneNumber){
        name=inName;
        address=inAddress;
        phoneNumber=inPhoneNumber;
    }
    public String getName(){
        return name;
    }
    public void setName(String newName){
        name = newName;
    }
    public String getAddress(){
        return address;
    }
    public void setAddress(String newAddress){
        address = newAddress;
    }
    public String getPhoneNumber(){
        return phoneNumber;
    }
    public void setPhoneNumber(String newPhoneNumber){
        phoneNumber = newPhoneNumber;
    }
    public String toString(){
        return String.format("Name: " + name + "\nAddress: " + address + "\nPhone Number: " + phoneNumber);
    }
    public void clearPerson(){
        name="none";
        address="none";
        phoneNumber="none";
    }
}

客户类别:

    public class Customer extends Person{
    private int customerNumber;
    private boolean mailingList;
    public Customer(){
        customerNumber=0;
        mailingList=false;
    }
    public Customer(int inCustomerNumber, boolean inMailingList){
        customerNumber=inCustomerNumber;
        mailingList=inMailingList;
    }
    public int getCustomerNumber(){
        return customerNumber;
    }
    public void setCustomerNumber(int newCustomerNumber){
        customerNumber=newCustomerNumber;
    }
    public boolean getMailingList(){
        return mailingList;
    }
    public void setMailingList(boolean newMailingList){
        mailingList=newMailingList;
    }
    public String toString(){
        return "Customer Number: "+customerNumber+"\nMailing List: "+mailingList+"\n";
    }
    public void clearCustomer(){
        customerNumber=0;
        mailingList=false;
    }
}

测试人员类别:

import java.util.*;
public class CustomerPersonTester{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        Person testP1 = new Person();
        Customer testC1 = new Customer();
        System.out.println("Empty paramaters constructed on Person and Customer. Results: ");
        System.out.println(testP1.toString());
        System.out.println(testC1.toString());
        /*System.out.println(testP1.getName());
        System.out.println(testP1.getAddress());
        System.out.println(testP1.getPhoneNumber());
        System.out.println(testC1.getCustomerNumber());
        System.out.println(testC1.getMailingList());*/
        System.out.println("Expected: none x3, 0, false.\n");
        System.out.print("Enter a new name: ");
        String newName = in.nextLine();
        System.out.print("Enter a new address: ");
        String newAddress = in.nextLine();
        System.out.print("Enter a new phone number: ");
        String newPhoneNumber = in.nextLine();
        System.out.print("Enter a new customer number: ");
        int newCustomerNumber = in.nextInt();
        System.out.print("Enter a new true/false for mailing list: ");
        boolean newMailingList = in.nextBoolean();
        testP1.setName(newName);
        testP1.setAddress(newAddress);
        testP1.setPhoneNumber(newPhoneNumber);
        testC1.setCustomerNumber(newCustomerNumber);
        testC1.setMailingList(newMailingList);
        System.out.println(testP1.toString());
        System.out.println(testC1.toString());
        /*System.out.println(testP1.getName());
        System.out.println(testP1.getAddress());
        System.out.println(testP1.getPhoneNumber());
        System.out.println(testC1.getCustomerNumber());
        System.out.println(testC1.getMailingList());*/
        System.out.println("Expected: given name/address/phone number/customer number/boolean.\n");
        testP1.clearPerson();
        testC1.clearCustomer();
        System.out.println("\nTest 1 Complete. Values are reset. Please continue.\n");
        System.out.print("Enter a name: ");
        String inName = in.nextLine();
        System.out.print("Enter an address: ");
        String inAddress = in.nextLine();
        System.out.print("Enter a phone number: ");
        String inPhoneNumber = in.nextLine();
        System.out.print("Enter a customer number: ");
        int inCustomerNumber = in.nextInt();
        System.out.print("Enter true/false for mailing list: ");
        boolean inMailingList = in.nextBoolean();
        Person testP2 = new Person(inName, inAddress, inPhoneNumber);
        Customer testC2 = new Customer(inCustomerNumber, inMailingList);
        System.out.println(testP2.toString());
        System.out.println(testC2.toString());
        /*System.out.println(testP2.getName());
        System.out.println(testP2.getAddress());
        System.out.println(testP2.getPhoneNumber());
        System.out.println(testC2.getCustomerNumber());
        System.out.println(testC2.getMailingList());*/
        System.out.println("Expected: given name/address/phone number/customer number/boolean.\n");
        System.out.println("Program complete. Terminating...");
        in.close();
    }
}

我的输出一直像这样:

Empty paramaters constructed on Person and Customer. Results: 
Name: none
Address: none
Phone Number: none
Customer Number: 0
Mailing List: false

Expected: none x3, 0, false.

Enter a new name: Bob
Enter a new address: 123 Happy Lane
Enter a new phone number: 123-456-7890
Enter a new customer number: 12
Enter a new true/false for mailing list: true
Name: Bob
Address: 123 Happy Lane
Phone Number: 123-456-7890
Customer Number: 12
Mailing List: true

Expected: given name/address/phone number/customer number/boolean.


Test 1 Complete. Values are reset. Please continue.

Enter a name: Enter an address: Problem starts here...

===============>>#1 票数:1 已采纳

nextBoolean()的调用不会占用您的truefalse输入之后的换行符。 结果,当您下次调用nextLine()时,此换行符仍在输入缓冲区中,该行立即消耗掉剩下的换行符,然后继续前进。

要获得预期的行为,只需在调用nextBoolean()之后再添加一个对nextLine()调用即可。 您还应该注意,如果nextInt()也是序列中的最后一个调用,也会发生这种情况。

您可能还想考虑将代码的某些部分放入某种形式的循环中(也许是while循环?),以便您不会有很大一部分重复的代码。

  ask by MikeN translate from so

未解决问题?本站智能推荐: