# JavaTask **Repository Path**: kais1122/JavaTask ## Basic Information - **Project Name**: JavaTask - **Description**: Java程序设计作业 - **Primary Language**: Java - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2017-10-08 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 国庆期间Java作业 1. 一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如,6的因子为1、2、3,而6 = 1 + 2 + 3,因此6是“完数”。编程序找出2000之内的所有完数。 public class Src01 { public static void main(String[] args) { int a; // 定义一个变量用来储存因子之和 System.out.println("2000以内的完数有:"); for (int i = 2; i <= 2000; i++) { // 对2000以内的数进行尝试 a = 0; // 每当开始尝试新的数字时,将a的值初始化 for (int j = 1; j < i; j++) { // 尝试寻找因子 if (i % j == 0) { a += j; } } if (a == i) { // 当因子之和与某数相等时,将该数输出 System.out.println(i); } } } } 2. 打印出2000以内的所有的“水仙花数”。所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如,153是一水仙花数,因为153 = 13 + 53 + 33。 public class Src02 { public static void main(String[] args) { int thousands, hundreds, tens, units; System.out.println("2000以内所有的水仙花数为:"); for (int i = 100; i <= 2000; i++) { thousands = i / 1000; hundreds = i / 100 % 10; tens = i / 10 % 10; units = i % 10; if (i == Math.pow(thousands, 3) + Math.pow(hundreds, 3) + Math.pow(tens, 3) + Math.pow(units, 3)) { System.out.println(i); } } } } 3. 求Sn=a+aa+aaa+…+aa…a之值,其中a是一个数字。例如:2+22+222+…+22222(此时n=5),n由键盘输入。 import java.util.*; public class Src03 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int a, n, sum, sn; System.out.println("请输入a的值:"); a = s.nextInt(); System.out.println("请输入n的值:"); n = s.nextInt(); sum = 0; sn = 0; for (int i = 0; i < n; i++) { sum += a * Math.pow(10, i); sn += sum; } System.out.println("Sn的值为:" + sn); } } 4. 一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。求它在第10次落地时,共经过了多少米?第10次反弹多高? public class Src04 { public static void main(String[] args) { double high = 100; double sum = 0; for (int i = 1; i <= 10; i++) { sum += high; high /= 2; } System.out.println("小球第十次落地共经过 " + sum + " 米"); System.out.println("第十次反弹 " + high + " 米"); } } 5. 编写一个程序,要求输入一个整数,将各位数字反序后输出。 import java.util.*; public class Src05 { public static void main(String[] args) { int revNum = 0; Scanner s = new Scanner(System.in); System.out.print("请输入一个正整数:"); int num = s.nextInt(); do { revNum = num % 10 + revNum * 10; num /= 10; } while (num > 0); System.out.println(revNum); } } 6. 猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉了一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩一个桃子了。求第一天共摘了多少桃子。 public class Src06 { public static void main(String[] args) { int total = 1; for (int day = 10; day > 1; day--) { total++; total *= 2; } System.out.println(total); } } 7. 编写一个Java Application类型的程序,从键盘上输入三角形的三条边的长度,计算三角形的面积和周长并输出。根据三角形边长求面积公式如下:Math.sqrt(s*(s-a)*(s-b)*(s-c)),其中a、b、c为三角形的三条边,s=(a+b+c)/2。 import java.util.*; public class Src07 { public static void main(String[] args) { int a,b,c,p,s,perimeter; // p为三角形半周长 Scanner sc = new Scanner(System.in); System.out.print("请输入边长a:"); a = sc.nextInt(); System.out.print("请输入边长b:"); b = sc.nextInt(); System.out.print("请输入边长c:"); c = sc.nextInt(); perimeter = a + b + c; p = perimeter / 2; s = (int)Math.sqrt(p * (p - a) * (p - b) * (p -c)); //根据海伦公式利用半轴长求三角形面积 System.out.println("该三角形周长为:" + perimeter); System.out.println("该三角形面积为:" + s); } } 8. 编写一个程序,要求读入若干个整数,统计出正整数个数和负整数个数,读入0则结束。 import java.util.*; public class Src08 { public static void main(String[] args) { int i; int positiveNum = 0; int negativeNum = 0; Scanner s = new Scanner(System.in); System.out.println("请输入若干个整数(输入0则结束):"); do { i = s.nextInt(); if (i > 0) { positiveNum++; } else if (i < 0) { negativeNum++; } }while (i != 0); System.out.println("正整数个数为:" + positiveNum); System.out.println("负整数个数为:" + negativeNum); } } 9. 编写一个Java Application类型的程序,从键盘上输入摄氏温度C,计算华氏温度F的值并输出。其转换公式如下: F = (9 / 5) * C + 32 import java.util.*; public class Src09 { public static void main(String[] args) { double c, f; Scanner s = new Scanner(System.in); System.out.print("请输入摄氏度度数:"); c = s.nextDouble(); f = (double) 9 / 5 * c + 32; System.out.println("转化为华氏温度:" + f); } } 10. 编写打印“九九乘法口诀表”的程序。 public class Src10 { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { System.out.print(j + "×" + i + "=" + (j * i) + "\t"); if (i == j) { break; } } System.out.println(); } } } 11. 编写一个根据上下限求回文数的方法,要求输出上下限范围内的回文数及个数。编写测试类进行测试。例如100-200之间的回文数为: 101 111 121 131 141 151 161 171 181 191,总共有10个。 12. 编写一个程序,接受用户输入的两个数据为上、下限,然后输出上、下限之间的所有素数。 import java.util.*; public class Src12 { public static void main(String[] args){ int upperLimit, lowerLimit; Scanner sc = new Scanner(System.in); System.out.print("请输入一个数作为下限:"); lowerLimit = sc.nextInt(); System.out.print("请输入一个数作为上限:"); upperLimit = sc.nextInt(); System.out.print("该范围内的素数有:"); if (lowerLimit == 0 || lowerLimit == 1) { lowerLimit = 2; } for (int i = lowerLimit; i <= upperLimit; i++) { if (isPrime(i)) { System.out.print(i + ", "); } } } private static boolean isPrime(int num1) { boolean prime = true; int num2 = (int)Math.sqrt(num1); for (int i = 2; i <= num2; i++) { if (num1 % i == 0) { prime = false; break; } } return prime; } } 13. 从键盘上输入一个字符串,试分别统计出该字符串中所有数字、大写英文字母、小写英文字母以及其他字符的个数并分别输出这些字符。 import java.util.*; public class Src13 { public static void main(String[] args) { int digit = 0; int capital = 0; int lowercase = 0; int other = 0; System.out.print("请输入一串字符:"); Scanner sc = new Scanner(System.in); String s = sc.nextLine(); char[] ch = s.toCharArray(); for (int i = 0; i < ch.length; i++) { if (ch[i] >= '0' && ch[i] <= '9') { digit++; } else if (ch[i] >= 'A' && ch[i] <= 'Z') { capital++; } else if (ch[i] >= 'a' && ch[i] <= 'z') { lowercase++; } else { other++; } } System.out.println("数字的个数为:" + digit); System.out.println("大写字母的个数为:" + capital); System.out.println("小写字母的个数为:" + lowercase); System.out.println("其它字符的个数为:" + other); } } 14. 编写程序,读取一个在0和1000之间的整数,并将该整数的各位数字加和。 import java.util.*; public class Src14 { public static void main(String[] args) { System.out.print("请输入一个0到1000之间的整数:"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int sum = 0; char ch; String st = String.valueOf(num); for (int i = 0; i < st.length(); i++) { ch = st.charAt(i); sum += ch - '0'; } System.out.println("各位数之和为:" + sum); } } 15. 编写程序对数组a={20,39,45,78,43,23,45,89,131}进行排序,输出各个元素并求出数组当中的最大值和最小值及平均值。 import java.util.*; public class Src15 { public static void main(String[] args) { int[] a = {20, 39, 45, 78, 43, 23, 45, 89, 131}; int sum = 0; double ave; Arrays.sort(a); System.out.print("排序后的顺序为:"); for (int i = 0; i < a.length; i++) { sum += a[i]; System.out.print(a[i] + " "); } ave = sum / (double)a.length; System.out.println("\n最小值为:" + a[0]); System.out.println("最大值为:" + a[a.length - 1]); System.out.println("平均值为:" + ave); } } 16. 一些网站设定了一些制定密码的规则。编写一个方法,检验一个字符串是否是合法的密码。假设密码规则如下:(1)密码必须至少有8个字符(2)密码只能包括字母和数字(3)密码必须至少有2个数字。编写一个程序,提示用户输入密码,如果该密码符合规则就显示“有效密码”,否则显示“无效密码”。 import java.util.*; public class Src16 { public static void main(String[] args) { System.out.print("请设定一个密码:"); Scanner sc = new Scanner(System.in); String st = sc.next(); if (isLegalPass(st)) { System.out.println("密码有效!"); } else { System.out.println("密码无效!"); } } private static boolean isLegalPass(String st) { if (isLegalLength(st) && isLegalChar(st) && isLegalNumber(st)) { return true; } else { return false; } } private static boolean isLegalLength(String st) { if (st.length() >= 8) { return true; } else { return false; } } private static boolean isLegalChar(String st) { boolean isLegalChar = true; for (int i = 0; i < st.length(); i++) { if (!Character.isLetterOrDigit(st.charAt(i))) { isLegalChar = false; break; } } return isLegalChar; } private static boolean isLegalNumber(String st) { int count = 0; for (int i = 0; i < st.length(); i++) { if (Character.isDigit(st.charAt(i))) { count++; } } if (count >= 2) { return true; } else { return false; } } } 17. 编写程序,求1+3+7+15+…+(2^20-1)。说明:2^20指的是2的20次幂。 public class Src17 { public static void main(String[] args) { int num1 = 1; int sum = 0; for (int i = 1; i <= 20; i++) { num1 *= 2; sum += num1 - 1; } System.out.println("该式的和为:" + sum); } } 18. 已知,s=1-1/2+1/3-1/4+…+1/(n-1)-1/n,编写程序,求n=100时,s的值。 public class Src18 { public static void main(String[] args) { int a = 1; double s = 0; for (int n = 1; n <= 100; n++) { s += (double) a / (double) n; a = -a; } System.out.println("s = " + s); } } 19. 用Java语言编写程序,要求能根据菜单选择操作,计算所选图形(例如矩形和圆形)周长和面积,并编写测试类测试完成测试。 20. 输入某年某月某日,判断这一天是这一年的第几天。计算方法为:h =(q+[26(m+1)/10]+k+[k/4]+[j/4]+5*j)%7,各变量含义如下:(1)h是一个星期中的每一天(0为星期六;1为星期天;2为星期一;3为星期二;4为星期三;5为星期四;6为星期五)(2)q是某月的某一天(3)m是月份(3为三月,4为四月,...,12为十二月)。一月和二月分别记为上一年的13和14月。(4)j是世纪数(即|year/100|)(5)k是世纪的年数(即year%100)。 import java.util.*; public class Src20 { public static void main(String[] args) { int year, month, day; Scanner sc = new Scanner(System.in); boolean b; do { b = true; System.out.print("请输入年:"); year = sc.nextInt(); System.out.print("请输入月:"); month = sc.nextInt(); System.out.print("请输入日:"); day = sc.nextInt(); if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) { System.out.println("该日期无效,请重新输入!"); b = false; } } while (!b); Calendar calendar = Calendar.getInstance(); calendar.set(year,month - 1, day); int sumDays = calendar.get(Calendar.DAY_OF_YEAR); System.out.println("该日期是这一年的第" + sumDays + "天"); } } 21. 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩。 import java.util.*; public class Src21 { public static void main(String[] args) { int sum; double avg; Scanner sc = new Scanner(System.in); String[][] information = new String[5][6]; for (int i = 1; i <= 5; i++) { System.out.print("请输入第" + i + "个学生的学号:"); information[i - 1][0] = sc.nextLine(); System.out.print("请输入第" + i + "个学生的姓名:"); information[i - 1][1] = sc.nextLine(); for (int j = 1; j < 4; j++) { System.out.print("请输入该学生的第" + j + "个成绩:"); information[i - 1][j + 1] = sc.nextLine(); } System.out.print("\n"); } for (int i = 0; i < 5; i++) { sum = 0; for (int j = 2; j < 5; j++) { sum += Integer.parseInt(information[i][j]); } avg = (double)sum / 3; information[i][5] = String.valueOf(avg); } String s; for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { s = information[i][j]; System.out.print(s + "\n"); } System.out.print("\n"); } } } 22. 编写程序,从键盘上输入自己的班级、学号、姓名、身份证号等真实信息,并在屏幕上输出。输出格式如下:2014网络媒体1班 11101016 Jike 4101041999052943. import java.util.*; public class Src22 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] str = new String[4]; System.out.print("请输入班级:"); str[0] = sc.nextLine(); System.out.print("请输入学号:"); str[1] = sc.nextLine(); System.out.print("请输入姓名:"); str[2] = sc.nextLine(); System.out.print("请输入身份证号:"); str[3] = sc.nextLine(); for (int i = 0; i < 4; i++) { System.out.print(str[i] + "\t"); } } } 23. 编写程序,从键盘上任意输入两个整数,并将其结果按格式打印输出。input:3 5 output:3×5=15。 import java.util.*; public class Src23 { public static void main(String[] args) { int num1, num2; Scanner sc = new Scanner(System.in); System.out.print("请输入第一个整数:"); num1 = sc.nextInt(); System.out.print("请输入第二个整数:"); num2 = sc.nextInt(); System.out.println(num1 + "×" + num2 + "=" + num1 * num2); } } 24. 对于给定数组ary={3,50,25,10,90,85}编写程序完成下述功能:1.对其中元素进行排序;2.对其中的元素求和。编写测试类完成测试。 import java.util.*; public class Src24 { public static void main(String[] args) { int[] array = {3, 50, 25, 10, 90, 85}; int sum = 0; Arrays.sort(array); System.out.print("排序后的顺序为:"); for (int i = 0; i< array.length; i++) { sum += array[i]; System.out.print(array[i] + " "); } System.out.println("\n元素和为:" + sum); } } 25. 编写程序求解1!+2!+3!+……+10!的值,并在屏幕上输出,要求用递归的方法求一个数的阶乘。 public class Src25 { public static void main(String[] args) { int sum = 0; int a = 1; for (int i = 1; i <= 10; i++) { a *= i; sum += a; } System.out.println("该式的值为:" + sum); } } 26. 无 27. 当用户输入的数据中有小写的字母时,自动转换为大写字母。编写程序完成该功能。 import java.util.*; public class Src27 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入需要转换的小写字母:"); String str = sc.nextLine(); System.out.println("转换后的字母为:" + str.toUpperCase()); } } 28. 利用循环控制语句,输出如下所示数字塔。 示意图: 1 1 22 22 333 333 4444 4444 55555 55555 666666 666666 7777777 7777777 88888888 88888888 999999999999999999 代码: public class Src28 { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++){ System.out.print(i); } for (int k = 2 * (9 - i); k >= 1; k--){ System.out.print(" "); } for (int l = 1; l <= i; l++) { System.out.print(i); } System.out.print("\n"); } } } 29. 双色球福利彩票的摇奖规则是从号码为1-33的红色球中随机选取六个,从号码为1-16的蓝色球中随机选取一个组成号码。编写程序,模拟双色球福利彩票开奖号码生成过程。 public class Src29 { public static void main(String[] args) { int red, blue; String str = ""; for (int i = 0; i < 6; i++) { red = (int)(Math.random() * 33) + 1; str += red + " "; } blue = (int)(Math.random() * 16) + 1; str += blue; System.out.println("双色球号码为:" + str); } } 30. 编写程序,要求用递归的方法求解表达式1×2+2×3+3×4+4×5+5×6+6×7+7×8+8×9+9×10的值,并将结果按格式1×2+2×3+3×4+4×5+5×6+6×7+7×8+8×9+9×10=330形式输出。 public class Src30 { public static void main(String[] args) { int sum = 0, i; for (i = 1; i < 9; i++) { System.out.print(i + "×" + (i + 1) + "+"); sum += function(i); } System.out.print(i + "×" + (i + 1) + "=" + (sum + function(i))); } private static int function(int a) { int x; if (a == 1) { x = 2; } else { x = function(a - 1) + 2 * a; } return x; } } 31. 编写程序,利用循环控制语句输出如下数字金字塔图形。 示意图: 1 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 代码: public class Src31 { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 9; j >= i; j--) { System.out.print(" "); } for (int k = i; k >= 1; k--) { System.out.print(i < 10 ? " " + i : i); } for (int l = 2; l <= i; l++) { System.out.print(i < 10 ? " " + i : i); } System.out.print("\n"); } } } 32. 从键盘输入一串字符串,运行程序,将首字母变为大写,其它小写。编写程序,完成上述功能。