1

在我的一项作业中,我被要求编写一个程序来计算半径为 1.0 的圆上的点的 (x, y) 坐标。以 0.1 为增量显示从 1.00 到负 1.00 的所有 x 值的 y 值输出,并使用 整齐地显示输出printf,其中所有 x 值垂直对齐,所有 x 值右侧,y 值对齐垂直像:

 x1    y1
1.00  0.00
0.90  0.44

我知道如何使用勾股定理计算 y 值,但我不知道如何通过使用循环和格式化来整齐地显示每个 x 和 y 值printf下面是我到目前为止的代码,任何帮助都会不胜感激:

public class PointsOnACircleV1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    // // create menu

    // create title
    System.out.println("Points on a circle of Radius 1.0");

    // create x1 and y1
    System.out.println("          x1                         y1");

    // create line
    System.out.println("_________________________________________________");

    // // display x values

    // loop?


    // // perform calculation

    // radius
    double radius = 1.00;

    // x value
    double x = 1.00;

    // calculate y value
    double y = Math.pow(radius, 2) - Math.pow(x, 2);
}

}
4

3 回答 3

3
public static void main(String[] args) {

    double radius =  1.00;
    double x  , y ;

    for ( x=-1.0 ; x<=1.0; x+=0.2 ) {
        y = Math.sqrt(radius - Math.pow(x,2)) ;
        System.out.printf("\n" + x +"     "+ y);
    }
}

循环内的代码您可以根据需要调整它们。

于 2013-11-11T19:57:18.573 回答
1
 public class PointsOnACircleV1
 {
  public static void main (String [] args)
{
    double r = 1; //radius initialized to one

    double x = 1; // x coordinate initialized to one, could be anything
    double y = 0.0; // y coordinate is dependent so left at 0.

    //output
    System.out.println("\tPoints on a Circle of Radius 1.0"); 
    System.out.printf("\t%6s%6s%12s%7s\n", "x1", "y1", "x1", "y2");
    System.out.println("--------------------------------------------");

    //for loop to decrement values from the initialized x coordinate to the 
    //end of the diameter, radius is 1 so diameter is 2 so 1 to -1.
    for(x = 1; x >= -1; x -= .1)
    {
        y = Math.sqrt(Math.pow(r,2) - Math.pow(x,2)); //pythagorean theorem to achieve y value.
        System.out.printf("\t%6.2f%7.2f%12.2f%8.2f\n", x, y, x, -y); //output, -y to get values
        //for the other 1/2 of the circle
    }
}

}

于 2013-12-26T05:12:32.283 回答
0
for(int i=100; i>=-100; i-=10) {
    x = i/100.0;
    //do stuff
    System.out.print("\t%.2f\t%.2f", x, y);
}

那应该让你开始。如果你不明白System.out.print语句括号内的部分,我建议你查一下what System.out.printdoes,查一下format specifiers,查一下escape characters。那么你应该准备好了。

于 2013-11-11T19:10:57.147 回答