Fork me on GitHub

Java经典逻辑编程三

求1+2!+3!+…+20!的和

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
import java.util.Scanner;
public class class_name {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("你想计算到哪个数的阶乘的和");
int num = in.nextInt();
long temp = 0L;
long sum = 0L;
for(int i = 1; i <= num; i++){
temp = recursion(i);
sum += temp;
}
System.out.println("计算到" + num + "的阶乘和是 " + sum);
}
//计算某个数 num 的阶乘
private static long recursion(int num) {
// TODO Auto-generated method stub
if(num > 1){
return num * recursion(num - 1);
}
return 1;
}
}

给一个不多于5位的正整数,要求: 一、求它是几位数;二、逆序打印出各位数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
public class class_name {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个不多于5位的整数");
int num = in.nextInt();
int n = 1;
System.out.print("这个数的逆序为:");
while(num / 10 > 0){
n++;
int tmp = num % 10;
System.out.print(tmp);
num /= 10;
}
System.out.println(num);
System.out.println("这个数共有" + n + "位数");
}
}

请输入星期几的第一个字母来判断以下是星期几,如果第一个字母一样,则继续判断第二个字符。

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
public class class_name {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char firstletter, secondletter; //存放第一第二个字母
System.out.println("请输入第一字母");
String letter = in.next();
firstletter = letter.charAt(0);
switch(firstletter){
case 'm':
case 'M':
System.out.println("是星期一");break;
case 'w':
case 'W':
System.out.println("是星期三");break;
case 'f':
case 'F':
System.out.println("是星期五");break;
case 't':
case 'T':
System.out.println("仅由该字母无法判断出星期几,请输入第二个字母");
letter = in.next();
secondletter = letter.charAt(0);
if(secondletter == 'u' || secondletter == 'U'){
System.out.println("是星期二");
break;
}else if(secondletter == 'h' || secondletter == 'H'){
System.out.println("是星期四");
break;
}else{
System.out.println("输入错误");
}
case 's':
case 'S':
System.out.println("仅由该字母无法判断出星期几,请输入第二个字母");
letter = in.next();
secondletter = letter.charAt(0);
if(secondletter == 'a' || secondletter == 'A'){
System.out.println("是星期六");
break;
}else if(secondletter == 'u' || secondletter == 'U'){
System.out.println("是星期天");
break;
}else{
System.out.println("输入错误");
}
}
}
}

求一个3*3矩阵对角线元素之和

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
import java.util.Scanner;
public class class_name {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//输入数据
int[][] mat = new int[3][3];
for(int i = 1; i <= 3; i++){
for(int j = 1; j <= 3; j++){
System.out.println("请输入第" + i + "行第" + j + "列的整数");
mat[i -1][j - 1] = in.nextInt();
}
}
//显示数据
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++)
System.out.printf("%d\t",mat[i][j]); // \t是制表符
System.out.println();
}
//计算对角线元素和
//从左上至右下的数归为主对角线,从左下至右上的数归为副对角线。
int sum1 = mat[0][0] + mat[1][1] + mat[2][2];
int sum2 = mat[2][0] + mat[1][1] + mat[0][2];
System.out.println("主对角线上元素和为 " + sum1);
System.out.println("副对角线上元素和为 " + sum2);
}
}

有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

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
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class class_name {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] ary = {10, 15, 20, 25, 30, 35, 40, 45, 50};
int[] changedAry = new int[ary.length + 1];
System.out.print("初始数组:");
for(int i = 0; i < ary.length; i++){
changedAry[i] = ary[i];
System.out.print(ary[i] + " ");
}
System.out.println();
System.out.println("请输入一个整数");
int num = in.nextInt();
System.out.print("插入数字后的数组:");
insort(changedAry, num);
}
private static void insort(int[] changedAry, int num) {
// TODO Auto-generated method stub
for(int i = 0; i < changedAry.length; i++){
if(num < changedAry[i]){ //找到插入位置
for(int j = changedAry.length - 1; j > i;j--){
changedAry[j] = changedAry[j - 1]; //待插入位置及以后的数后退一个位置
}
changedAry[i] = num;
break;
}
}
if(num > changedAry[changedAry.length - 1]) //待插入数大于所有数
changedAry[changedAry.length - 1] = num;
for(int k = 0; k < changedAry.length; k++)
System.out.print(changedAry[k] + " ");
}
}

将一个数组逆序输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class class_name {
public static void main(String[] args) {
//这里直接初始化一个数组
int[] ary = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// String[] ary = {"i", "love", "you"}; //字符(串)数组也可以
System.out.print("数组初始顺序为:");
for(int i = 0; i < ary.length; i++)
System.out.print(ary[i] + " ");
System.out.println(); //换行
System.out.print("数组的逆序为:");
for(int i = ary.length - 1; i >= 0; i--)
System.out.print(ary[i] + " ");
System.out.println();
}
}

取一个整数a从右端开始的4~7位。

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Scanner;
public class class_name {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个大于等于 7 位的整数");
long sum = in.nextLong();
Long sum1 = new Long(sum); //Long是 long 的包装类
String sum2 = sum1.toString();
System.out.println(sum2.substring(sum2.length() - 7, sum2.length() - 3));
} // substring 截取两参数间的字符串
}

打印出杨辉三角形

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
public class class_name {
public static void main(String[] args) {
int i, j;
int[][] num = new int[10][10];
for (i = 0; i < 10; i++) {// 把所有的 1 放入数组
num[i][0] = 1;
num[i][i] = 1;
}
for (i = 2; i < 10; i++) {// 计算数组的其他数
for (j = 1; j < i; j++) {
num[i][j] = num[i - 1][j - 1] + num[i - 1][j];
}
}
for (i = 0; i < 10; i++) {
// k < 4 倍的空格是因为输出每个数字时用了 4 个占位符
for (int k = 0; k < 4 * (10 - i) / 2; k++) // 输出空格
System.out.printf(" ");
for (j = 0; j <= i; j++) {
System.out.printf("%4d", num[i][j]); // 每个数字 4 个占位符
}
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
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class class_name {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入数组长度");
int arylen = in.nextInt();
int[] num = new int[arylen];
for(int i = 0; i < num.length; i++) {
System.out.printf("请输入第 %d 个数:", i + 1);
num[i] = in.nextInt();
}
System.out.println("变化前的数组:");
outArray(num);
//循环找出最大值、最小值
int max = num[0];
int min = num[0];
for(int i = 1; i < num.length; i++) {
if(num[i] > max)
max = num[i];
if(num[i] < min)
min = num[i];
}
//替换
num[0] = max;
num[num.length - 1] = min;
System.out.println("变化后的数组:");
outArray(num);
}
private static void outArray(int[] num) {
// TODO Auto-generated method stub
for(int i = 0; i < num.length; i++)
System.out.print(num[i] + " ");
System.out.println();
}
}

有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。

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
import java.util.Scanner;
public class class_name {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] num = {14, 32, 5, 42, 36, 80};
int[] temp = new int[num.length];
System.out.println("移动前的数组:");
for(int i = 0; i < num.length; i++){
temp[i] = num[i];
System.out.print(num[i] + " ");
}
System.out.println();
System.out.println("需要向后移动几个位置");
int m = in.nextInt();
for(int i = 0; i < num.length; i++){
int t = (i + m) % num.length;
num[t] = temp[i]; //这行的 t 和 i 互换一下就变成了(1)
}
System.out.println("移动后的数组:");
for(int i: num)
System.out.print(i + " ");
System.out.println();
}
}
Your support will encourage me to continue to create!