将正弦余弦图旋转+90度



我正在编写一个简单的java程序来实现以下输出,即绘制sin和cos,但将输出按间隔旋转90度(x 0到360,步长为10(。sin用*表示,cos用o表示。

x=  0      *      o
x= 10       *     o
x= 20        *   o
x= 30         *  o
x= 40          *o
x= 50          o*
x= 60         o  *
x= 70        o   *
x= 80       o     *
x= 90      o      *
x= 100     o       *
x= 110    o       *
x= 120   o        *
x= 130  o        *
x= 140  o        *
x= 150 o        *
x= 160 o       *
x= 170 o      *
x= 180 o     *
x= 190 o    * 
x= 200 o   * 
x= 210 o  *
x= 220 o*
x= 230  *o 
x= 240 *  o
x= 250 *   o 
x= 260 *    o
x= 270 *     o
x= 280 *      o
x= 290 *       o
x= 300 *        o
x= 310  *        o
x= 320  *        o
x= 330   *        o
x= 340    *       o
x= 350     *       o
x= 360      *      o

这是我的代码

class SinCos {
public static void main(String[] args)
{
int numx = 360;
double numy = 25.0;
for (double y = 1 ; y >= -1 ; y-=1/numy) {
double nexty = y-(1/numy);
for (double x = 0; x <= numx; x+=10) {
double siny = Math.sin(Math.toRadians(x));
double cosy = Math.cos(Math.toRadians(x));
if (siny >= nexty && siny <= y)
System.out.print('*');
else
System.out.print(' ');
if (cosy >= nexty && cosy <= y)
System.out.print('o');
else
System.out.print(' ');
}
System.out.println();
}
}
}

如何旋转输出并打印x的值作为示例。

终于解决了这个问题。使用java SinCos a1 a2 platos运行即java SinCos 0 360 25

class SinCos {
public static void main(String[] args) {
int a1, a2, w, platos;
a1 = Integer.parseInt(args[0]);
a2 = Integer.parseInt(args[1]);
w = Integer.parseInt(args[2]);
if (args.length != 3) {
System.out.println("Wrong input");
System.exit(0);
}
if (!(a1 <= a2)) {
System.out.println("a1 must be <= a2");
System.exit(0);
}
platos = (w / 2) + 1;
System.out.println();
for (int x = a1; x <= a2; x += 10) {
int sinValue = (int) Math.round(Math.sin(Math.toRadians(x)) * platos);
int cosValue = (int) Math.round(Math.cos(Math.toRadians(x)) * platos);
System.out.printf("x= %4s", x + " ");
for (int j = -platos; j <= platos; j++) {
if (j == sinValue)
System.out.print("*");
else
System.out.print(' ');
if (j == cosValue) {
System.out.print("o");
} else
System.out.print(' ');
}
System.out.println();
}
}
}

最新更新