有两个地方建议改改。。。第一个是循环中的“number<=i”,,,把等号删除。。。。第二个是循环中的fact*=i....把i改为number。。。。改正之后应该就是没问题了
你这啥玩意 用我这个
Scanner input=new Scanner(System.in);
System.out.println("输入数据:");
int i=input.nextInt();
// String msg="";
StringBuffer msg = new StringBuffer();
int fact=1;
if(i>10||i<1){
System.out.println("程序有误");
return;
}
msg.append(i+"!=");
// System.out.print(i+"!=");
for(int number=1;number<=i;number++){
fact=fact*number;
if(number == 4){
msg.append(number);
}else{
msg.append(number+"*");
}
}
msg.append("="+fact);
// fact*=i;
// msg+=i+"="+fact;
System.out.println(msg);
for(int number=1;number<=i;number++)
其中判断条件的number
因为你在循环外还有一次乘法
这么改就对了:
public class jieda2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入数据:");
int i = input.nextInt();
String msg = "";
int fact = 1;
if (i > 10 || i < 1) {
System.out.println("程序有误");
return;
}
System.out.print(i + "!=");
for (int number = 1; number <= i; number++) {
msg += number + "*";
fact = fact * number;
}
msg += i + "=" + fact;
System.out.println(fact);
}
}
for 循环改为
for (int number = 1; number msg += number + "*";
fact =fact*number;
}
fact =fact*i;
msg += i+"=" + fact;
System.out.println(msg);