所以在我的班上我们正在制作简单的程序,我们正在引入继承。我已经让一切正常工作,但我无法让这一点输出正常工作。当我要求输入时,它会跳过我的一个输入并转到第二个请求。当我在不需要之前评论所有其他事情时,它不会跳过第一个输入。我不明白为什么会这样,如果有人可以帮我解释为什么会受到高度赞赏。在此先感谢,这是我的两个课程,然后是测试人员:
人员类:
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...
答案 0 :(得分:1)
对nextBoolean()
的调用不会使用true
或false
输入后面的换行符。因此,当您下次调用nextLine()
时,此换行符仍然在输入缓冲区中,它会立即使用剩余的换行符然后继续。
要获得预期的行为,只需在致电nextLine()
后再向nextBoolean()
添加另一个电话即可。您还应该注意,如果nextInt()
也是序列中的最后一次调用,则会发生这种情况。
您可能还想考虑将部分代码放入某种形式的循环(也许是一个while
循环?),这样就不会有大量的重复代码。