2016-11-04 4 views
-1

ここに私の宿題のコードがあります。それはずっと期限切れで、私はそれをうまくやっていませんでしたが、学校で実際にうまくいくために私の過ちや問題に取り組んでみたいと思っていました。しかし、とにかく:コードでJava - ダブルデータ型をフォーマットしてエラーが発生する

double x1, y1, x2, y2, x3, y3; // Three points for three vertices. 
           // (x1, y1), (x2, y2), (x3, y3) 

    Scanner input = new Scanner(System.in); 

    // The program asks the user what the name of the triangle is. Then the program 
    // asks the user for the cordinates of each point. 
    System.out.print("Enter a three letter name for the triangle: "); 
    String triangleName = input.next(); 
    System.out.print("Coordinates of vertex " + triangleName.substring(0,1).toUpperCase() + ":"); 
    x1 = input.nextDouble(); 
    y1 = input.nextDouble(); 
    System.out.print("Coordinates of vertex " + triangleName.substring(1,2).toUpperCase() + ":"); 
    x2 = input.nextDouble(); 
    y2 = input.nextDouble(); 
    System.out.print("Coordinates of vertex " + triangleName.substring(2,3).toUpperCase() + ":"); 
    x3 = input.nextDouble(); 
    y3 = input.nextDouble(); 

    double a, b, c; // These values will serve as the three side 
        // lengths between the points. 

    // The program displays side lengths in this block. 
    System.out.println("--- Side lengths ---"); 
    a = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2)); 
    System.out.printf(triangleName.substring(0,1).toUpperCase() 
         + triangleName.substring(1,2).toUpperCase() + ":" 
         + "%.3f", a); 
    System.out.println(); 
    b = Math.sqrt(Math.pow(x3-x2, 2) + Math.pow(y3-y2, 2)); 
    System.out.printf(triangleName.substring(1,2).toUpperCase() 
         + triangleName.substring(2,3).toUpperCase() + ":" 
         + "%.3f", b); 
    System.out.println(); 
    c = Math.sqrt(Math.pow(x3-x1, 2) + Math.pow(y3-y1, 2)); 
    System.out.printf(triangleName.substring(2,3).toUpperCase() 
         + triangleName.substring(0,1).toUpperCase() + ":" 
         + "%.3f", c); 
    System.out.println(); 

    double perimeter, area, xCentroid, yCentroid, inRadius, inArea, semiPerimeter, triangleHeight, triangleBase; 

    // The program displays other measures in this block. 
    System.out.println("--- Other measures ---"); 
    perimeter = a + b + c; 
    System.out.printf("Perimeter   = " + "%.3f", perimeter); 
    System.out.println(); 
    triangleHeight = y3 - 0.1; 
    triangleBase = x2 - x1; 
    area = (triangleHeight * triangleBase)/2; 
    System.out.printf("Area    = " + "%.3f", area); 
    System.out.println(); 
    xCentroid = (x1 + x2 + x3)/3; 
    yCentroid = (y1 + y2 + y3)/3; 
    System.out.printf("Centroid   = (" + "%.3f", xCentroid + ", " + yCentroid + ")"); 
    System.out.println(); 
    semiPerimeter = 0.5 * perimeter; 
    inRadius = area/semiPerimeter; 
    System.out.format("Incircle radius = " + "%.3f", inRadius); 
    System.out.println(); 
    inArea = Math.PI * Math.pow(inRadius, 2); 
    System.out.format("Incircle area  = " + "%.3f", inArea); 
    System.out.println(); 

問題は、それぞれxCentroidyCentroid値をフォーマットするための行に存在します。 "java.util.IllegalFormatConversionException: f != java.lang.String"というエラーが表示されます。プログラムの数学はうまく動作し、動作します。 "%.3f"の文を削除して、正しい答えが得られたかどうかを確認しています。それはちょうどこのフォーマットが私を怒らせることです。応答して、助けてください。私はしばらくの間起きて、忍耐強くなるでしょう。

新しいコード(固定):

System.out.print("Centroid   = "); 
    System.out.print("("); 
    System.out.printf("%.3f", xCentroid); 
    System.out.print(", "); 
    System.out.printf("%.3f", yCentroid); 
    System.out.print(")"); 
    System.out.println(); 
+0

投稿する前に[mcve]に問題を減らしてください。あなたが投稿したコードの大部分は無関係で、コピー/貼り付け/コンパイル/実行が可能な完全なサンプルを私たちに与えていません。 –

+0

すみません。将来の参考になることを覚えておいてください!私に言ってくれてありがとう。私はこの全体のStackOverflowのことを初めて知っています。間違いなくこれをより頻繁にするつもりで、コードを投稿する方が良いでしょう。 –

答えて

3
このコードで

System.out.printf("Centroid   = (" + "%.3f", xCentroid + ", " 
                 + yCentroid + ")"); 

特にこの

xCentroid + ", " + yCentroid + ")" 

の数になるだろうが、文字列

されていません

私はあなたが欲しいと思う

System.out.printf("Centroid   = (%.3f)", xCentroid + yCentroid); 
関連する問題