字符串输出 - > System.out.println崩溃了

时间:2013-10-18 07:18:06

标签: java string

我正在尝试分解一些代码,以便我能更好地理解它。 这是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中的那种形式?

5 个答案:

答案 0 :(得分:1)

我不确定你的意思

  

我是否有可能以某种形式重新格式化system.out.print形式的逻辑?

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

//编辑:

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

答案 1 :(得分: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();

答案 2 :(得分: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()));
}

答案 3 :(得分:0)

你可能最好直接打印它就是你要做的就是输出。但win和winPerTurn方法是简单的吸气剂吗?应该这样命名。

如果是的话

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());
 }

}

答案 4 :(得分: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()));
}