Java的for循环 、do-while、while三种方法求n的阶乘

 

for循环:

package com.study;
import java.util.Scanner;
public class CountFor {
    public static void main(String[] args) {
        int x;
        int sum=1;
        System.out.println("请输入数字n:");
        Scanner  in=new Scanner(System.in);
        int i=in.nextInt();
        String s = " ! =";
        for(x=1;x<=i;x++){
            sum=sum*x;
            if (xpackage com.study;
import java.util.Scanner;
public class CountDo {
    public static void main(String[] args) {
        int x=1;
        int sum=1;
        System.out.println("请输入数字n:");
        Scanner in=new Scanner(System.in);
        int i=in.nextInt();
        String s = " ! =";
        do {
            sum=sum*x;
            if (xpackage com.study;
import java.util.Scanner;
public class CountWhile {
    public static void main(String[] args) {
        int x=1;
        int sum=1;
        System.out.println("请输入数字n:");
        Scanner in=new Scanner(System.in);
        int i=in.nextInt();
        String s = " ! =";
        while(x<=i){
            sum=sum*x;
            if (x

你可能感兴趣的:(Java)