0
votes

For a class that I am taking I am trying to create a program that produces a table of sin(), cos(), and tan() values for angles from 0 to 180 degrees in steps of 5 degrees.

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

So far I have the following code, which produces an introduction and the first two lines of the table, but I cannot figure out how to get it to repeat.

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

5 Answers

3
votes

Is not really ok that you ask people on forums to solve your assignments. Otherwise, several issues with your little program (didn't test, please do it yourself).

  1. anglex should start at 0 and stop at 180. So for(int anglex=0; anglex<=180; anglex+=5). Use anglex instead of i, inside the loop.
  2. the calculations for sinx, cosx, tanx and the printing of the new line should be inside the curlies {}. As your code is right now, the only thing inside the loop is the increment of anglex.

Sorry for not providing the full solution, pretty sure you can do it.

2
votes

Recast your for loop to

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

Note well the opening brace to enclose multiple subsequent statements. Don't forget to balance it with a closing }; probably after the println call.

Using a double as a loop index is not to everyone's taste (you can get yourself into trouble if you are not using whole numbers), but this is fine in this instance, particularly also as you are using <= as the stopping condition.

Starting variable names with an upper case letter is also to be discouraged in Java as it's unconventional.

0
votes

Your for loop only applies on the Anglex = 0+i line.

Add {} to the whole section that should be repeated.

0
votes
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);

    }
}
0
votes
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));
}

Enclose the above statements inside a { and }. The for loop applies for only the first statement in your code.