0

我将如何完成这个程序?我知道将计算输入代码的地方是错误的。我需要改变什么才能让程序按照作业要求的方式工作(见图)?如果用户最后选择重新计算,我会在哪里循环重新计算?

package cosx;

import java.util.Scanner;

public class cosx2 {

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

    System.out.print("Enter the degree: ");
    double degree = input.nextDouble(); 

    double radian = getRadian(degree);

    calculateCos(input, radian,degree);

}


public static double getRadian(double x) {      
    return x = (Math.PI/180)*x;     
}

private static void calculateCos(Scanner std, double rad, double deg) {
    System.out.print("How many terms do you want to calculate the cos: ");
    int terms = std.nextInt();
    double top;
    double bottom;
    double sum=0;

    for(int i = 0; i<= terms; ++i){
        top = Math.pow(-1, i);          
        bottom =1;
        int repeat = 2*i+1;
        for(int j = 1; j <= repeat; ++j){               
            bottom *= j; 
        }
        if(i%2 == 0)
            sum += (top/bottom);
        else
            sum -= (top/bottom);
    }
    System.out.printf("The sin (%.1f", deg);
    System.out.printf(") is %.6f", sum);        
     }
    }
4

0 回答 0