循环语句for

AnthonyWang_ 2013-03-27 08:16:34
import java.util.Scanner;
import java.math.BigInteger;
public class T5
{
public static void main(String[] args)
{
int a;
int b=1;
System.out.println("请输入要求阶乘的数:");
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
BigInteger d=BigInteger.valueOf(1);
do
{
d=d.multiply(BigInteger.valueOf(b));
b++;
}while(b<=a);
System.out.println("所求的数的阶乘是:"+d);
}
}
}
对于这个代码,我想要用for语句表达,怎么表示啊?我是卡在那个for(a;b;c)这里不知道怎么写,a是初始语句,b是跳出的语句,c是循环语句,
...全文
139 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
AnthonyWang_ 2013-03-28
  • 打赏
  • 举报
回复
在for(a;b;c)这个语句里,a部分的初始语句能不能有多条赋值语句?
AnthonyWang_ 2013-03-28
  • 打赏
  • 举报
回复
看错了,不是2楼的,搞错了
AnthonyWang_ 2013-03-28
  • 打赏
  • 举报
回复
感觉2楼写的好啊,简单,怎么不要BigInteger也能求的啊
clause3c7 2013-03-28
  • 打赏
  • 举报
回复
for (; b<=a; b++) { ... } 另外do while循环里的语句至少执行一次,所以要在for loop之前先写一遍那些语句。 这个練習应该是告诉你,何种情况下用do while循环比较好。
清蒸豆豆 2013-03-27
  • 打赏
  • 举报
回复
关于“for(a;b;c)这里不知道怎么写,a是初始语句,b是跳出的语句,c是循环语句” 其实for循环里面的a、b、c不用全写,用不到就可以不写嘛,如果都不写的话,就是while循环了··· 希望我理解的没错。
晓剑 2013-03-27
  • 打赏
  • 举报
回复
BigInteger d=BigInteger.valueOf(a); for{int b=1;b<a+1;b++}{ d=d.multiply(BigInteger.valueOf(b)); } System.out.println("所求的数的阶乘是:"+d);
冰女潮汐 2013-03-27
  • 打赏
  • 举报
回复
public class TestFactorial {
	
	public  static void main (String [] args) {

		long f = 1;
		for (int i = 1; i <= 10; i++) {
			f = f * i;
			//System.out.println("f=" + f);
		}
		System.out.println("f=" + f);              //求n!
	}
}
给楼主个小的测试下。看行不,可能有许多错误
yongtaimen 2013-03-27
  • 打赏
  • 举报
回复
for(;b<a+1;b++) { a*=a; } System.out.print(a);

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧