一些代码例子:
1、//打印1~100的和
public static void main1(String[] args) {
int i = 1;
int sum = 0;
while(i <= 100){
sum += i;
i++;
}
System.out.println(sum);
}
2、//求5的阶乘
public static void main2(String[] args) {
int i = 1;
int ret = 1;
while(i <= 5) {
ret *= i;
i++;
}
System.out.println(ret);//120
}
方法2:
public static void main12(String[] args) {
int ret = 1;
for (int i = 1; i <= 5 ; i++) {
ret *= i;
}
System.out.println(ret);//120
}
3、//求1~100的和,及其奇数和,偶数和
public static void main6(String[] args) {
int i = 1;
int sum = 0;
while(i <= 100) {
sum += i;
i++;
}
System.out.println("总和:"+sum);//总和:5050
i = 1;
int sum1 = 0;
while(i <= 100) {
sum1 += i;
i += 2;
}
System.out.println("奇数和:"+sum1); ("奇数和:"+sum1);//奇数和:2500
i = 2;
int sum2 = 0;
while(i <= 100) {
sum2 += i;
i += 2;
}
System.out.println("偶数和:"+sum2);//偶数和:2550
}
4、//求5的阶乘的和;153
public static void main7(String[] args) {
int j = 1;
int sum = 0;
int i = 1;
int ret = 1;
while (j <= 5) {
while (i <= j) {
ret *= i;
i++;
}
sum += ret;
j++;
}
System.out.println(sum);
}
5、 //在100到200之间找出一个被3整除的数
public static void main8(String[] args) {
int n = 100;
while (n <= 200) {
if (n % 3 == 0) {
System.out.println(n);
break;//结束所有的循环
}
n++;
}
}
6、 //输出100到200所有被3整除的数
public static void main9(String[] args) {
int n = 100;
while (n <= 200) {
if (n % 3 != 0) {
n++;
continue;//结束本趟循环
}
System.out.println(n);
n++;
}
}
7、//输出1~100即被3整除又被5整除的数
public static void main10(String[] args) {
int i = 1;
while(i <= 100) {
if (i % 15 != 0 ){
i++;
continue;
}
System.out.println(i);
i++;
}
}
8、 //求1!+2!+3!+4!+5!
public static void main13(String[] args) {
int ret = 1;
int sum = 0;
int i = 1;
int j = 1;
for (; i <= 5 ; i++) {
for (; j <= i ; j++){
ret *= j;
}
sum += ret;
}
System.out.println(sum);//153
}
9、 public static void main14(String[] args) {
int i = 1;
do {
System.out.println("hhh");
}while (i != 1);//hhh,不管条件为真假,程序总会执行一次。
}
10、 //猜数字游戏
public static void main18(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int ran = random.nextInt(100);//[0,100)
while (true) {
System.out.println("请输入你要猜的数字:");
int i = scanner.nextInt();
if (i > ran) {
System.out.println("高了");
} else if (i == ran) {
System.out.println("猜对了");
break;
} else {
System.out.println("低了");
}
}
}
11、//根据输入的数字,判断其年龄段
public static void main19(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int age = scanner.nextInt();
if (age < 18) {
System.out.println("少年!");
} else if (age < 28) {
System.out.println("青年!");
} else if (age < 55) {
System.out.println("中年");
} else {
System.out.println("老年!");
}
}
}
12、 //判断一个数是否为素数
public static void main20(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n == 1) {
System.out.println("既不是素数,也不是合数");
}
int i = 2;
for (i = 2; i < n; i++) {//或者i<n/2
if (n % i == 0) {
System.out.println(n + "不是素数");
break;
}
}
if (i == n){//或者i>n/2
System.out.println(n+"是素数");
}
}
方法二:
public static void main22(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n == 1) {
System.out.println("既不是素数,也不是合数");
}
int i = 2;
for (i = 2; i <= Math.sqrt(n) ; i++) {//或者i <= n/2
if (n % i == 0) {
System.out.println(n + "不是素数");
break;
}
}
if (i > Math.sqrt(n) ){//或者i > n/2
System.out.println(n+"是素数");
}
}
13、 //输出1到100的所有素数
public static void main21(String[] args) {
int i = 1;
for (; i <= 100; i++) {
int j = 2;
for (; j < i; j++) {
if (i % j == 0) {
break;
}
}
if (j == i){
System.out.println(i+"是素数");
}
}
}
14、 //输入一个n,打印出对应的乘法表
public static void main23(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 1 ; i <= n ; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();
}
}
15、 //求两个数的最大公约数
public static void main24(String[] args) {
Scanner scanner = new Scanner(System.in);
int n1 = scanner.nextInt(); //18
int n2 = scanner.nextInt();//12
int n3 = n1 % n2;//6
while (n3 != 0){
n1 = n2;//12
n2 = n3;//6
n3 =n1 % n2;//0
}
System.out.println(n2);//6
}
16、//输出1/1-1/2+1/3+...-1/100的值
public static void main25(String[] args) {
int f = 1;
double sum = 0;
for(int i = 1 ; i <= 100 ; i++ ){
double a = 1.0 / i*f;
sum += a;
f = -f;
}
System.out.println(sum);
}
17、//输出三位数每一位的数字
public static void main26(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a = n % 10;
int b =( n / 10 ) % 10;
int c = (n / 100) % 10;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
18、 //打印X图形
public static void main27(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入打印行数:");
int num = scanner.nextInt();
int i = 0;
int j = 0;
for ( i = 1 ; i <= num ; i++) {
for ( j = 1 ; j <= num ; j++ ){
if (j == i || j + i == num + 1){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
19、 //0~999中输出水仙花
public class Test {
public static void main28(String[] args) {
int n = 0;
for (n = 0; n <= 999; n++) {
int a = n % 10;
int b = (n / 10) % 10;
int c = (n / 100) % 10;
double d = Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3);// Math.pow(a, 3):a的三次方
if (n == d) {
System.out.println(n);
}
}
}