gpt4 book ai didi

java - 字符串值数据成员被编译器跳过

转载 作者:行者123 更新时间:2023-12-01 18:23:50 26 4
gpt4 key购买 nike

尽管告诉计算机显示,但字符串值并未全部显示,请帮忙

import java.util.Scanner;

class Author
{
private String name;
private String email;
private char gender;

public Author (String name, String email, char gender) {
this.name=name;
this.setEmail(email);
this.gender=gender;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the gender
*/
public char getGender() {
return gender;
}

@Override
public String toString() {
return "Author [name=" + name + ", email=" + email + ", gender=" + gender + "]";
}


}
class Book
{
private String Name;
Author auth;
private double price;
private int qty;

/**
* @param name
* @param auth
* @param price
*/
public Book(String name, Author auth, double price) {
super();
Name = name;
this.auth = auth;
this.price = price;
}

/**
* @param name
* @param auth
* @param price
* @param qty
*/
public Book(String name, Author auth, double price, int qty) {
super();
Name = name;
this.auth = auth;
this.price = price;
this.qty = qty;
}

public String getName() {
return Name;
}


public Author getAuth() {
return auth;
}



public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public int getQty() {
return qty;
}

public void setQty(int qty) {
this.qty = qty;
}

@Override
public String toString() {
return "Book [Name=" + Name + ", auth=" + auth + ", price=" + price + ", qty=" + qty + "]";
}



}
class Student
{
private String Name;
private int roll;
date issueDate;
date returnDate;
/**
* @param name
* @param roll
* @param issueDate``
* @param returnDate
*/
public Student(String name, int roll, date issueDate, date returnDate) {
super();
Name = name;
this.roll = roll;
this.issueDate = issueDate;
this.returnDate = returnDate;
}
public String getName() {
return Name;
}

public int getRoll() {
return roll;
}

public date getIssueDate() {

return issueDate;
}

public date getReturnDate() {
return returnDate;
}
public void setReturnDate(date returnDate) {
this.returnDate = returnDate;

}



}
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
Author ahTeck=null;
System.out.println("How many Book are there in library ?");
int n=sc.nextInt();
Book []ob=new Book[n];
for(int i=0;i<n;++i)
{ System.out.println("Author's name");
String s=sc.nextLine();
sc.nextLine();
System.out.println("Author's Email Id");
String s1=sc.nextLine();
System.out.println("gender:");
char c = sc.next(".").charAt(0);
sc.nextLine();
System.out.println("Book Name:");
String b=sc.nextLine();
System.out.println("Book price:");
double price=sc.nextInt();
System.out.println("Book quantity");
int q=sc.nextInt();
ob[i]=new Book(b, new Author(s, s1, c),price,q);
System.out.println(ob[i]);
}

}

}

问题出现在 https://www.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html

输出如下

Book [Name=g, auth=Author [name=, email=e, gender=m], price=6.0, qty=6]

跳过作者姓名的显示

最佳答案

啊是的。老扫描仪。

在顶部输入整数后,立即调用 sc.nextLine();

int n = sc.nextInt();
sc.nextLine();

实际上,您甚至不必在输入字符串或字符后调用 sc.nextLine()。问题是,当您输入数字时,您也会按回车键。当您使用 nextInt() 时,Scanner 类将其视为另一个 token ,因此数字会正确存储,但回车键 \n 会被视为另一个 token .

因此,当您在输入数字后调用 nextLine() 时,系统会发现 Scanner 对象中已经剩余了一个 token ,因此它将 '\n' 作为其输入。因此,该名称实际上存储了“\n”,它是空的。

关于java - 字符串值数据成员被编译器跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60263200/

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