class Anadromi001
{
public void Anadromi01 (Foititis[] F, short ap01, short i)
{
if (ap01 == 4) System.exit(0);
Anadromi002 obj02 = new Anadromi002();
String onoma, epitheto;
short MesOr;
int AritMit, EtosEis;
short ap02;
boolean flag00, flag02, flag03;
int k, f, l = 0;
k = F.length-1
[...]
case 3:
f = obj02.Anadromi02(F, l);
if (f == -1) System.out.println("O pinakas einai adeios.");
else
{
for (int j = 0; j <= f; j++)
{
System.out.println(F[j].toString());
}
}
//System.out.println(f);
break;
}
}
这是我的代码的一部分,我想要做的是如果我调用第二次递归(当前代码本身是递归)然后使用第二次递归的值来打印我的toString方法(放在对象类)。不要注意case 3
它只是一个“代码”,供用户选择在程序中做什么。
class Foititis
{
String onoma, epitheto;
short MesOr;
int AritMit, EtosEis;
public Foititis (String on, String ep, int AM, int EE, short MO)
{
onoma = on;
epitheto = ep;
AritMit = AM;
EtosEis = EE;
MesOr = MO;
}
public String toString()
{
String emf;
emf = "--------------------" + "\n";
emf = "Onoma: " + onoma + "\n";
emf = "Epwnymo: " + epitheto + "\n";
emf = "Arithmos Mitrwoy: " + AritMit + "\n";
emf = "Etos Eisagwnis: " + EtosEis + "\n";
emf = "Mesos Oros Mathimatwn: " + MesOr + "\n";
emf = "--------------------";
return emf;
}
}
当我执行程序时,我得到的所有输出是:--------------------
答案 0 :(得分:0)
使用
emf = emf + "Prevous line" + "\n";
在每一个新行上。 你唯一的最后一行是打印。
正确的格式是:
emf = "--------------------" + "\n"+
"Onoma: " + onoma + "\n"+
"Epwnymo: " + epitheto + "\n"+
"Arithmos Mitrwoy: " + AritMit + "\n"+
"Etos Eisagwnis: " + EtosEis + "\n"+
"Mesos Oros Mathimatwn: " + MesOr + "\n"+
"--------------------";
OR
emf = "--------------------" + "\n";
emf = emf + "Onoma: " + onoma + "\n";
emf = emf + "Epwnymo: " + epitheto + "\n";
emf = emf + "Arithmos Mitrwoy: " + AritMit + "\n";
emf = emf + "Etos Eisagwnis: " + EtosEis + "\n";
emf = emf + "Mesos Oros Mathimatwn: " + MesOr + "\n";
emf = emf + "--------------------";
答案 1 :(得分:0)
使用+=
代替=
来连接字符串,或者您将看到最后被分配的内容。
public String toString()
{
String emf;
emf = "--------------------" + "\n";
emf += "Onoma: " + onoma + "\n";
emf += "Epwnymo: " + epitheto + "\n";
emf += "Arithmos Mitrwoy: " + AritMit + "\n";
emf += "Etos Eisagwnis: " + EtosEis + "\n";
emf += "Mesos Oros Mathimatwn: " + MesOr + "\n";
emf += "--------------------";
return emf;
}
或者只是
public String toString()
{
return "--------------------" + "\n" +
"Onoma: " + onoma + "\n" +
"Epwnymo: " + epitheto + "\n" +
"Arithmos Mitrwoy: " + AritMit + "\n" +
"Etos Eisagwnis: " + EtosEis + "\n" +
"Mesos Oros Mathimatwn: " + MesOr + "\n" +
"--------------------";
}
答案 2 :(得分:0)
您实际上是在每行中分配新值而不是追加。所以它只显示最后一行。从第二次分配,您可以使用
emf += <your string>
或者您可以使用stringbuilder并附加行..最后是toString