1

Javaコードで10進形式を使用しない理由を理解しようと、しばらく時間を費やしました。他の整数で小数形式を宣言しましたが、まだエラーが発生しています。これが私のコードです:

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 に答える