Java,不知道出了什么问题?非常基本的代码
我试图在一行代码中打印出整个“f”变量。。f显然代表女性,但当我使用我的代码方式时,它会显示错误吗 对不起,我是初学者Java,不知道出了什么问题?非常基本的代码,java,Java,我试图在一行代码中打印出整个“f”变量。。f显然代表女性,但当我使用我的代码方式时,它会显示错误吗 对不起,我是初学者 import java.util.*; class university { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); Person2 mPerson, fPerson = null;`
import java.util.*;
class university {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Person2 mPerson, fPerson = null;`
String fFirstName, fSurname, mFirstName, mSurname;
int fAge, mAge;
System.out.println("Please enter the firstname of your favourite female author");
fFirstName = scanner.nextLine();
System.out.println("Please enter her second name");
fSurname = scanner.nextLine();
System.out.println("Please enter her age");
fAge = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter the firstname of your favourite female author");
mFirstName = scanner.nextLine();
System.out.println("Please enter her second name");
mSurname = scanner.nextLine();
System.out.println("Please enter her age");
mAge = scanner.nextInt();
fPerson = new Person2(fFirstName, fSurname, fAge);
System.out.print(fPerson);
}
}
您的问题是,您没有阅读任何关于Java的教程,您试图盲目地解决即将出现的问题 你面临的问题是,你没有声明一个地方,计算机可以在那里获得你期望他拥有的信息。编码不是一件神奇的事情,你得到了你所写的东西 您缺少的是
Person2
类中的toString()
方法的实现
@Override
public String toString() {
return fFirstName
}
不要浪费更多的时间来解决平台问题,试着阅读一篇文章 为Person类实现toString()方法,仅打印对象不会打印值
public String toString() {
//format the way you want the values to be printed
return fFirstName + " " + fSurname + " " + fAge;
}
什么错误?
fPerson2
是如何定义的?没有“fPerson2”1)要更快地获得更好的帮助,请发布一个。2) 总是复制/粘贴错误和异常输出。哎呀,对不起。person2
是如何定义的?如果你不花时间格式化你的代码,事情就会被误读。我搜索了一下,猜怎么着?类名是Person2,你需要添加@Override