學校作業[求助]急~拜託~作業一下3題又要考期中考

Home Home
引用 | 編輯 stabmyop
2006-11-12 15:29
樓主
推文 x0
試寫一程式,可讓使用者計算以下數學方程式,其中x及n的值皆 ..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 stabmyop
2006-11-12 22:57
1樓
  
希望版主們~能夠幫我解決我目前的困境~
我會很感謝版主們的

獻花 x0
引用 | 編輯 panasonic732
2006-11-13 00:41
2樓
  
複製程式
/*
       試寫一程式,可讓使用者計算以下數學方程式,其中x及n的值皆由試用者自行輸入
       x+1/n+x+2/n-1+...+x+n/1
*/
class A3
{       public static void main(String [] args)
       {
              float n,x,ans=0,temp;
              if(args.length>1)
              {
                     System.out.println("start");
                     n=Float.parseFloat(args[0]);
                     x=Float.parseFloat(args[1]);
                     System.out.println("n="+n+"x="+x);
                     temp=n;
                     for(int i=1;i<=temp;i++)
                     {
                            System.out.println("n="+n+" x="+x+" x+i/n--="+(x+i/n));
                            ans+=(x+i/n--);
                     }
              }       
              System.out.println(ans);
       }
}

假如有錯在跟我說吧

獻花 x0
引用 | 編輯 stabmyop
2006-11-13 07:03
3樓
  
感謝大大~可是題目需要我自行輸入~x及n值
就是在還沒出現答案前.先要我輸入x及n
然後才讓答案顯示出來.

獻花 x0
引用 | 編輯 PeterPan
2006-11-16 15:47
4樓
  
複製程式
public class Test{

  public static void main(String[] args) throws Exception{
    Test t = new Test();

    java.io.BufferedReader input = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

    System.out.println("calculate one function: x+1/n+x+2/n-1+...+x+n/1");
    System.out.print("please enter x: ");
    String stTemp = input.readLine();
    double x = 0.0D;
    try{
      x = Double.parseDouble(stTemp);
    }
    catch(Exception e){
      System.out.println("parse number x error, plz enter a valid number.");
      return;
    }

    System.out.print("please enter n(n>=1): ");
    stTemp = input.readLine();
    long n = 0L;
    try{
      n = Long.parseLong(stTemp);
    }
    catch(Exception e){
      System.out.println("parse number n error, plz enter a valid number.");
      return;
    }

    if(n==0L){
      System.out.print("n must not be zero.");
    }
    else if(n<0L){
      System.out.print("n must not a negative number.");
    }
    else{
      double dResult = 0.0D;
      for(long i=0L;i<n;i++){
        dResult += x + (double)(i+1)/(double)(n-i);
      }
      System.out.print("answer: "+dResult);
    }
  }



}


獻花 x0
引用 | 編輯 stabmyop
2006-11-16 23:07
5樓
  
大大~你寫的程式真詳細~我還有很多地方要多向你學習~

獻花 x0