【问题标题】:Beginner, basic trig table in increments of 5 degrees初学者,以 5 度为增量的基本三角表
【发布时间】:2016-04-14 02:05:50
【问题描述】:

对于我正在上的一门课程,我正在尝试创建一个程序,该程序以 5 度的步长为 0 到 180 度的角度生成一个 sin()、cos() 和 tan() 值表。

!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);
    }
}

【问题讨论】:

    标签: java math trigonometry


    【解决方案1】:

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

    1. anglex 应该从 0 开始并在 180 停止。所以for(int anglex=0; anglex&lt;=180; anglex+=5)。在循环内使用anglex 而不是i
    2. sinxcosxtanx 的计算和新行的打印应该在花括号 {} 内。正如您现在的代码一样,循环内唯一的内容是anglex 的增量。

    很抱歉没有提供完整的解决方案,很确定您可以做到。

    【讨论】:

    • 感谢您的回答,我并没有要求任何人为我解决我的任务,只是为了告诉我我做错了什么。我要去学校教 K-8 数学,所以我不确定为什么这门课是必修课,而且我之前没有 Java 编程经验。到目前为止,我自己编写了整个代码,这对于知道自己在做什么但我自己不知道的人来说似乎很容易。
    • 好的,知道了。问题是修复代码的“建议”将完全解决您的任务。很遗憾听到你需要这门课来完成你的 k-8 数学教学工作,我同意这没有多大意义。
    【解决方案2】:

    将您的 for 循环重铸为

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

    请注意用大括号括起多个后续语句。不要忘记用关闭} 来平衡它;可能在println 通话之后。

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

    在 Java 中也不鼓励以大写字母开头的变量名,因为它非常规。

    【讨论】:

    • 感谢您,重铸我的 for 循环并添加 {} 帮助,现在我的代码可以工作了,感谢您提供有关使用双精度作为循环索引和使用大写字母作为变量名的提示!我对编程完全陌生,所以你帮了大忙:)
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:
      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);
      
          }
      }
      

      【讨论】:

        【解决方案5】:
        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 循环仅适用于代码中的第一条语句。

        【讨论】:

          猜你喜欢
          • 2022-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多