4

我正在尝试分解一些代码,以便我可以更好地理解它。这是 String 方法的一个片段

public String randomGame() {

     String output = "There were " + Count + " wars\n";
     output += player1.getName() + " won " + player1.win() + " cards and " + player1.getTheory() + " theory(s)\n";
     output += player2.getName() + " won " + player2.win() + " cards and " + player2.getTheory() + " theory(s)\n";


     if(player1.winPerTurn() > player2.winPerTurn()){
        output += "Winner: " + player1.getName();
     } 
     else {
        output += "Winner: " + player2.getName();
     }

     return output;
  }

就我个人而言,我对这些输出感到不舒服。我已经知道它打印出来了,我的问题是。我有可能以某种方式以 system.out.print 形式重新格式化这个逻辑吗?

4

5 回答 5

1

我不确定你的意思

我有可能以某种方式以 system.out.print 形式重新格式化这个逻辑吗?

但是,如果您的目标是更好地了解代码中发生的情况,您可以使用调试器逐步检查您的代码,并查看哪个变量包含哪个值。

//编辑 :

有关如何在 Eclipse 中使用调试器的教程,请参阅此处

于 2013-10-18T07:21:04.397 回答
1

可以使用 %s 来修改信息,所以第一个输出

String output = String.format("There were %s wars\n", Count);

并以最佳实践方法。

private static final String WARS_STRING = "There were %s wars\n";

//in some code
String output = String.format(WARS_STRING, Count);

还有另一种构建字符串的选项,使用前面的方法,即使用 StringBuilder 将它们全部连接起来。

private static final String WARS_STRING = "There were %s wars\n";
private static final String WON = "%s won %s cards and %s theory(s)\n";

//in some code
StringBuilder builder = new StringBuilder();
builder.append(String.format(WARS_STRING, Count));
builder.append(String.format(WON, player2.win(), player1.win(),player1.getTheory()));
builder.append(String.format(WON, player1.win(), player2.win(),player2.getTheory()));
return builder.toString();
于 2013-10-18T07:21:12.823 回答
1

有帮助吗:

private void displayPlayerInfo(Player player)
{
    System.out.println(player.getName() + " won " + player.win() + " cards and " + player.getTheory() + " theory(s)");
}

public void randomGame() {
    System.out.println("There were " + Count + " wars");
    displayPlayerInfo(player1);
    displayPlayerInfo(player2);
    System.out.println("Winner: " + (player1.winPerTurn() > player2.winPerTurn()? player1.getName(): player2.getName()));
}
于 2013-10-18T07:31:50.357 回答
0

尝试这个 ...

public void randomGame() {
    System.out.println("There were " + Count + " wars");
    System.out.println(player1.getName() + " won " + player1.win() + " cards and " + player1.getTheory() + " theory(s)");
    System.out.println(player2.getName() + " won " + player2.win() + " cards and " + player2.getTheory() + " theory(s)");
    System.out.println("Winner: " + (player1.winPerTurn() > player2.winPerTurn()? player1.getName(): player2.getName()));
}
于 2013-10-18T07:49:19.693 回答
0

您最好直接打印它,这就是您要对输出做的所有事情。但是 win 和 winPerTurn 方法是简单的 getter 吗?应该这样命名。

如果是这样

public void randomGame() {

 System.out.print ("There were " + Count + " wars\n");
 System.out.print (player1.getName() + " won " + player1.win() + " cards and " + player1.getTheory() + " theory(s)\n");
 System.out.print (player2.getName() + " won " + player2.win() + " cards and " + player2.getTheory() + " theory(s)\n");


 if(player1.winPerTurn() > player2.winPerTurn()){
    System.out.println ("Winner: " + player1.getName());
 } 
 else {
    System.out.println (output += "Winner: " + player2.getName());
 }

}
于 2013-10-18T07:22:48.243 回答