當前位置:首頁 » 編程語言 » 階乘java

階乘java

發布時間: 2022-01-07 23:49:35

1. java 階乘

同意qsmy的意見,不過樓主為什麼非用for循環而不用遞歸來實現呢?

2. 用java寫的階乘

public class DoWork extends Thread {
//author by sunvins 5.14
private int type;

private static Double result = 0d;

private static boolean isOver = false;

private static Double num = 0d;

private final Double maxN = 100d;
//最大記到的整數,我測的結果,最多取171就溢出了

public DoWork(int type) {
super();
this.type = type;
}

public static void main(String[] args) {
DoWork work1 = new DoWork(1);
DoWork work2 = new DoWork(2);
work1.start();
work2.start();
}

public void run() {
while (!isOver && type == 1) {// 注意:兩個while不能合並
sumN(maxN);
}
while (num <= maxN && type == 2) {
// 讀取結果
System.out.println("result=" + result + " n=" + num);
// work1停了,work2還要讀最後一次的結果
if (num >= maxN)
break;
try {
sleep(10);// 小睡一會兒再讀
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

// 算階乘和
public void sumN(Double n) {
Double lastFactorial = 1d;
for (Double i = 1d; i <= n; i++) {
this.num = i;// 計數器,記住算到哪個數了
try {
sleep(5);// work1也要小小休息,不然太快了,work2都還來不及記,呵
} catch (InterruptedException e) {
e.printStackTrace();
}
// result += factorial(i);// 常規演算法調用
lastFactorial *= i;
result += lastFactorial; //這是優化演算法,只是不知道在這里有沒意義
}
isOver = true;
}

// 算階乘,這個是常規演算法
// public Double factorial(Double n) {
// if (n < 1)
// return 1d;
// return n * factorial(n - 1);
// }
}

3. java n的階乘

無語

public class jjjj {

int pp(int n)
{
if(n<2)
return 1*n;
else
return n*pp(n-1);
}
public static void main(String args[])
{
jjjj b=new jjjj();
int d=b.pp(5);
System.out.print(d);
}
}

階乘就是利用遞歸啊。

整個程序的核心就是int pp(int n)
{
if(n<2)
return 1*n;
else
return n*pp(n-1);
}
這一段, 如果n大於2,返回n*pp(n-1)的意思,就是,任何一個N的階乘等於n*比n小1的數的階乘,打個比方,比如5的階乘等於5*(4的階乘),4的階乘等於4*(3的階乘),而1的階乘,等於1本身。

4. java計算n的階乘

int n=8;
int p=n;
for(int i=n;i>1;i--){
p*=i;
}
print("%d\n",p);

5. Java階乘 程序

缺少返回值
public void (n){
for(n=20;n>1;n--)
{
n=n*(n-1);
}
return n;
}

在smultiply(n)函數里的n是局部變數,只在函數內部有效,一出這個函數,n就失效了,有了return n;這條語句才能把n的值返回給result1,否則result1還是原來的值,在定義變數的時候你沒有給它賦值,導致你在輸出的時候找不到result1的值

在寫程序的時候,最好在定義的時候給它賦一個初值:double result1=0;

6. 關於JAVA階乘!

答案肯定有問題撒。。你根本就沒求階乘。。
static int fuc(int n){
return n;
}
這個方法改下
static int fuc(int n){
if(n == 0) return 0;
int sum = 1;
for(int i = 1; i <= n; i++) {
sum *= i;
}
return sum;
}

7. 用java求階乘

求階乘用for就行,假設我們要對num求階乘,結果是result
int result = 1;

for (int i=1;i<=num;i++){
result *= i;
}
此時result的值即為num的階乘(僅正整數)

8. java階乘

主要原因是你的輸入方法寫錯了
我又給你寫了一個 ,我運行了,好用。具體如下
public class Text {
public static void main(String[] args) throws IOException{
// int i=0;
// i=i++;
// System.out.println(i);
// System.out.println(i);
// System.out.println(i+1);
// System.out.println(i);
System.out.println("請輸入一個數:");
Scanner sc=new Scanner(System.in);
int one=sc.nextInt();
int sum=1;
for(int i=one;i>0;i--){
sum*=i;
}
System.out.println(one+"!"+"="+sum);
}
}

9. java中階乘怎麼表示

沒有自帶方法表示階乘,必須自己通過循環來實現

10. JAVA中怎麼表示階乘

1、首先在電腦打開eclipse軟體,創建Scanner對象。

熱點內容
安卓版托卡世界如何下載 發布:2024-10-25 20:51:24 瀏覽:578
賣二手安卓機哪個平台好 發布:2024-10-25 20:44:41 瀏覽:235
sql聯表更新 發布:2024-10-25 20:38:18 瀏覽:481
文件加密拒絕訪問 發布:2024-10-25 20:29:58 瀏覽:736
紅樓夢的編譯人有哪些 發布:2024-10-25 20:12:33 瀏覽:136
訪問的訪 發布:2024-10-25 20:12:31 瀏覽:625
樂視手機安裝包文件夾 發布:2024-10-25 20:08:27 瀏覽:861
電腦網頁顯示無法連接代理伺服器 發布:2024-10-25 20:06:46 瀏覽:408
基礎教程sqlserver2008 發布:2024-10-25 20:05:52 瀏覽:772
什麼是ris伺服器 發布:2024-10-25 19:54:58 瀏覽:853