输入两个正整数 m 和 n,求其最大公约数和最小公倍数
1 /**在循环中,只要除数不等于 0,用较大数除以较小的数,将小的一个数作为下一轮循环的
2 大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为 0,返回较大
3 的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。* /
4 package com.day2;
5 import java.util.*;
6 public class test6 {
7 public static Scanner input = new Scanner(System.in);
8 public static void main(String[] args) {
9 System.out.println("请输入两个数来求最大公约数和最小公倍数:");
10 System.out.println("第一个数:");
11 int a = input.nextInt();
12 System.out.println("第二个数:");
13 int b = input.nextInt();
14 Deff cd = new Deff();
15 int x = cd.deff(a, b);//调用函数找到最大公约数
16 int y = a * b / x;//两个数的积除以最大公约数就是最小公倍数
17 System.out.println("最大公约数为:"+x);
18 System.out.println("最小公倍数为:"+y);
19 }
20
21 }
22 class Deff{
23 public int deff(int a, int b)
24 {
25 if(a < b)
26 {
27 a = a ^ b;
28 b = a ^ b;
29 a = a ^ b;
30 }
31 while(b != 0)
32 {
33 if(a == b)
34 return a;
35 else
36 {
37 int k = a % b;
38 a = b ;
39 b = k;
40 }
41 }
42 return a;
43 }
44 }
test6
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
1 package com.day2;
2 import java.util.*;
3 public class test7 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 System.out.println("请输入一行字符串:");
7 String str = input.nextLine();
8 int digital = 0,character = 0, other = 0, blank = 0;
9 char [] ch = str.toCharArray();//String的方法,将字符串转换为字符数组;
10 for (int i = 0; i <ch.length; i++) {
11 if(ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z')
12 character++;
13 else if(ch[i] >= '0' && ch[i] <= '9')
14 digital++;
15 else if(ch[i] == ' ')
16 blank++;
17 else
18 other++;
19 }
20 System.out.println("字母个数:"+character);
21 System.out.println("数字个数:"+digital);
22 System.out.println("空格个数:"+blank);
23 System.out.println("其他个数:"+other);
24 }
25 }
test7
求 s=a+aa+aaa+aaaa+aa...a 的值,其中 a 是一个数字。例如 2+22+222+2222+22222(此时共有 5 个数相加),几个数相加由键盘控制
1 package com.day2;
2 import java.util.*;
3 public class test8 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 System.out.println("请输入个位数字:");
7 int single = input.nextInt();
8 System.out.println("请输入最高位数:");
9 int max = input.nextInt();
10 int sum = 0,temp = 0;
11 for (int i = 0; i < max; i++) {
12 temp = single + temp; //先把本次要加的值赋值给temp;
13 single *= 10; //每次把单数乘以10,向前进一位,加上之前的temp正好满足需要
14 sum = sum + temp; //把每次的temp相加起来就是要的结果
15 }
16 System.out.println("数字"+single+"公共有"+max+"个数相加的好结果为:"+sum);
17 }
18 }
test8
一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如 6=1+2+3.编程找出 1000 以内的所有完数
1 package com.day2;
2 public class test9 {
3 public static void main(String[] args) {
4 for (int i = 1; i <= 1000; i++) {
5 int b = 0;//每次都要把b重置
6 for (int j = 1; j <= i/2; j++) {
7 if(i % j == 0)//找到因数,然后相加
8 {
9 b = b + j;//相加供后边使用
10 }
11 }
12 if(i == b)//如果是完数,则输出完数
13 System.out.println(i);
14 }
15 }
16 }
test9
一球从 100 米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10 次落地时,共经过多少米?第 10 次反弹多高?
1 package com.day2;
2 import java.util.*;
3 public class test10 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 System.out.println("请输入第几次?");
7 int num = input.nextInt();
8 double sum = 0, high = 100;
9 for (int i = 1; i < num; i++) {
10 if(i == 1)
11 sum += high;
12 else
13 sum = sum + 2*high;
14 if(i < 10)
15 high /= 2;
16 }
17 System.out.println("第"+num+"次时经过"+sum+"米,第"+num+"次反弹"+high+"米!");
18 }
19 }
test10
1 package com.day2;
2 import java.util.*;
3 public class test10 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 System.out.println("请输入第几次?");
7 int num = input.nextInt();
8 double sum = 100, high = 100;
9 for (int i = 1; i < num; i++) {//执行9次
10 sum += high;
11 high /= 2;
12 }
13 System.out.println("第"+num+"次时经过"+sum+"米,第"+num+"次反弹"+high+"米!");
14 }
15 }
test10
有 1、 2、 3、 4 四个数字, 能组成多少个互不相同且无重复数字的三位数?都是多少?
1 package com.day3;
2 public class test11 {
3 public static void main(String[] args) {
4 int count = 0 ;
5 for (int i = 1; i < 5; i++) {//最外层循环,控制百位数;
6 for (int j = 1; j < 5; j++) {//第二层循环控制十位数;
7 for (int z =1; z < 5; z++) {//第三层循环控制个位数;
8 if(i!=j&&i!=z&&j!=z)//如果三个位上的值互不相等,执行计数操作;
9 {
10 count++;
11 System.out.println(i*100+j*10+z);
12 }
13 }
14 }
15 }
16 System.out.println("共有"+count+"个这样的数!");
17 }
18 }
test11
企业发放的奖金根据利润提成。利润(I)低于或等于 10 万元时,奖金可提 10%;利润高于 10 万元,低于 20 万元时,低于 10 万元的部分按 10%提成,高于 10 万元的部分,可可提成 7.5%;20 万到 40 万之间时,高于 20 万元的部分,可提成 5%;40 万到 60 万之间时高于 40 万元的部分, 可提成 3%; 60 万到 100 万之间时, 高于 60 万元的部分, 可提成 1.5%,高于 100 万元时,超过 100 万元的部分按 1%提成,从键盘输入当月利润,求应发放奖金总数?
1 package com.day3;
2 import java.util.*;
3 public class test12 {
4 public static void main(String[] args) {
5 double x = 0,y = 0;
6 System.out.print("输入当月利润(万) :");
7 Scanner s = new Scanner(System.in);
8 x = s.nextInt();
9 if(x > 0 && x <= 10) {
10 y = x * 0.1;
11 } else if(x > 10 && x <= 20) {
12 y = 10 * 0.1 + (x - 10) * 0.075;
13 } else if(x > 20 && x <= 40) {
14 y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05;
15 } else if(x > 40 && x <= 60) {
16 y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03;
17 } else if(x > 60 && x <= 100) {
18 y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x - 60) * 0.015;
19 } else if(x > 100) {
20 y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01;
21 }
22 System.out.println("应该提取的奖金是 " + y + "万");
23 }
24 }
test12
一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少?
1 package com.day3;
2 public class test13 {
3 public static void main(String[] args) {
4 long startTime = System.currentTimeMillis(); //获取执行开始时间
5 int i = 0;
6 while(true)
7 {
8 if(Math.sqrt(i+100) % 1 == 0)
9 if(Math.sqrt(i+100+168) % 1 ==0)
10 {
11 System.out.println(i+"加上100或者168都是完全平方数!");
12 }
13 i++;
14 if(i > 10000)
15 break;
16 }
17 long endTime = System.currentTimeMillis(); //获取执行结束时间
18 System.out.println("time:" + (endTime - startTime)); //打印程序执行时间
19 }
20 }
21
22
23 程序也可参考以下地址的写法:http://blog.****.net/yueqinglkong/article/details/22805293
test13
输入某年某月某日,判断这一天是这一年的第几天?
1 package com.day3;
2 import java.util.Scanner;
3 public class test14 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 int day , month ,year , dayNum = 0;//定义年月日,以及本月之前的总天数
7 while(true)
8 {
9 System.out.println("请输入年:");
10 year = input.nextInt();
11 System.out.println("请输入月:");
12 month = input.nextInt();
13 System.out.println("请输入日:");
14 day = input.nextInt();
15 if(month < 1 || month > 12 || day < 1 || day > 31)
16 continue;
17 else
18 break;
19 }
20 for(int i =1; i < month; i++)//通过循环来找到本月之前的总天数;判断月的总天数和闰年等
21 {
22 int days = 0;
23 switch(i)
24 {
25 case 1:
26 case 3:
27 case 5:
28 case 7:
29 case 8:
30 case 10:
31 case 12:
32 days = 31;
33 break;
34 case 4:
35 case 6:
36 case 9:
37 case 11:
38 days = 30;
39 break;
40 case 2://闰年29天,非闰年28天
41 if(year % 400 ==0||(year%4 == 0 && year % 100 != 0))
42 days = 29;
43 else
44 days = 28;
45 break;
46 }
47 dayNum += days;//输入月份之前月份的总天数
48 }
49 System.out.println("这是本年的第"+(dayNum+day)+"天");
50 }
51 }
test14
输入三个整数 x,y,z,请把这三个数由小到大输出。
1 package com.day3;
2 import java.util.Arrays;
3 import java.util.Scanner;
4 public class test15 {
5 public static Scanner input = new Scanner(System.in);
6 public static void main(String[] args) {
7 long startTime = System.currentTimeMillis(); //获取执行开始时间
8 sort(44,35,37);
9 long endTime = System.currentTimeMillis(); //获取执行结束时间
10 System.out.println("time:" + (endTime - startTime)); //打印程序执行时间
11 }
12 public static void sort(int a, int b, int c)
13 {
14 if(a > b)
15 {
16 a = a ^ b ;
17 b = a ^ b ;
18 a = a ^ b ;
19 }
20 if(a > c)
21 {
22 a = a ^ c ;
23 c = a ^ c ;
24 a = a ^ c ;
25 }
26 if(b > c)
27 {
28 b = b ^ c ;
29 c = b ^ c ;
30 b = b ^ c ;
31 }
32 System.out.println("从小到大的顺序是:"+a+"<"+b+"<"+c);
33 }
34 public static void sort1(int a, int b, int c)
35 {
36 int arr[] = {a,b,c};
37 Arrays.sort(arr);
38 System.out.println("从小到大依次是:");
39 for (int i = 0; i < arr.length; i++) {
40 System.out.print(arr[i]);
41 System.out.print(' ');
42 }
43 }
44 }
test15
输出 9*9 口诀。
1 package com.day4;
2 public class test16 {
3 public static void main(String[] args) {
4 for (int i = 1; i < 10 ; i++) {
5 for (int j = 1; j <= i ; j++) {
6 System.out.print(j+"*"+i+"="+i*j+" ");
7 }
8 System.out.println();
9 }
10 }
11 }
test16
猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下 的一半零一个。 到第10天早上想再吃时, 见只剩下一个桃子了。 求第一天共摘了多少。
1 package com.day4;
2 public class test17 {
3 public static void main(String[] args) {
4 // TODO Auto-generated method stub
5 int num = 1;
6 for (int i = 9; i >= 1; i--) {
7 num = (num + 1) * 2;
8 }
9 System.out.println("猴子第一天摘的桃子的个数是:"+num);
10 }
11
12 }
test17
两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a 说他不和 x 比,c 说他不和 x,z 比,请编程序找出三队赛手的名单。
1 package com.day4;
2 public class test18 {
3 static char[] m ={'a','b','c'};//把要处理的字符放进字符数组中便于处理;
4 static char[] n ={'x','y','z'};
5 public static void main(String[] args) {
6 for (int i = 0; i < m.length; i++) {//外层循环遍历甲队队员,
7 for (int j = 0; j < n.length; j++) {//内层循环遍历乙队队员,
8 if(m[i] == 'a' && n[j] == 'x')
9 continue;
10 //根据题意知道c对战y,a不可能对战y;
11 else if(m[i] == 'a' && n[j] == 'y')
12 continue;
13 //根据题意;
14 else if((m[i] == 'c' && n[j] == 'x' ) || (m[i] == 'c' && n[j] == 'z'))
15 continue;
16 //推测出b不可能对战y和z;
17 else if((m[i] == 'b' && n[j] == 'y' ) || (m[i] == 'b' && n[j] == 'z'))
18 continue;
19 else
20 System.out.println(m[i] +"对战"+n[j]);
21 }
22 }
23 }
24 }
test18
打印出如下图案(菱形)
1 package com.day4;
2 import java.util.*;
3 public class test19 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 System.out.println("请输入你要显示的总行数(奇数):");
7 int num = input.nextInt();
8 for (int i = 1; i <= (num+1) / 2; i++) {//此循环是控制上层的三角的,包括最中间的一行;
9 for (int j = 0; j < (num+1) / 2 -i ; j++) {//控制每一行的空格数
10 System.out.print(" ");
11 }
12 for (int j = 0; j < 2*i - 1; j++) {//控制每一行显示的*符号数
13 System.out.print("*");
14 }
15 System.out.println();//换行
16 }
17 for (int i = 1; i <= (num -1 ) / 2; i++) {//此循环是控制下层的三角的
18 for (int j = 0; j < i ; j++) {//控制每一行的空格数
19 System.out.print(" ");
20 }
21 for (int j = 0; j < num - 2*i; j++) {//控制每一行显示的*符号数
22 System.out.print("*");
23 }
24 System.out.println();//换行
25 }
26 }
27 }
test19
有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前 20 项之和
1 package com.day4;
2 import java.util.Scanner;
3 public class test20 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 int x = 2 , y = 1;
7 double sum = 0;
8 for (int i = 1; i <= 20; i++) {//根据之间的规律来逐项想加
9 sum = sum + (double)x / y;
10 x = x ^ y;
11 y = x ^ y;
12 x = x ^ y;
13 x = x + y;
14 }
15 System.out.println("前20项想加之和为:"+sum);
16 }
17
18 }
test20
求 1+2!+3!+...+20!的和
1 package day5;
2 public class test21 {
3 public static void main(String[] args) {
4 long sum = 0 ;long temp = 1;//必须要设置为long类型,不然超过范围;
5 for (int i = 1; i <= 20; i++) {
6 temp = 1;
7 for (int j = 1; j <= i; j++) {
8 temp *= j;
9 }
10 sum += temp;
11 }
12 System.out.println(sum);
13 }
14 }
test21
利用递归方法求 5!;
1 package day5;
2 public class test12 {
3 public static void main(String[] args) {
4 System.out.println(rec(5));
5 }
6 public static long rec(int n) {//定义函数实现递归
7 long value = 0 ;
8 if(n ==1 )
9 {
10 value = 1;
11 }
12 else
13 {
14 value = n * rec(n-1);
15 }
16 return value;
17 }
18 }
test22
有 5 个人坐在一起,问第五个人多少岁?他说比第 4 个人大 2 岁。问第 4 个人岁数,他说比第 3 个人大 2 岁。问第三个人,又说比第 2 人大两岁。问第 2 个人,说比第一个人大两岁。最后问第一个人,他说是 10 岁。请问第五个人多大?
1 package day5;
2 public class test23 {
3 public static void main(String[] args) {
4 int age = 10;//第一个人的年龄
5 for (int i = 1; i <= 4; i++) {//依次从第一个人加到第五个人
6 age += 2;
7 }
8 System.out.println("第五个人"+age+"岁");
9 }
10 }
test23
给一个不多于 5 位的正整数, 要求: 一、 求它是几位数, 二、 逆序打印出各位数字。
1 package day5;
2 import java.util.Scanner;
3
4 public class test24 {
5 public static Scanner input = new Scanner(System.in);
6 public static void main(String[] args) {
7 System.out.println("请输入一个不多于五位数的数字:");
8 Integer num = input.nextInt();//定义Integer类型变量,便于转换成数组;
9 String numString = Integer.toString(num);//利用Integer的方法转换成字符串;
10 char [] arrChar = numString.toCharArray();//利用字符串的方法转换成字符数组,便于求长度和输出
11 System.out.println("您输入的是"+arrChar.length+"位数");
12 for (int i = 0; i < arrChar.length; i++) {
13 System.out.println("第"+(i+1)+"个数字是"+arrChar[i]);
14 }
15 System.out.println("逆序打印:");
16 for (int i = arrChar.length - 1; i >= 0; i--) {
17 System.out.print(arrChar[i]);
18 }
19 }
20 }
test24
一个 5 位数,判断它是不是回文数。即 12321 是回文数,个位与万位相同,十位与千位相同。
1 package day5;
2 import java.util.Scanner;
3 public class test25_1 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 boolean isHuiWen = false;
7 System.out.println("请输入一个数是不是回文数:");
8 Integer num = input.nextInt();
9 char[] arrChar = num.toString().toCharArray();//像上一题一样,利用字符数组解决
10 for (int i = 0; i < arrChar.length / 2; i++) {
11 if (arrChar[i] == arrChar[arrChar.length - i - 1]) {
12 isHuiWen = true;
13 }else {
14 isHuiWen = false;
15 }
16 }
17 if (isHuiWen) {
18 System.out.println("这个数是回文数!");
19 }else {
20 System.out.println("这个数不是回文数!");
21 }
22 }
23 }
不限制位数
请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
1 package com.day6;
2 import java.util.Scanner;
3 public class test26 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 System.out.println("请输入一个字符串:");
7 String str = input.nextLine().toUpperCase();//将输入的都转换成大写
8 switch(str.charAt(0))//利用字符串的charAt方法,取得字符串的第一个字符
9 {
10 case 'M':
11 System.out.println("Monday");
12 break;
13 case 'W':
14 System.out.println("Wednesday");
15 break;
16 case 'F':
17 System.out.println("Friday");
18 break;
19 case 'T': {//利用字符串的charAt方法,取得字符串的第二个字符
20 if(str.charAt(1)== 'U') {System.out.println("Tuesday"); }
21 else if(str.charAt(1)== 'H') {System.out.println("Thursday"); }
22 else {System.out.println("无此写法!");
23 }
24 };
25 break;
26 case 'S': {
27 if(str.charAt(1) == 'U') {System.out.println("Sunday"); }
28 else if(str.charAt(1) == 'A') {System.out.println("Saturday"); }
29 else {System.out.println("无此写法!");
30 }
31 };
32 break;
33 default:System.out.println("无此写法!");
34 }
35 }
36 }
test26
求 100 之内的素数
1 package com.day6;
2
3 public class Test27 {
4 public static void main(String[] args) {
5 System.out.print("2 3 ");
6 boolean is = false;
7 for (int i = 4; i <= 100; i++) {
8 for (int j = 2; j <= Math.sqrt(i); j++) {
9 if(i % j == 0)//不是素数,找下一个数
10 {
11 is = false;
12 break;
13 }
14 else//是素数,设为true;输出此数
15 is = true;
16 }
17 if (is == true) {
18 System.out.print(i+" ");
19 }
20 }
21 }
22 }
Test27
对 10 个数进行排序
1 package com.day6;
2
3 import java.lang.reflect.Array;
4 import java.util.Arrays;
5 import java.util.Scanner;
6 public class Test28 {
7 public static Scanner input = new Scanner(System.in);
8 public static void main(String[] args) {
9 System.out.println("请输入您要输入的个数");
10 int num = input.nextInt();
11 int [] arrInt = new int[num];
12 System.out.println("输入"+num+"位数进行排序:");
13 for (int i = 0; i < num; i++) {
14 arrInt[i] = input.nextInt();
15 }
16 //Arrays.sort(arrInt);//利用自带的排序函数进行排序
17 sort(arrInt);//自定义函数进行排序
18 for (int i = 0; i < arrInt.length; i++) {
19 System.out.println(arrInt[i]);
20 }
21 }
22 public static int[] sort(int [] arr)
23 {
24 for (int i = 0; i < arr.length; i++) {
25 for (int j = i; j < arr.length; j++) {
26 if(arr[i] > arr[j])
27 {
28 arr[i] = arr[i] ^ arr[j];
29 arr[j] = arr[i] ^ arr[j];
30 arr[i] = arr[i] ^ arr[j];
31 }
32 }
33 }
34 return arr;
35 }
36 }
Test28
求一个 3*3 矩阵对角线元素之和
1 package com.day6;
2 import java.util.Scanner;
3 public class Test29 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 int sum = 0;
7 System.out.println("请输入9个整数以求对角线之和");
8 int [][] arrInt = new int[3][3];
9 for (int i = 0; i < arrInt.length; i++) {
10 for (int j = 0; j < arrInt.length; j++) {
11 arrInt[i][j] = input.nextInt();
12 }
13 }
14 System.out.println("您输入的9位数矩阵为:");
15 for (int i = 0; i < arrInt.length; i++) {
16 for (int j = 0; j < arrInt.length; j++) {
17 System.out.print(arrInt[i][j]+" ");
18 }
19 System.out.println();
20 }
21 for (int i = 0; i < arrInt.length; i++) {
22 for (int j = 0; j < arrInt.length; j++) {
23 if (i == j || i == arrInt.length - 1 - j ) {
24 sum += arrInt[i][j];
25 }
26 if (i == 1 && j == 1) {//最中间的那个数少加一次,要记得加上,如果不是9位矩阵,则需改变
27 sum += arrInt[i][j];
28 }
29 }
30 }
31 System.out.println("对角线之和为:"+sum);
32 }
33 }
Test29
有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
1 package com.day6;
2 import java.util.Scanner;
3 public class Test30 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 int[] a = new int[]{1, 2, 6, 14, 25, 36, 37,55};
7 int[] b = new int[a.length+1];
8 int i =0;
9 System.out.print("请输入一个整数:");
10 int num = input.nextInt();
11 if(num >= a[a.length-1])
12 {//如果大于最大数,直接加在最后
13 b[b.length-1] = num;
14 for(i=0; i<a.length; i++)
15 {//把a数组复制给b数组
16 b[i] = a[i];
17 }
18 }
19 else
20 {//如果不大于最大数
21 for(i=0; i<a.length; i++)
22 {
23 if(num >= a[i])
24 {//如果次数大于当前的数
25 b[i] = a[i];//加在b对应的的位置
26 }
27 else
28 {
29 b[i] = num;
30 break;
31 }
32 }
33 for(int j=i+1; j<b.length; j++)
34 {//a中的i后边元素都在b中往后移一个位置
35 b[j] = a[j-1];
36 }
37 }
38 for (i = 0; i < b.length; i++)
39 {//输出数组
40 System.out.print(b[i] + " ");
41 }
42 }
43 }
Test30
将一个数组逆序输出
1 package com.day7;
2 import java.util.Scanner;
3 public class Test31 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 int [] arr = new int [100];//初始化定义数组,默认长度为100;
7 System.out.println("请输入多个正整数(输入-1结束):");
8 int i = 0;//定义i是为了知道数组中有多少个元素;
9 do//用户do while循环是为了控制数组输入的结束;
10 {
11 arr[i] = input.nextInt();
12 i++;
13 }while(arr[i-1] != -1);//第一次到这里的时候,i已经是1,所以可以减去1
14 System.out.println("您输入的数组是:");
15 for (int j2 = 0; j2 < i-1; j2++) {//顺序输入刚才输入的数组
16 System.out.println(arr[j2]+ " ");
17 }
18 System.out.println("您输入的数组逆序输出为:");
19 for (int j2 = 0; j2 < i-1; j2++) {//逆序输入刚才输入的数组
20 System.out.println(arr[i-2-j2] + " ");
21 }
22 }
23 }
Test31
取一个整数 a 从右端开始的 4~7 位
1 package com.day7;
2 import java.util.Scanner;
3 public class Test32 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 System.out.println("请输入一个大于7位数的整数:");
7 long num = input.nextLong();//定义数值类型是long类型,防止越界
8 String str = Long.toString(num);//将long类型转换成字符串
9 char[] charStr = str.toCharArray();//利用字符串的方法转换为字符数组
10 int length = charStr.length;
11 if (length < 7) {//容错判断
12 System.out.println("您输入的整数长度有误!");
13 }
14 else {//如果输入正确,输入该整数的倒数4-7位
15 System.out.println("您输入的整数从右端开始的4-7位分别是:"+
16 charStr[length-4] +" "+charStr[length-5]+" "
17 +charStr[length-6]+" "+charStr[length-7]);
18 }
19 }
20 }
Test32
打印出杨辉三角形(手动选择要打印的行数)
1 package com.day7;
2 import java.util.Scanner;
3 public class Test33 {
4 public static void main(String[] args) {
5 Scanner input = new Scanner(System.in);
6 System.out.println("请输入要显示的杨辉三角的行数:");
7 int num = input.nextInt();//获得要显示的行数
8 int[][] arr = new int[num][num];//定义二维数组,存储要显示的数字
9 for (int i = 0; i < arr.length; i++) {
10 arr[i][i] = 1;//确定每行最后的数字
11 arr[i][0] = 1;//确定每行开始的数字
12 }
13 for (int i = 2; i < arr.length; i++)
14 {//获取每一行的开始和结束的数字
15 for (int j = 1; j < i; j++) {
16 arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
17 }
18 }
19 for (int i = 0; i < arr.length; i++)
20 {//打印出二维数组
21 for (int j = 0; j < 2*(arr.length-i)-1; j++)
22 {//控制每一行的最前面显示的空格数
23 System.out.print(" ");
24 }
25 for (int j = 0; j <= i; j++)
26 {//打印出数组中的元素,并且以空格隔开
27 System.out.print(arr[i][j]+" ");
28 }
29 System.out.println();//每次打印一行结束之后换行;
30 }
31
32 }
33 }
Test33
输入 3 个数 a,b,c,按大小顺序输出。
1 package com.day7;
2 import java.util.Arrays;//引入Arrays,获取排序方法
3 import java.util.Scanner;
4 public class Test34 {
5 public static void main(String[] args) {
6 Scanner input = new Scanner(System.in);
7 int [] arr = new int[3];
8 System.out.println("请输入三个数字,以按照大小输出:");
9 for (int i = 0; i < arr.length; i++) {
10 arr[i] = input.nextInt();
11 }
12 Arrays.sort(arr);//利用JAVA数组的排序,直接输出数组
13 for (int i = 0; i < arr.length; i++) {
14 System.out.println(arr[i]);
15 }
16 }
17 }
Test34
输入数组, 最大的与第一个元素交换, 最小的与最后一个元素交换, 输出数组。
1 package com.day7;
2 import java.util.Scanner;
3 public class Test35 {
4 public static Scanner input = new Scanner(System.in);
5 public static void main(String[] args) {
6 System.out.println("请输入要多大的数组:");
7 int arrLength = input.nextInt();
8 int [] arr = new int[arrLength];
9 for(int i = 0; i < arrLength; i++)
10 {
11 arr[i] = input.nextInt();//初始化数组
12 }
13 int max = arr[0] , min=arr[0] ,maxIndex = 0,minIndex = 0;
14 for (int i = 1; i < arr.length; i++) {
15 if (max < arr[i]) {//找到数组的最大值索引
16 max = arr[i];
17 maxIndex = i;
18 }
19 else if(min > arr[i]) {//找到数组的最小值索引
20 min = arr[i];
21 minIndex = i;
22 }
23 }
24 if(maxIndex != 0)//如果最大值的索引不是0,交换元素
25 {
26 arr[0] = arr[0] ^ arr[maxIndex];
27 arr[maxIndex] = arr[0] ^ arr[maxIndex];
28 arr[0] = arr[0] ^ arr[maxIndex];
29 }
30 if(minIndex != arrLength - 1 )//如果最大值的索引不是arrLength - 1,交换元素
31 {
32 arr[arrLength - 1] = arr[arrLength - 1] ^ arr[minIndex];
33 arr[minIndex] = arr[arrLength - 1] ^ arr[minIndex];
34 arr[arrLength - 1] = arr[arrLength - 1] ^ arr[minIndex];
35 }
36 for (int i = 0; i < arr.length; i++) {//输出数组
37 System.out.println(arr[i]);
38 }
39 }
40 }
Test35
有n个整数, 使其前面各数顺序向后移m个位置, 最后m个数变成最前面的m个数
1 package com.day8;
2 import java.util.Scanner;
3 public class Test36 {
4 public static void main(String[] args) {
5 Scanner input = new Scanner(System.in);
6 System.out.println("请输入数组的长度:");//定义数组长度
7 int num = input.nextInt();
8 int [] arr = new int[num];
9 System.out.println("请输入数组元素:");//键入数组元素
10 for (int i = 0; i < num; i++) {
11 arr[i] = input.nextInt();
12 }
13 System.out.println("您输入的数组是:");//打印数组
14 for (int j = 0; j < arr.length; j++) {
15 System.out.print(arr[j] + " " );
16 }
17 System.out.println("请输入移动的位数:");//获取移动位数
18 int m = input.nextInt();
19 int [] arr2 = new int[num];
20 for (int k = 0; k < m; k++) {//先把移动的转移进新数组
21 arr2[k] = arr[num - m + k];
22 }
23 for (int k2 = 0; k2 < num - m; k2++) {//把向后移的插入到新数组
24 arr2[m+k2] = arr[k2];
25 }
26 System.out.println("移动后的数组为:");
27 for (int l = 0; l < arr2.length; l++) {
28 System.out.println(arr2[l]);
29 }
30 }
31 }
Test36
有 n 个人围成一圈,顺序排号。从第一个人开始报数(从 1 到 3 报数),凡报到 3的人退出圈子,问最后留下的是原来第几号的那位
1 package com.day8;
2 import java.util.Scanner;
3 public class Test37 {
4 public static void main(String[] args) {
5 Scanner input = new Scanner(System.in);
6 System.out.println("请输入总人数:");//定义数组长度
7 int num = input.nextInt();
8 //定义数组,用其中的元素标记是否已经被淘汰,0表示为被淘汰
9 int [] arr = new int[num];
10 for (int i = 0; i < num; i++) {//初始化数组元素都是1
11 arr[i] = 1;
12 }
13 for (int i = 0; i < arr.length; i++) {
14 System.out.println(arr[i]);
15 }
16 int index = 0;
17 int sum = 0;
18 while(num > 1 )//用来控制剩余的人数
19 {
20 if (arr[index] == 1) {
21 sum++;
22 if (sum == 3) {//如果是3,则重新记,从1开始
23 sum = 0;
24 arr[index] = 0;
25 num-- ;
26 }
27 }
28 index++ ;
29 if (index == arr.length) {//如果索引是数组的长度,则从0开始
30 index = 0 ;
31 }
32 }
33 for (int i = 0; i < arr.length; i++) {
34 System.out.println(arr[i]);
35 }
36 for (int i = 0; i < arr.length; i++) {
37 if (arr[i] == 1) {
38 System.out.println("第"+(i+1)+"留了下来");
39 }
40 }
41 }
42 }
Test37
写一个函数, 求一个字符串的长度, 在 main 函数中输入字符串, 并输出其长度。
1 package com.day8;
2 import java.util.Scanner;
3 public class Test38 {
4 public static void main(String[] args) {
5 Scanner input = new Scanner(System.in);
6 System.out.println("请输入一个字符串:");
7 String str = input.nextLine();
8 System.out.println("该字符串的长度是:"+getArrLength(str));
9 }
10 public static int getArrLength(String str)
11 {
12 char[] charStr = str.toCharArray();
13 return charStr.length;
14 }
15 }
Test38
编写一个函数,输入 n 为偶数时,调用函数求 1/2+1/4+...+1/n,当输入 n 为奇数时,调用函数 1/1+1/3+...+1/n
1 package com.day8;
2 import java.util.Scanner;
3 public class Test39 {
4 public static void main(String[] args)
5 {
6 Scanner s = new Scanner(System.in);
7 System.out.print("请输入一个正整数 n= ");
8 int n = s.nextInt();
9 System.out.println("相应数列的和为:" + sum(n));
10 }
11 public static double sum(int n)
12 {
13 double res = 0;
14 if(n % 2 == 0) {
15 for(int i=2; i<=n; i+=2) {
16 res += (double)1 / i;
17 }
18 } else {
19 for(int i=1; i<=n; i+=2) {
20 res += (double)1 / i ;
21 }
22 }
23 return res;
24 }
25 }
Test39