很多未知来源..?

时间:2015-07-23 07:18:20

标签: java

  

线程“main”中的异常
  java.util.UnknownFormatConversionException:Conversion =''在
  java的java.util.Formatter.checkText(未知来源)   java的java.util.Formatter.parse(未知来源)   java的java.util.Formatter.format(未知来源)   java的java.util.Formatter.format(未知来源)   java中的java.lang.String.format(未知来源)   在。的graphics.shape.toString(shape.java:24)   在。的java.lang.String.valueOf(未知来源)   java的java.io.PrintStream.println(未知来源)   graphics.Test.main(Test.java:7)

以下代码对应上述错误:

 package graphics;
    public class shape {
    public double radius, angleT, angleP, arcT, arcP, wS, h, wanted;
    public double sH, w, overlap,tR;

    public shape (double shortHeight, double width, double overlapDistance, double tR){
        sH = shortHeight;
        w = width;
        overlap = overlapDistance;
        this.tR = tR;
        radius = Maths.getRadius(sH,w);
        angleT = Maths.getAngleTotal(sH, w);

        arcP = Maths.getArcPartial(sH, w, overlap);
        angleP = (angleT*(arcP/arcT))/2;
        wS = 2*(Math.sin(angleP)*radius);
        h = radius * Math.cos(angleP);
        wanted = tR * wS;
    }

    @Override
    public String toString(){
        return String.format("Given:/n"
                + "Short Height: %f\n"
                + "Width: %f\n"
                + "Percent Overlap: %f/%\n"
                + "Projector's throw ratio: %.2f\n"
                + "<Calculated>\n"
                + "Radius: %.2f\n"
                + "Angle Total: %.2f degrees\n"
                + "Arc Total: %.2f\n"
                + "Angle Partial: %.2f\n"
                + "Arc Partial: %.2f\n"
                + "Width smaller: %.2f\n"
                + "Height of smaller: %.2f\n"
                + "[wanted length]: %.2f\n",sH,w,overlap, tR, radius, angleT, arcT, angleP, arcP, wS, h, wanted);
    }

}

数学课程是:

package graphics;

public class Maths {

    public double radius, angleTotal, arcTotal, arcPartial, anglePartial;

    public static double getRadius(double heightSmall, double width){
        return ((Math.pow(heightSmall,2))+ (Math.pow((width/2),2)))/(2*heightSmall);
    }

    public static double getAngleTotal(double hS, double w){
        return 2*Math.toDegrees(Math.atan2((w/2),((getRadius(hS, w))-hS)));
    }

    public static double getArcTotal(double hS, double w){
        return Math.toRadians(getAngleTotal(hS,w))*getRadius(hS, w);
    }

    public static double getArcPartial(double hS, double w, double overlap){
        return (getArcTotal(hS, w)*(1+overlap))/2;
    }
}

但我不认为这部分是问题....测试类有:

package graphics;

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

        shape demo = new shape(1,6,.25, .5);
        System.out.println(demo);       
    }    
}

我不知道为什么形状类中的toString()不起作用...

1 个答案:

答案 0 :(得分:1)

试试这个:

return String.format("Given:\n"
                + "Short Height: %f\n"
                + "Width: %f\n"
                + "Percent Overlap: %f%%\n"
                + "Projector's throw ratio: %.2f\n"
                + "<Calculated>\n"
                + "Radius: %.2f\n"
                + "Angle Total: %.2f degrees\n"
                + "Arc Total: %.2f\n"
                + "Angle Partial: %.2f\n"
                + "Arc Partial: %.2f\n"
                + "Width smaller: %.2f\n"
                + "Height of smaller: %.2f\n"
                + "[wanted length]: %.2f\n", ...);

<强>输出:

Given:
Short Height: 1.000000
Width: 1.000000
Percent Overlap: 1.000000%
Projector's throw ratio: 1.00
<Calculated>
Radius: 1.00
Angle Total: 1.00 degrees
Arc Total: 1.00
Angle Partial: 1.00
Arc Partial: 1.00
Width smaller: 1.00
Height of smaller: 1.00
[wanted length]: 1.00
  • /n在第一行更改为\n
  • "Percent Overlap: %f/%\n"已更改为"Percent Overlap: %f%%\n"(%符号需要使用%符号进行转义)
  • 当我给它12个漂浮值时它起作用了。