/*
题干:将1~5的这5个数相乘,得出相乘的积。
思路:一、1~5这5个数是变化的不确定的他们相乘以后的积也是不确定的。
所以定义两个变量。
二、因为这5个数要不断的增加而必须在1<=&5=>这个区域,所以用
while循环。
*/
public class da
{
public static void main(String[]args)
{ int x=1;
int a=0;
while(x<=5)
{ x++;
a=x*(x+1);
a++;
}
System.out.print("a="+a);
}
}
作者: 865408846 时间: 2016-8-23 16:19
a++是a自增的意思,应该是 a += a;作者: 865408846 时间: 2016-8-23 16:22
没必要,方法语句写成
int a= 1;
int x =1;
while(x <= 5 ) {
a *= x;
x++;
} 作者: 水月灬清影 时间: 2016-8-23 16:24
题干要求写5的阶乘,我是不知道你这思路和代码写的是啥
[Java] 纯文本查看复制代码
public static void main(String[] args) {
int n=5;
int product = 1;
while(true){
product*=n--;
if(n==1){
System.out.println(product);
break;
}
}
}