2013-02-06 18 views
1

我花了一段时间试图找出为什么它不会在我的java代码中使用十进制格式。我已经用其他整数声明了小数格式,但仍然出现错误。这里是我的代码: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)); 
    } 
} 
+1

发表您的错误取悦 – 75inchpianist

回答

2

您需要将其导入:

import java.text.DecimalFormat; 

public class Assign31 { 
    public static void main(String [] args) { 
     DecimalFormat speedPattern = new DecimalFormat("00.00"); 
     // etc. 
+0

谢谢!我之前尝试过导入,但没有在公开课之前放入导入lol – darkleo91

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 

    } 
} 
+1

IMO只是将代码倾倒在没有解释的人身上(之前已经有一个解释它的答案)似乎有点多余。 –