一、求一个数的阶乘
代码:
import java.util.Scanner;
public class Operator02 {
public static void main(String[] args) {
//
// // 求阶乘
Scanner sc = new Scanner(System.in);
while (true) {
int res = 1;
int num = sc.nextInt();
int n = num;
if (n<=0) {
System.out.println("整数输入不能为负数!");
} else {
while (n >= 1) {
res = res * n;
n = n-1;
}
System.out.println(num+"的阶乘是:"+res);
}
}
}
}
二、求1-100 之间的奇数
代码:
public class While01 {
public static void main(String[] args) {
// 求1-100 之间的奇数
int count = 0;
while(count <100) {
if (count%2 != 0) {
System.out.println(count);
}
count++;// count 每次自增1 相当于count = count +1;
}
}
}