计算用户输入角度的正弦和余弦的程序
Program for calculating sine and cosine of angles inputted by user
我一直在尝试编写一个程序,让用户输入一个角度,假设它是度数,它会从度数转换为弧度,然后计算它的正弦或余弦。我尝试以方便用户的方式执行此操作,但事实证明我在尝试计算这两个(余弦和正弦)并输出正确结果时遇到了一些问题。我感谢您的帮助。还假设术语数不变,即 30。
import java.util.Scanner;
public class Question8 {
public static double radians(double angle)
{
double pi = 3.141592653589793238462643;
return angle / 180.0 * pi; //conversion from degrees to radians
}
public static void main( String [] args)
{
Scanner sc = new Scanner(System.in);
int sw = 0, n, j=1, m=-1;
double sine, i=0, r=0, cosine, c=0, rad; //initialising variables of type double and int
do{
System.out.println("Please input either 1 or 2 to calculate Sine or Cosine respectively.");
sw = sc.nextInt();
switch(sw) { //implementing a switch to differentiate between sine and cosine
case 1:{ //this calculates the sine
System.out.println("Please input Angle and n (number of terms) to calculate sine");
sine = sc.nextDouble();
n = sc.nextInt();
rad = radians(sine);
i = rad;
for(int k = 3; k < n; k = k+2) {
double o = Math.pow(rad,k);
j = j*(k-1)*k;
r = o/j;
i=i+m*r;
m=m*(-1);
}
System.out.println("Sine " + sine + " = " + i);
}
break;
case 2:{ //this calculates cosine
System.out.println("Please input angle and n to calculate cosine");
cosine = sc.nextDouble();
n = sc.nextInt();
rad = radians(cosine);
c = 1.0;
for(int k = 2; k < n; k = k+2) {
double o = Math.pow(rad,k);
j = j*(k-1)*k;
r = o/j;
c=c+m*r;
m = m*(-1);
}
System.out.println("Cosine " + cosine + " = " + c);
}
break;
default: {
System.out.println("Invalid choice"); //user selects invalid numbers
}
break;
}
} while(sw != 0);
}
}
您计算 sin 和 cos 的算法是正确的。您应该在每次微积分后重新初始化变量 m
、j
和 r
。您可以使用 CORDIC algorithm or chebyshev polynomial as more accurate substitute for Taylor series.
我一直在尝试编写一个程序,让用户输入一个角度,假设它是度数,它会从度数转换为弧度,然后计算它的正弦或余弦。我尝试以方便用户的方式执行此操作,但事实证明我在尝试计算这两个(余弦和正弦)并输出正确结果时遇到了一些问题。我感谢您的帮助。还假设术语数不变,即 30。
import java.util.Scanner;
public class Question8 {
public static double radians(double angle)
{
double pi = 3.141592653589793238462643;
return angle / 180.0 * pi; //conversion from degrees to radians
}
public static void main( String [] args)
{
Scanner sc = new Scanner(System.in);
int sw = 0, n, j=1, m=-1;
double sine, i=0, r=0, cosine, c=0, rad; //initialising variables of type double and int
do{
System.out.println("Please input either 1 or 2 to calculate Sine or Cosine respectively.");
sw = sc.nextInt();
switch(sw) { //implementing a switch to differentiate between sine and cosine
case 1:{ //this calculates the sine
System.out.println("Please input Angle and n (number of terms) to calculate sine");
sine = sc.nextDouble();
n = sc.nextInt();
rad = radians(sine);
i = rad;
for(int k = 3; k < n; k = k+2) {
double o = Math.pow(rad,k);
j = j*(k-1)*k;
r = o/j;
i=i+m*r;
m=m*(-1);
}
System.out.println("Sine " + sine + " = " + i);
}
break;
case 2:{ //this calculates cosine
System.out.println("Please input angle and n to calculate cosine");
cosine = sc.nextDouble();
n = sc.nextInt();
rad = radians(cosine);
c = 1.0;
for(int k = 2; k < n; k = k+2) {
double o = Math.pow(rad,k);
j = j*(k-1)*k;
r = o/j;
c=c+m*r;
m = m*(-1);
}
System.out.println("Cosine " + cosine + " = " + c);
}
break;
default: {
System.out.println("Invalid choice"); //user selects invalid numbers
}
break;
}
} while(sw != 0);
}
}
您计算 sin 和 cos 的算法是正确的。您应该在每次微积分后重新初始化变量 m
、j
和 r
。您可以使用 CORDIC algorithm or chebyshev polynomial as more accurate substitute for Taylor series.