实验三:分别用forwhile和do-while循环语句以及递归方法计算n!,并输出算式

Posted java-wyw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验三:分别用forwhile和do-while循环语句以及递归方法计算n!,并输出算式相关的知识,希望对你有一定的参考价值。

一.程序与结果

1.for循环

package package;

public class class {

public static void main(String[] args);

int i;

int fact=1;

int n=5;

for(i=1;i<=n;i++)

{

fact=fact*i;

}

   System.out.print("fact="+fact);

}

}

实验结果:fact=120

2.while循环

package while_package;

public class while_class {

public static void main(String[] args) {

int i=1;

int fact=1;

int n;

Scanner in=new Scanner(System.in);

System.out.println("请输入n:");

n=in.nextInt();

while(i<=n)

{

fact=fact*i;

i=i+1;

}

for(i=1;i<=n;i++)

if(i<n)

System.out.print(i+"*");

else 

System.out.print(i);

System.out.print("="+fact);

}

}

实验结果:1*2*3*4*5=120

3.do-while循环

package dowhile_package;

public class dowhile_class {

public static void main(String[] args) {

int i=1;

int fact=1;

int n;

Scanner in=new Scanner(System.in);

System.out.println("请输入n:");

n=in.nextInt();

do

{

fact=fact*i;

i=i+1;

}while(i<=n);

for(i=1;i<=n;i++)

if(i<n)

 System.out.print(i+"*");//记得加+

else

 System.out.print(i);

System.out.print("="+fact);

}

}

实验结果:1*2*3*4*5=120

二.心得体会

      在本次的实验调试过程中,我遇到了很多问题。比如刚开始是要新建一个Java项目,建立好之后要在这个Java项目下再新建一个类,但在建立类的过程中总是会出现“不鼓励使用默认包”的此类问题。但在经过不断调试之后,这个问题得到了解决。还有一些问题是在编译程序方面的,最终问题都有所改善。通过本次实验,使我提高了对Java语言的学习兴趣。

以上是关于实验三:分别用forwhile和do-while循环语句以及递归方法计算n!,并输出算式的主要内容,如果未能解决你的问题,请参考以下文章

实验三:分别用forwhile和do-while循环语句以及递归方法计算n!,并输出算式

实验三:分别用forwhile和do-while循环语句以及递归方法计算n!,并输出算式

实验三:分别用forwhile和do-while循环语句以及递归方法计算n!,并输出算式

实验三:分别用for,while和do-while以及递归算法求n!

c语言实验三

约瑟夫环(循环链表)