gpt4 book ai didi

Java Eclipse : My code's output is skipping a line and I don't know why

转载 作者:行者123 更新时间:2023-12-02 11:41:55 25 4
gpt4 key购买 nike

标题说明了一切。这是一个基本的 Customer 类,用户输入他们的姓名/年龄/街道地址/城市/州/邮政编码,然后程序格式化输入并将其返回给用户。当我运行这个类(class)时,它会跳过“街道地址”并直接进入“城市”,因此我无法让它让我输入我的街道地址。

我在此线程中查看了一个相当相似的问题:Java is skipping a line (Strings in a Scanner ??)但是我没有从中得到任何可以帮助我解决这个问题的东西。我确信它非常基础,但我只是无法理解它并且今天没有太多时间来处理这个问题,所以任何提示/帮助都将不胜感激!

public class Customer {
String name;
String streetAddress;
String city;
String state;
String zip;
int age;

//default constructor
public Customer() {
name = "Unknown";
streetAddress = "Unknown";
city = "Unknown";
state = "Unknown";
zip = "Unknown";
age = 0;
}

//constructor to accept values for the above attributes
public Customer(String n, String sAdd, String c, String st8, String z, int a) {
name = n;
streetAddress = sAdd;
city = c;
state = st8;
zip = z;
age = a;
}

//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public String displayAddress() { //returns a string with the complete formatted address
String showAddress;
showAddress = ("\nStreet Address: " + streetAddress + "\nCity: " + city + "\nState: " + state + "\nZip Code: " + zip);
return showAddress;
}

public String displayAddressLabel() { //returns a string with the customers name/age
String nameAgeAddress;
nameAgeAddress = ("Name: " + name + "\nAge: " + age);
return nameAgeAddress;
}



//main method
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

//creating an object of the Customer class
Customer actualCustomer = new Customer();

//getting info for displayAddressLabel() and displayAddress
System.out.println("Enter your name: ");
actualCustomer.setName(scan.nextLine());

System.out.println("Enter your age: ");
actualCustomer.setAge(scan.nextInt());





//issue is here
System.out.println("Enter your street address: ");
actualCustomer.setStreetAddress(scan.nextLine());





System.out.println("Enter the city you live in: ");
actualCustomer.setCity(scan.nextLine());

System.out.println("Enter the state you live in: ");
actualCustomer.setState(scan.nextLine());

System.out.println("Enter your zip code: ");
actualCustomer.setZip(scan.nextLine());

System.out.println(actualCustomer.displayAddressLabel());
System.out.println(actualCustomer.displayAddress());


}
}

最佳答案

此行之后:

actualCustomer.setAge(scan.nextInt());

你应该打电话:

scan.nextLine();

因为在 scan.nextInt() 之后还有一个新行字符需要读取(在输入 int 后,您按 Enter 确认输入,并且您丢失了从扫描仪中读取)。而不是写这两行:

actualCustomer.setAge(scan.nextInt());
scan.nextLine();

您可能想将其更改为:

actualCustomer.setAge(Integer.parseInt(scan.nextLine()));

它将摆脱换行符。

关于Java Eclipse : My code's output is skipping a line and I don't know why,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com