English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

任意輸入一個整數,例如輸入整數5,輸出5!=120
public class NameDialog
{
public static void main( String args[] )
{
// prompt user to enter name
String name =
JOptionPane.showInputDialog( "請輸入數字?" );
{
double a =1;
for (int x =0; x<=100; x+=10)
{
a=1;
for(int y=1; y<=x; y++)
{
a*=y;
}
System.out.println(x+ "!="+a);
}
}
}
}

2007-05-18 13:00:47 · 2 個解答 · 發問者 Anonymous in 電腦與網際網路 程式設計

2 個解答

你是不是把問題想得太難了... 其實你已經寫得差不多啦!
你把main小修一下即可 (如下):

public static void main(String[] args) {
// prompt user to enter name
String name = JOptionPane.showInputDialog("請輸入數字?");
int x = Integer.parseInt(name);
double a = 1;
for (int y = 1; y <= x; y++) {
a *= y;
}

System.out.println(x + "!=" + a);
}

希望對你有所幫助!

2007-05-18 19:40:44 · answer #1 · answered by tomy 4 · 0 0

//Power by Eclipse v3.2
import java.io.*;
import javax.swing.*;
//測試類別檔名 TEST_JAVA.java
public class TEST_JAVA
{
public static void main(String[] args)
{
//JAVA輸入輸出!階乘!
//==========START==========//
PrintStream p=new PrintStream(System.out);
int num=Integer.parseInt(JOptionPane.showInputDialog("使用者:"+JOptionPane.showInputDialog("請輸入姓名")+"\n請輸入階層數值"));
int result=1;
for(int i=1;i<=num;i++){
result*=i;
}
p.printf("%d!= %d\n",num,result);
//==========END==========//
}
}

2007-05-19 08:39:44 · answer #2 · answered by Big_John-tw 7 · 0 0

fedest.com, questions and answers