代码
if-else语句
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
int x = 0;
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
if (x <= 1) {
System.out.println(x);
} else if (x > 1 & x < 10) {
System.out.println(3 * x - 2);
} else {
System.out.println(4 * x);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for循环语句
1+3+7+…+(2n-1)
/*
- 1+3+7+…+(2^n-1)
/
public class demo02 {
public static void main(String[] args) {
int sum = 0;
int a = 1;
for (int i = 1; i <= 20; i++) {
// int a=0;
// a=a2+1;
// sum=sum+a;
a = a * 2;
sum = sum + a - 1;
}
System.out.println(sum);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
输出1+2+4+…+10000(忽略末位数为3的数)
/*
- 1+2+4+…+10000忽略末位数为3的数
/
public class demo03 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10000; i++) {
if (i % 10 == 3) {
continue;
}
sum = sum + i;
}
System.out.println(sum);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
打印金字塔
import java.util.Scanner;
/ - 打印金字塔
/
public class demo04 {
public static void main(String[] args) {
int count=0;
Scanner sc = new Scanner(System.in);
System.out.print(“请输入你要打印的排数:”);
count = sc.nextInt();
for(int i=1;i<=count ;i++){
for(int j=0;j<=count-i;j++){
System.out.print(" ");
}
for(int j=1;j<=2i-1;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
打印九九乘法表
/*
- 打印九九乘法表
/
public class demo05 {
public static void main(String[] args) {
System.out.println(“九九乘法表”);
for(int i=1;i<=9 ;i++){
for(int j=1;j<=i;j++){
System.out.print(j+“x”+i+"="+ij+"\t");
}
System.out.println(" ");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
输出1—1000以内的素数
/* - 输出1——1000以内的素数
*/
public class demo06 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
int count = 0;// 被整除的次数
for (int j = 1; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
count++;
}
}
if (count <= 2) {
System.out.println(i);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
另外一种比较好的方法:
public class demo07 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
boolean isSushu=true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isSushu =false;
break;
}
}
if (isSushu) {
System.out.println(i);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
求1—1000以内的完全数
/*
- 求1——1000以内的完全数
/
public class demo08 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
int sum = 0;
for (int j = 1; j <i; j++) {
if (i % j == 0) {
sum = sum + j;// 此时j是i的因子
}
}
if (sum == i) {
System.out.println(i);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
生兔子问题
/ - 现有一对成熟的兔子,一对成熟的兔子每个月生一对兔子,小兔子三个月长成为成熟的兔子
- 求20个月后的兔子总对数
*/
public class demo09 {
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = 3;
int temp;
for (int i =4; i <= 20; i++) {
temp = a + c;
a = b;
b = c;
c = temp;
System.out.println(“第”+i+“个月生的兔子对数为”+temp+“对”);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
拓展
打印实心菱形
import java.util.Scanner;
public class demo11 {
public static void main(String[] args) {
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.print(“请输入你要打印的总行数:”);
count = sc.nextInt();
System.out.println("实心菱形:");//count为总行数,i为第几行数
for (int i = 1; i <= count; i++) {
if (i <= count / 2 + 1) {
for (int j = 1; j <= count / 2 + 1 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
} else {
for (int j = 1; j <= (i - (count / 2 + 1)); j++){
System.out.print(" ");
}
for(int j = 1; j <= (2*(count / 2 + 1) - i);j++){
System.out.print("* ");
}
System.out.println();
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
打印空心菱形
import java.util.Scanner;
public class demo10 {
public static void main(String[] args) {
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.print(“请输入你要打印的总行数:”);
count = sc.nextInt();
System.out.println("空心菱形:");//i为第几行数,count为总行数
for (int i = 1; i <= (count / 2 + 1); i++) {
for (int j = 1; j <= ((count / 2 + 1) - i); j++) {
System.out.print(" ");
}
System.out.print("*");
for (int j = 1; j <= ((i - 2) * 2 + 1); j++) {
System.out.print(" ");
}
if (i != 1) {
System.out.print("*");
}
System.out.println();
}
for (int i = (count / 2); i >= 1; i--) {
for (int j = 1; j <= ((count / 2 + 1) - i); j++) {
System.out.print(" ");
}
System.out.print("*");
for (int j = 1; j <= ((i - 2) * 2 + 1); j++) {
System.out.print(" ");
}
if (i != 1) {
System.out.print("*");
}
System.out.println();
}
}
}
作者:木大白易
来源:CSDN
原文:https://blog.csdn.net/aiynmimi/article/details/46917077