大家幫幫忙阿~~~
要寫1個java program 
然後叫用戶輸入 X , 然後用 X 做出以下的計算

解决方案 »

  1.   

    呃~~主要是想解决一个阶乘问题吧。
    写一个阶乘方法:public long factorial(int n){
    long result = 1;
    for(int i = 1; i<=n; i++){ 
    result *= i; 
    }
    } 之后带入公式不是就能得出来吗?
      

  2.   

    不會寫了耶,有人能教一教嗎@@
    import javax.swing.JOptionPane;
    public class myprogram {public static void main(String[] args)
    {
    String valueX=JOptionPane.showInputDialog("Enter an integer value");
    int x=Integer.parseInt(myValue);for ( long counterf = 0; counter <= 6; counterf++ )

    long result;
    result =1+ myValue/factorial( counterf ) - 2*myValue/factorial( counterf ) + 3*myValue/factorial( counterf ) - 4*myValue/factorial( counterf ) + 5*myValue/factorial( counterf ) - 6*myValue/factorial( counterf );
    System.out.println("The vAnswer is"+result); }
    // recursive declaration of method factorial  
           public long factorial( long number )          
           {                                             
              // base case                               
              if ( number <= 1 )                         
                 return 1;                               
                                                         
              // recursive step                          
              else                                       
                 return number * factorial( number - 1 );
                                                         
           } // end method factorial                     
        
        } // end class myprogram
      

  3.   

    import javax.swing.JOptionPane;public class Test {    public static void main(String[] args) {
            String value = JOptionPane.showInputDialog("Enter an integer value");
            int x = Integer.parseInt(value);
            System.out.println("The answer is: " + calculate(x));
        }    private static double calculate(int num) {
            double result = 1;
            int sign = 1;
            int factor = 1;
            for(int k = 1; k <= 6; k++) {
                factor *= k;
                result += (sign * k * num) / (double)(factor);
                sign = -sign;
            }
            return result;
        }
    }
      

  4.   


    謝謝你唷,我沒有在考試@@
    只是功課的一部分.
    現在我試試吧他改寫去interface,等等再回來請教你們^^
      

  5.   

    JFRAME 也不會寫@_@"
    怎麼辦QQ
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class TestJ extends JFrame implements ActionListener{
    Label lblEnterNumber,lblResult; 
    TextField txtInputNumber,resultInputNumber;  
        public static void main(String[] args) {  Container container = getContentPane();
             container.setLayout( new FlowLayout() );
            lblEnterNumber=new Label("Plese Input The Value:"); 
    container.add( lblEnterNumber );  // create txtInputNumber and attach it to content pane
             txtInputNumber = new TextField( 10 );
             container.add( txtInputNumber ); // register this applet as numberField’s ActionListener
             txtInputNumber.addActionListener( this );   
    // create resultLabel and attach it to content pane
             lblResult = new Label( "The answer is:" );
             container.add( lblResult );  // create numberField, make it uneditable
             // and attach it to content pane
              resultInputNumber = new TextField( 15 );
              resultInputNumber.setEditable( false );
              container.add( resultInputNumber );
            } // end method 
        
        // obtain user input and call method Calculate
           public void actionPerformed( ActionEvent event )
           {  int number ,calculatevalue;// obtain user’s input and convert to long
             int x = Integer.parseInt(txtInputNumber.getText() ); showStatus( "Calculating ..." ); 
    // calculate Answer for number user input
    calculatevalue = calculate(x);
    // indicate processing complete and display result
            showStatus( "Done." );   
    resultInputNumber.setText( double.toString( resultvalue ) );
    } // end method actionPerformed
    // recursive declaration of method calculate    private static double calculate(int num) {
            double result = 1;
            int sign = 1;
            int factor = 1;
            for(int k = 1; k <= 6; k++) {
                factor *= k;
                result += (sign * k * num) / (double)(factor);
                sign = -sign;
            }
            return result;
        }// end method calculat
    }// end class TestJ