1.
- package day7_27;
-
- import java.util.Scanner;
-
- public class Zuo67 {
- public static void main(String[] args) {
- System.out.print("请输入一个整数:");
- Scanner scanner = new Scanner(System.in);
- long input = scanner.nextLong();
- System.out.println(sumDigits(input));
- scanner.close();
- }
-
- public static int sumDigits(long n) {
- int i,b=0;
- while(n!=0){
- i = (int) n % 10;
- n = n / 10;
- b += i;
- }
- return b;
- }
- }
-
- /*
- 请输入一个整数:234
- 9
- */
2.
- package day7_27;
-
- import java.util.Scanner;
-
- public class Zuo68 {
- public static void main(String[] args) {
- System.out.print("请输入一个整数:");
- Scanner scanner = new Scanner(System.in);
- int num = scanner.nextInt();
- if (isPalindrome(num)) {
- System.out.println("是回文整数!");
- }else {
- System.out.println("不是回文数字!");
- }
- scanner.close();
- }
-
- public static int reverse(int number) {
- int reverseNumber = 0;
- do {
- reverseNumber = reverseNumber * 10 + number % 10;
- number /= 10;
- }while(number > 0);
- return reverseNumber;
- }
- public static boolean isPalindrome(int number) {
- return (reverse(number) == number);
- }
- }
-
- /*
- 请输入一个整数:122
- 不是回文数字!
- */
3.
- package day7_27;
-
- import java.util.Random;
- import java.util.Scanner;
-
- public class Zuo69 {
- public static void main(String[] args) {
- System.out.print("Enter n:");
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- printMatrix(n);
- scanner.close();
- }
-
- public static void printMatrix(int n) {
- int arr[][] = new int[n][n];
- Random random = new Random();
- for (int i = 0; i <= (arr.length - 1); i++) {
- for (int j = 0; j <= (arr[0].length - 1); j++) {
- arr[i][j] = random.nextInt(2);
- System.out.print(arr[i][j] + " ");
- }
- System.out.println();
- }
-
- }
- }
-
- /*
- Enter n:5
- 1 0 0 1 0
- 1 1 0 1 0
- 1 0 0 1 0
- 1 1 0 0 0
- 0 0 1 1 0
- */
4.(检测密码)一些网站对于密码具有一些规则。编写一个方法,检测字符串是否是一个有效密码。假定密码规则如下:
●密码必须至少8位字符。密码仅能包含字母和数字。密码必须包含至少两个数字。 编写一个程序,提示用户输入一个密码,如果符合规则,则显示Valid Password,否则显示Invalid Password。
- package day7_27;
-
- import java.util.Scanner;
-
- public class Zuo70 {
- public static void main(String[] args) {
- Scanner scanner=new Scanner(System.in);
- System.out.print("请输入密码:");
- String password=scanner.nextLine();
-
- if(isValid(password)){
- System.out.println("Valid Password");
- }else{
- System.out.println("Invalid Password");
- }
- scanner.close();
- }
-
- public static boolean isValid(String s){
- return isLengthValid(s)&&isContentValid(s)&&isNumberValid(s);
- }
- public static boolean isLengthValid(String s){
- return s.length()>=8;
- }
- public static boolean isContentValid(String s){
- char c=' ';
- for(int i=0;i
- c=s.charAt(i);
- if(!isLetter(c)&&!isDigit(c)){
- return false; //如果有不是数字或字母的,则输出false
- }
- }
- return true;
- }
- public static boolean isNumberValid(String s){ //第三个条件判断是否超过两个数字
- int count=0;
- char c=' ';
- for(int i=0;i
- c=s.charAt(i);
- if(isDigit(c)){
- count++;
- }
- }
- return count>=2;
- }
-
- public static boolean isLetter(char c){
- return c>='a'&&c<='z' || c>='A'&&c<='Z';
- }
- public static boolean isDigit(char c){
- return c>='0'&&c<='9';
- }
- }
-
- /*
- 请输入密码:123456789
- Valid Password
- */
5.(MyTriangle类)创建一个名为MyTriangle的类,它包含如下两个方法
编写一个测试程序,读入三角形三边的值,若输人有效,则计算面积;否则显示输人无效。三角形面积的计算公式在编程练习题2.19中给出。
- package day7_27;
-
- import java.util.Scanner;
-
- public class MyTriangle {
- public static void main(String[] args) {
- System.out.println("请输入三角形的边长:");
- Scanner scanner = new Scanner(System.in);
- while (true) {
- double a = scanner.nextDouble();
- double b = scanner.nextDouble();
- double c = scanner.nextDouble();
- if (isValid(a, b, c)) {
- System.out.println("三角形的面积为:"+area(a, b, c));
- break;
- }else {
- System.out.println("输入无效!!!");
- break;
- }
- }
- }
-
- public static boolean isValid(double s1,double s2,double s3) {
- return (s1 + s2 > s3) && (s1 + s3 > s2) && (s2 + s3 > s1)
- && (s1 - s2 < s3) && (s1 - s3 < s2) && (s2 - s3 < s1)
- && (s2 - s1 < s3) && (s3 - s1 < s2) && (s3 - s2 < s1);
- }
-
- public static double area(double s1,double s2,double s3) {
- double p = (s1 + s2 + s3) / 2;
- double s =Math.sqrt(p * (p - s1) * (p - s2) * (p - s3));
- return s;
- }
-
- }
-
-
- /*
- 请输入三角形的边长:
- 3 4 5
- 三角形的面积为:6.0
- */
6.(计算一个字符串中字母的个数)编写一个方法,使用下面的方法头计算字符串中的字母个数:public static int countLetters(String s)
编写一个测试程序,提示用户输入字符串,然后显示字符串中的字母个数。
- package day7_27;
-
- import java.util.Scanner;
-
- public class Zuo72 {
- public static void main(String[] args) {
- System.out.print("请输入字符串:");
- Scanner scanner = new Scanner(System.in);
- String str = scanner.nextLine();
- System.out.println("你输入的字符串中包含字母:"+countLetters(str)+"个");
- scanner.close();
- }
-
- public static int countLetters(String string) {
- int count = 0;
- for (int i = 0; i < string.length(); i ++) {
- if (string.charAt(i) >= 'A' && string.charAt(i) <= 'Z') {
- count ++ ;
- }
- if (string.charAt(i) >= 'a' && string.charAt(i) <= 'z') {
- count ++ ;
- }
- }
- return count;
- }
- }
-
- /*
- 请输入字符串:asd1234
- 你输入的字符串中包含字母:3个
- */
7.(数学:平方根的近似求法)有几种实现Math类中 sqrt方法的技术。其中一个称为巴比伦法。它通过使用下面公式的反复计算近似地得到:
nextGuess - (lastGuess +n / lastGuess) / 2
当nextGuess和 lastGuess几乎相同时,nextGuess就是平方根的近似值。最初的猜测值可以是任意一个正值(例如1)。这个值就是1astGuess的初始值。如果nextGuess和lastCuess的差小于一个很小的数,比如0.0001,就可以认为nextGuess是n的平方根的近似值;否则,nextGuess就成为1astGuess,近似过程继续执行。实现下面的方法,返回n的平方根。
public static double sqrt(long n)
- package day7_27;
-
- public class Zuo73 {
- public static void main(String[] args) {
- System.out.println(sqrt(9));
- }
-
- public static double sqrt(long n) {
- double lastGuess;
- double nextGuess = 1;
- do {
- lastGuess = nextGuess;
- nextGuess = (lastGuess + n / lastGuess) / 2;
- } while (lastGuess - nextGuess >= 0.0001 || nextGuess - lastGuess >= 0.0001);
- return nextGuess;
- }
- }
-
-
- /*
- 3.000000001396984
- */
8.(回文素数)回文素数是指一个数同时为素数和回文数。例如:131是一个素数,同时也是一个回文素数。数字313和757也是如此。编写程序,显示前100个回文素数。每行显示10个数并且准确对齐,数字中间用空格隔开。如下所示:
2 3 5 7 11 101 131 151 181 191
313 353 373 383 727 757 787 797 919 929
- package day7_27;
-
- public class Zuo74 {
- public static void main(String[] args) {
- int count = 1;
- for (int i = 2; count <= 100;i++){
- if (isHui(i) && primeNumber(i)){
- System.out.print(i + " ");
- count++;
- if (count % 10 == 0)
- System.out.println();
- }
- }
- }
-
- public static boolean primeNumber(int i){
- boolean bool = true;
- for (int n = 2; n <= i / 2 ;n++){
- if ( i % n == 0){
- bool = false;
- break;
- }
- }
- return bool;
- }
-
- public static boolean isHui(int i) {
- int s = i;
- int b = 0;
- while(s!=0)
- {
- b = b * 10 + s % 10;
- s=s/10;
- }
- if(i==b) {
- return true;
- }
- return false;
- }
- }
-
- /*
- 2 3 5 7 11 101 131 151 181
- 191 313 353 373 383 727 757 787 797 919
- 929 10301 10501 10601 11311 11411 12421 12721 12821 13331
- 13831 13931 14341 14741 15451 15551 16061 16361 16561 16661
- 17471 17971 18181 18481 19391 19891 19991 30103 30203 30403
- 30703 30803 31013 31513 32323 32423 33533 34543 34843 35053
- 35153 35353 35753 36263 36563 37273 37573 38083 38183 38783
- 39293 70207 70507 70607 71317 71917 72227 72727 73037 73237
- 73637 74047 74747 75557 76367 76667 77377 77477 77977 78487
- 78787 78887 79397 79697 79997 90709 91019 93139 93239 93739
- 94049
- */
9.(反素数)反素数(反转拼写的素数)是指一个非回文素数,将其反转之后也是一个素数。例如:17是一个素数,而71也是一个素数,所以17和71是反素数。编写程序,显示前100个反素数。每行显示10个,并且数字间用空格隔开,如下所示:
1 3 17 31 37 71 73 79 97 107 113
149 157 167 179 199 311 337 347 359 389
- package day7_27;
-
- public class Zuo75 {
- public static void main(String[] args) {
- int count = 0;
- for (int i = 2; count <= 100; i ++ ) {
- if (isSu(fanHui(i)) && isSu(i) && (i != fanHui(i))) {
- System.out.print(i + " ");
- count ++;
- if (count % 10 == 0) {
- System.out.println();
- }
- }
- }
- // System.out.println(fanHui(123));
- // System.out.println(isSu(17));
- // System.out.println(isSu(22));
- }
-
- public static boolean isSu(int i) {
- for (int b = 2; b < i; b ++) {
- if (i % b == 0) {
- return false;
- }
- }
- return true;
- }
-
- public static int fanHui(int i) {
- int s = i;
- int b = 0;
- while(s != 0) {
- b = b * 10 + s % 10;
- s /= 10;
- }
- return b;
- }
- }
-
-
- /*
- 13 17 31 37 71 73 79 97 107 113
- 149 157 167 179 199 311 337 347 359 389
- 701 709 733 739 743 751 761 769 907 937
- 941 953 967 971 983 991 1009 1021 1031 1033
- 1061 1069 1091 1097 1103 1109 1151 1153 1181 1193
- 1201 1213 1217 1223 1229 1231 1237 1249 1259 1279
- 1283 1301 1321 1381 1399 1409 1429 1439 1453 1471
- 1487 1499 1511 1523 1559 1583 1597 1601 1619 1657
- 1669 1723 1733 1741 1753 1789 1811 1831 1847 1867
- 1879 1901 1913 1933 1949 1979 3011 3019 3023 3049
- 3067
- */
10.(梅森素数)如果一个素数可以写成2-1的形式,其中p是某个正整数,那么这个素数就称作梅森素数。编写程序,找出p≤31的所有梅森素数,然后显示如下的输出结果:
p 2^p-l
2 3
3 7
5 31
- package day7_27;
-
-
- public class Zuo76 {
- public static void main(String[] args) {
- int number = 0;
- for(int p = 2; p <=31; p++)
- {
- number = (int) (Math.pow(2, p) - 1);
- if(isSu(number))
- {
- System.out.printf("%d\t\t%d\n",p,number);
- }
- }
- }
-
- public static boolean isSu(int i) {
- for (int a = 2; a < i; a ++) {
- if (i % a == 0) {
- return false;
- }
- }
- return true;
- }
- }
-
-
- /*
- 2 3
- 3 7
- 5 31
- 7 127
- 13 8191
- 17 131071
- 19 524287
- */
11.(双素数)双素数是指一对差值为2的素数。例如: 3和5就是一对双素数, 5和7是一对双素数,而11和13也是一对双素数。编写程序,找出小于1000的所有双素数。显示结果如下所示:(3,5) (5,7)
- package day7_27;
-
- public class Zuo77 {
- public static void main(String[] args) {
- for (int a = 2; a <= 997; a++){
- if (isSu(a) && isSu(a+2)){
- System.out.println("(" + a + "," + (a+2) + ")");
- }
- }
- }
-
- public static boolean isSu(int i) {
- for (int a = 2; a < i; a ++) {
- if (i % a == 0) {
- return false;
- }
- }
- return true;
- }
- }
-
- /*
- (3,5)
- (5,7)
- (11,13)
- (17,19)
- (29,31)
- (41,43)
- (59,61)
- (71,73)
- (101,103)
- (107,109)
- (137,139)
- (149,151)
- (179,181)
- (191,193)
- (197,199)
- (227,229)
- (239,241)
- (269,271)
- (281,283)
- (311,313)
- (347,349)
- (419,421)
- (431,433)
- (461,463)
- (521,523)
- (569,571)
- (599,601)
- (617,619)
- (641,643)
- (659,661)
- (809,811)
- (821,823)
- (827,829)
- (857,859)
- (881,883)
- */
12.(几何问题:五边形的面积)五边形的面积可以使用下面的公式计算:
编写一个方法,使用下面的方法头来返回五边形的面积。public static double area(double side)
编写一个主方法,提示用户输人五边形的边、然后显示它的面积。下面是一个运行示例:
Enter the side: 5.5
The area of the pentagon is 52.04444136781625
- package day7_27;
-
- import java.util.Scanner;
-
- public class Zuo78 {
- public static void main(String[] args) {
- System.out.print("Enter the side:");
- Scanner scanner = new Scanner(System.in);
- double s = scanner.nextDouble();
- System.out.println("The area of the pentagon is:" + area(s));
- scanner.close();
- }
-
- private static double area(double s) {
- double b = (5 * s * s) / (4 * Math.tan(Math.PI / 5));
- return b;
- }
- }
-
- /*
- Enter the side:5.5
- The area of the pentagon is:52.04444136781625
- */
13.(几何问题:正多边形的面积)正多边形是一个n条边的多边形,它的每条边的长度都相等,而且所有角的角度也相等(即多边形既是等边又等角的)。计算正多边形面积的公式是:
使用下面的方法头编写方法,返回正多边形的面积:pub1ic static double area(int n,double side)
编写一个main方法,提示用户输人边的个数以及正多边形的边长,然后显示它的面积。下面是一个运行示例:
- package day7_27;
-
- import java.util.Scanner;
-
- public class Zuo79 {
- public static void main(String[] args) {
- System.out.print("Enter the number of sides :");
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- System.out.print("Enter the side :");
- Scanner scanner1 = new Scanner(System.in);
- double s = scanner.nextDouble();
- System.out.println("The area of polygon is:"+area(n, s));
- scanner.close();
- scanner1.close();
- }
- public static double area(int n, double s) {
- double mian;
- mian = (n * s * s) / (4 * Math.tan(Math.PI / n));
- return mian;
- }
- }
-
- /*
- Enter the number of sides :5
- Enter the side :6.5
- The area of polygon is:72.69017017488385
- */
14.递归解决斐波那契数列
- package day7_27;
-
- public class Zuo80 {
- public static void main(String[] args) {
- System.out.println(fei(10));
- }
- public static int fei(int n) {
- if (n == 1) {
- return 1;
- }
- else if (n == 2) {
- return 2;
- }
- else {
- return (fei(n - 1) + fei(n - 2));
- }
- }
- }
15.递归解决汉诺塔问题
- package day7_27;
-
- public class Test02 {
- static int count = 0;
- public static void main(String[] args) {
- move(4, 'x', 'y', 'z');
- System.out.println("总共移动"+ count);
- }
-
- public static void move(int n,char x,char y,char z) {
- count ++;
- if (n == 1) {
- printMove(x, n, z);
- }else {
- move(n - 1, x, z, y);
- printMove(x, n, z);
- move(n - 1, y, x, z);
- }
- }
-
- public static void printMove(char x, int n, char z) {
- System.out.println("移动" + n + "从" + x + "到" + z);
- }
- }
16.(计算数字的出现次数)编写程序,读取在1到100之间的整数,然后计算每个数出现的次数。假定输入是以0结束的。下面是这个程序的一个运行示例:
- package day7_27;
- import java.util.Scanner;
-
- public class Zuo82 {
- public static void main(String[] args) {
- int[] lst1 = new int[101];
- int n = -1;
- Scanner input = new Scanner(System.in);
- System.out.print("Enter the integers between 1 and 100: ");
- do{
- n = input.nextInt();
- ++lst1[n];
- }while(n != 0);
-
- for (int i = 1; i < 101;i++){
- if (lst1[i] == 0){continue;}
- else if (lst1[i] == 1){
- System.out.println(i + " occurs " + lst1[i] + " time");
- } else
- System.out.println(i + " occurs " + lst1[i] + " times");
- }
- input.close();
- }
- }
-
- /*
- Enter the integers between 1 and 100: 1 2 3 2 1 2 0
- 1 occurs 2 times
- 2 occurs 3 times
- 3 occurs 1 time
- */
17.(打印不同的数)编写一个程序,读入10个数并且显示互不相同的数(即一个数出现多次,但仅显示一次)。(提示,读人一个数,如果它是一个新数,则将它存储在数组中。如果该数已经在数组中,则忽略它。)输入之后,数组包含的都是不同的数。下面是这个程序的运行示例:
- package test01;
-
- import java.util.Scanner;
-
- public class Zuo83 {
- public static void main(String[] args) {
- System.out.print("Enter the numbers:");
- Scanner scanner = new Scanner(System.in);
- int[] arr=new int[0];
- for (int i = 1; i <= 10; i ++) {
- int n = scanner.nextInt();
- if(isNew(arr, n)){
- arr=copyOf(arr,arr.length+1);
- arr[arr.length-1] = n;
- }
- }
- scanner.close();
- for (int b = 0; b < arr.length; b ++) {
- System.out.print( arr[b] + " ");
- }
- }
-
- public static boolean isNew(int arr[], int a) {
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] == a) {
- return false;
- }
- }
- return true;
- }
-
- public static int[] copyOf(int[] arr,int newLen){
- int[] newArr=new int[newLen];
- for(int i=0;i
- newArr[i]=arr[i];
- }
- return newArr;
- }
- }
-
- /*
- Enter the numbers: 1 2 3 4 5 6 1 2 3 1
- 1 2 3 4 5 6
- */
18.(求数组的平均值)编写两个重载的方法,使用下面的方法头返回一个数组的平均数:
public static int average(int[]array)
pub1ic static double average(double[]array)
编写测试程序,提示用户输入10个double型值,调用这个方法,然后显示平均值。
- package test01;
-
- import java.util.Scanner;
-
- public class Zuo84 {
- public static void main(String[] args) {
- System.out.print("请输入十个整型或者浮点型数值:");
- Scanner scanner = new Scanner(System.in);
- double dou [] = new double[10];
- for (int i = 0; i < 10; i ++) {
- dou[i] = scanner.nextDouble();
- }
- System.out.println(average(dou));
- scanner.close();
- }
-
- private static double average(double[] dou) {
- double sum = 0;
- for (int i = 0; i < dou.length; i++) {
- sum += dou[i];
- }
- return sum / dou.length;
- }
-
- public static int average(int [] array) {
- int sum = 0;
- for (int i = 0; i < array.length; i++) {
- sum += array[i];
- }
- return sum / array.length;
- }
- }
-
- /*
- 请输入十个整型或者浮点型数值: 1.1
- 1.2 1.3 1.1 1.2 1.3 1.1 1.2 1.3 1.0
- 1.18
- */
19.(计算ged)编写一个方法,返回个数不确定的整数的最大公约数。指定这个方法头如下所示:public static int gcd(int. . . numbers)
编写测试程序,提示用户输入5个数字,调用该方法找出这些数的最大公约数,并显示这个最大公约数。
- package test01;
-
- public class Zuo85 {
- public static void main(String[] args) {
- System.out.println("The gcd number of numbers is:"+gcd(36,72,24));
- System.out.println("The gcd number of numbers is:"+gcd(6,36,24));
- }
-
- public static int gcd(int... numbers) {
- int gcd = numbers[0];
- for (int i = 1; i <= numbers[0]; i++){
- if (!gcdOfNums(i,numbers)){
- continue;
- }
- else{
- gcd = i;
- }
- }
- return gcd;
- }
-
- private static boolean gcdOfNums(int i, int[] numbers) {
- for (int j = 0 ; j < numbers.length; j++){
- if(numbers[j] % i != 0){
- return false;
- }
- }
- return true;
- }
- }
-
- /*
- The gcd number of numbers is:12
- The gcd number of numbers is:6
- */
20.(消除重复)使用下面的方法头编写方法,消除数组中重复出现的值;
public static int[]eliminateDup1icates(int[] 1ist)
编写一个测试程序,读取10个整数,调用该方法,然后显示结果。
下面是程序的运行示例:
Enter ten numbers: 12321634 5 2 Enker The distinct numbers are: 12 36 4 5
- package test01;
-
- import java.util.Arrays;
-
- public class Zuo86 {
- public static void main(String[] args) {
- int[] a = {1,2,3,4,5,6,7,7,6};
- int[] b = eliminateDuplicates(a);
- System.out.println(Arrays.toString(b));
- }
-
- public static int[] eliminateDuplicates(int [] list) {
- int[] list_1;
- int count = 1;
- int mis = list[0];
- for(int i = 1;i < list.length;i++){
- for(int j = i+1;j < list.length;j++){
- if(list[i] == list[j]){
- list[j] = mis;
- }
- }
- if(list[i] != mis)count++;
- }
- int k = 1;
- list_1 = new int[count];
- list_1[0] = mis;
- for(int i = 1;i < list.length;i++){
- if(list[i] != mis)
- list_1[k++] = list[i];
- }
- return list_1;
- }
- }
-
相关阅读:
typora主题切换与推荐主题
深入理解synchronized关键字
[Linux初阶]常见的指令
java项目-第161期ssm弹幕视频网站系统_ssm毕业设计_计算机毕业设计
做自媒体有哪些违规内容需要注意?
Vue3 ref函数和reactive函数
CentOS7安装Oracle数据库的全流程
MaskRCNN(matterport)模型搭建与实验
解决yolo无法指定显卡的问题,实测v5、v7、v8有效
通过核密度分析工具建模,基于arcgis js api 4.27 加载gp服务
-
原文地址:https://blog.csdn.net/qq_58029155/article/details/125717394