1

我花了一段时间试图弄清楚为什么它不会在我的 java 代码中使用十进制格式。我已经用其他整数声明了十进制格式,但仍然出现错误。这是我的代码:

public class Assign31
{
  public static void main(String [] args)
  {
    DecimalFormat speedPattern = new DecimalFormat( "00.00" );
    int gearRadius = 100;
    double pi = Math.PI;
    int cadence = 90;

     double gearspeed = gearRadius * pi;

     double speedmph = gearspeed % cadence;

     System.out.println("The speed of the cyclist on a bike with gear 100.0 and cadence 90 is " + 
speedmph + " mph.");
     System.out.println("The speed (rounded) of the cycleist on the bike with gear 100.0 and cadence 90 is " + 
speedPattern.format(speedmph));
  }
}
4

2 回答 2

3

您需要导入它:

import java.text.DecimalFormat;

public class Assign31 {
    public static void main(String [] args) {
        DecimalFormat speedPattern = new DecimalFormat( "00.00" );
        // etc.
于 2013-02-06T00:08:49.847 回答
1
import java.text.DecimalFormat;

public class Assign31 {
    public static void main(String[] args) {

        // Problem 1

        // declare variables
        DecimalFormat speedPattern = new DecimalFormat("00.00");
        int gearRadius = 100;
        double pi = Math.PI;
        int cadence = 90;

        // initialze data for first scenario
        double gearspeed = gearRadius * pi;

        // calculate speed
        double speedmph = gearspeed % cadence;

        // display speed
        System.out
                .println("The speed of the cyclist on a bike with gear 100.0 and cadence 90 is "
                        + speedmph + " mph.");
        System.out
                .println("The speed (rounded) of the cycleist on the bike with gear 100.0 and cadence 90 is "
                        + speedPattern.format(speedmph));

        // end problem 1

    }
}
于 2013-02-06T00:11:44.930 回答