Java - 如何将值打印到2位小数

时间:2013-09-17 01:34:41

标签: java printf

我正在编写体育游戏的模拟,它在大多数情况下都能正常工作;编译和运行它应该。方向要求我假设我应该使用printf和%.2f,但每当我尝试将其合并到我的代码中时,它就会停止正常运行。非常感谢帮助!

import java.util.Scanner;

public class Team {

public String name;

public String location;

public double offense;

public double defense;

public Team winner;

public Team(String name, String location) {
  this.name = name;
  this.location = location;
  this.offense = luck();
  this.defense = luck();
}     

public double luck() {
    return Math.random();
}

Team play(Team visitor) {
    Team winner;
    double home;
    double away;
    home = (this.offense + this.defense + 0.2) * this.luck();
    away = (visitor.offense + visitor.defense) * visitor.luck();
    if (home > away)
      winner = this;
    else if (home < away)
      winner = visitor;
    else
      winner = this;
    return winner;
}

public static void main(String[] args) {
  Scanner s = new Scanner(System.in);
    System.out.println("Enter name and location for home team (on separate lines)");
                         String homeName = s.next();
                         String homeLocation = s.next();
                         Team homeTeam = new Team(homeName, homeLocation);
    System.out.println("Enter name and location for home team (on separate lines)");
                         String awayName = s.next();
                         String awayLocation = s.next();
                         Team awayTeam = new Team(awayName, awayLocation);
    Team winnerTeam = homeTeam.play(awayTeam);
    System.out.printf("Home team is:" + homeName + " from" + homeLocation + " rated" + homeTeam.offense + " (offense) +" + homeTeam.defense + " (defense)" + "\n");
    System.out.printf("Away team is:" + awayName + " from" + awayLocation + " rated" + awayTeam.offense + " (offense) +" + awayTeam.defense + " (defense)" + "\n");
    System.out.printf("Winner is:" + winnerTeam.name + " from" + winnerTeam.location + " rated" + winnerTeam.offense + " (offense) +" + winnerTeam.defense + " (defense)" + "\n");
}

2 个答案:

答案 0 :(得分:2)

您误解了printf方法。您不会像在此行及其后续行中那样连接字符串(出于宽度原因重新格式化):

 System.out.printf("Home team is:" + homeName +
                   " from" + homeLocation +
                   " rated" +  homeTeam.offense +
                   " (offense) +" + homeTeam.defense +
                   " (defense)" + "\n");

这就像一位老同事试图使用PreparedStatements来防止SQL注入攻击的方式,但无论如何通过连接构造查询字符串,使得尝试无效。相反,请查看printf

的签名
public PrintWriter format(String format, Object... args)

第一个参数是格式字符串,其中包含以%开头的静态文本和格式指令。在典型的使用中,每个格式指令对应于该方法的一个参数。用指令替换插值变量。

字符串通常使用%s:s格式化为字符串。双打通常用%f格式化:f表示浮点(或双精度)。 %和字母之间的字符是选项。所以,让我们用指令替换你插入的字符串:

"Home team is: "  + "%s" +             // Inserted a space.
" from"           + "%s" +
" rated"          + "%6.2f" +          // Six characters, 2 after the decimal.
" (offense)    +" + "%6.2f" +
" (defense)"      + "%n"               // %n means the appropriate way to get a new line
                                       // for the encoding.

现在我们把它们放在一起:

System.out.format("Home team is: %s from %s rated %6.2f (offense) + %6.2f (defense)%n",
                   homeName, homeLocation, homeTeam.offense, homeTeam.defense);

这简单得多。此外,避免在格式字符串中插入字符串的另一个原因是您插入的字符串本身可能包含百分号。看看如果你无意中写下这会发生什么:

String salesTax = "5%";
System.out.format("The sales tax is " + salesTax);

这相当于

System.out.format("The sales tax is 5%");

不幸的是,百分号被视为格式指令,格式语句抛出异常。正确的是:

System.out.format("The sales tax is 5%%");

String salesTax = "5%";
System.out.format("The sales tax is %s", salesTax);

但现在我应该问你为什么不从homeName采取homeLocationTeam。当然,它们与Team相比更具相关性。实际上,您应该查找Formattable界面,并使用正确的编码编写:

System.out.format("%s%, homeTeam); 

答案 1 :(得分:1)

试试这个:

public class A {
  public static void main(String[] args) {
    System.out.println(String.format("%.2f", 12.34123123));
  }
}