-5

我正在编写一个体育游戏的模拟程序,它在大多数情况下都可以正常工作;像它应该的那样编译和运行。指示要求我假设我应该使用 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");
}
4

2 回答 2

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

但是现在我要问你为什么不拿homeNameand homeLocationfrom Team。当然,它们之间的相关Team性比彼此更相关。实际上,您应该查看Formattable界面,并且通过适当的编码,您可以编写:

System.out.format("%s%, homeTeam); 
于 2013-09-17T02:07:24.087 回答
1

尝试这个:

public class A {
  public static void main(String[] args) {
    System.out.println(String.format("%.2f", 12.34123123));
  }
}
于 2013-09-17T01:56:23.450 回答