初学者,基本触发表,增量为5度

时间:2016-04-13 17:01:14

标签: java math sin cos

对于我正在学习的课程,我正在尝试创建一个程序,该程序生成一个sin(),cos()和tan()值表,其中0到180度的角度为5度。

http://i65.tinypic.com/14ahliq.jpg

到目前为止,我有以下代码,它产生了一个介绍和表的前两行,但我无法弄清楚如何让它重复。

import java.util.*;

public class Angles {

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        System.out.println("This program computes the");
        System.out.println("sin(), cos(), and tan() values");
        System.out.println("for angles from 0 to 180 degrees");
        System.out.println("in steps of 5 degrees.");

        System.out.println("");

        System.out.println("Angle\tSin()\tCos()\tTan()");
        double Anglex = 0;
        for(double i = 5;i <= Anglex;i += 5) {
            Anglex = 0 + i;
        }
        double Sinx = Math.sin(Math.toRadians(Anglex));
        double Cosx = Math.cos(Math.toRadians(Anglex));
        double Tanx = Math.tan(Math.toRadians(Anglex));

        System.out.println(Anglex + "\t" + Sinx + "\t" + Cosx + "\t" + Tanx);
    }
}

5 个答案:

答案 0 :(得分:3)

你要求论坛上的人解决你的作业真的不错。 否则,你的小程序有几个问题(没有测试,请自己动手)。

  1. anglex应从0开始,然后停在180。所以for(int anglex=0; anglex<=180; anglex+=5)。在循环中使用anglex代替i
  2. sinxcosxtanx的计算以及新行的打印应在curlies {}内。由于您的代码现在是正确的,因此循环内部唯一的内容是anglex的增量。
  3. 很抱歉没有提供完整的解决方案,相当确定你可以做到。

答案 1 :(得分:2)

将您的for循环重播为

for (double Anglex = 0; Anglex <= 180; Anglex += 5){

请注意打开括号以包含多个后续语句。不要忘记平衡它与结束};可能在println电话之后。

使用double作为循环索引并不符合每个人的口味(如果你不使用整数,你可能会遇到麻烦),但在这种情况下这很好,尤其是您使用<=作为停止条件。

在Java中也不鼓励使用大写字母起始变量名称,因为它是非常规的。

答案 2 :(得分:0)

for(double i = 5;i <= Anglex;i += 5) {
    Anglex = 0 + i;
    double Sinx = Math.sin(Math.toRadians(Anglex));
    double Cosx = Math.cos(Math.toRadians(Anglex));
    double Tanx = Math.tan(Math.toRadians(Anglex));
}

将上述陈述包含在{和}中。 for循环仅适用于代码中的第一个语句。

答案 3 :(得分:0)

您的for循环仅适用于Anglex = 0 + i行。

将{}添加到应重复的整个部分。

答案 4 :(得分:0)

public static void main(String[] args) {

    System.out.println("This program computes the");
    System.out.println("sin(), cos(), and tan() values");
    System.out.println("for angles from 0 to 180 degrees");
    System.out.println("in steps of 5 degrees.");

    System.out.println("");

    System.out.println("Angle\tSin()\tCos()\tTan()");
    double maxAngleX = 180.0;
    for (double angleX = 5; angleX <= maxAngleX; angleX += 5) {

      double Sinx = Math.sin(Math.toRadians(angleX));
      double Cosx = Math.cos(Math.toRadians(angleX));
      double Tanx = Math.tan(Math.toRadians(angleX));

      System.out.println(angleX + "\t" + Sinx + "\t" + Cosx + "\t" + Tanx);

    }
}