No21
我觉得是5个。"a","b","c","ab","abc"。
首先先有"a","b","c"这三个字符串。然后执行"a"+"b",生成"ab"字符串,然后执行"ab"+"c",生成"abc"。
如果String str算的话就是6个……
No22
这个有点乱……import java.util.Scanner;
public class No22 {
public static void main(String[] args) {
int count = 0, len1, i, j;
String s1, s2;
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
s1 = sc.nextLine();
System.out.println("请输入子串:");
s2 = sc.nextLine();
len1 = s1.length();
for (i = 0; i < s1.length(); i++, len1--) {
if (len1 < s2.length()) {
break;
}
if (s1.charAt(i) == s2.charAt(0)) {
for (j = 1; j < s2.length(); j++) {
if (s1.charAt(i + j) != s2.charAt(j)) {
break;
}
}
if (j == s2.length()) {
count++;
}
}
}
System.out.println("子串数量为:" + count);
sc.close();
}
}
No23import java.util.Scanner;
public class No23 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num;
System.out.println("请输入大于6的偶数");
num = sc.nextInt();
if (num <= 6 || num % 2 != 0) {
System.out.println("输入的数字不符合要求。");
} else {
for (int i = 1; i <= num / 2; i++) {
if (prime(i) && prime(num - i)) {
System.out.println(num + "=" + i + "+" + (num - i));
break; // 如果需要输出全部可能则将此行去掉。
}
}
}
sc.close();
}
static boolean prime(int i) {
if (i == 1) {
return false;
} else {
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
return false;
}
}
return true;
}
}
}
No24import java.util.Scanner;
public class No24 {
public static void main(String[] args) {
int a, b, c;
double p, q, deta;
Scanner sc = new Scanner(System.in);
System.out.println("请分别依次输入二次项系数、一次项系数和常数项,用空格隔开:");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if (a == 0) {
System.out.println("输入的不是一元二次方程。");
} else {
deta = b * b - 4 * a * c;
if (deta == 0) {
System.out.println("X1 = X2 = " + (-b / 2 / a));
} else if (deta > 0) {
p = -b / (2 * a);
q = Math.sqrt(deta) / (2 * a);
System.out.println("X1 = " + (p + q));
System.out.println("X1 = " + (p - q));
} else {
p = -b / (2 * a);
q = Math.sqrt(-deta) / (2 * a);
System.out.println("X1 = " + p + "i" + "+" + q);
System.out.println("X1 = " + p + "i" + "-" + q);
}
}
sc.close();
}
}
No25import java.util.Scanner;
public class No25 {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要打印杨辉三角的行数:");
n = sc.nextInt();
int[][] yang = new int[n][];
for (int i = 0; i < n; i++) {
yang[i] = new int[i + 1];
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
yang[i][j] = 1;
} else {
yang[i][j] = yang[i - 1][j - 1] + yang[i - 1][j];
}
}
}
for (int p = 0; p < n; p++) {
for (int q = 0; q < yang[p].length; q++) {
System.out.print(yang[p][q] + " ");
}
System.out.println();
}
sc.close();
}
}
No26import java.util.Scanner;
public class No26 {
public static void main(String[] args) {
String s;
Scanner sc = new Scanner(System.in);
System.out.println("请输入星期的第一个字母:");
s = sc.nextLine();
if (run(s.toLowerCase())) {
System.out.println("无法判断,请输入第二个字母:");
s += sc.nextLine();
next(s.toLowerCase());
}
sc.close();
}
static boolean run(String s) {
switch (s) {
case "m":
System.out.println("是星期一,Monday。");
return false;
case "w":
System.out.println("是星期三,Wednesday。");
return false;
case "f":
System.out.println("是星期五,Friday。");
return false;
case "s":
case "t":
return true;
default:
System.out.println("输入有误");
return false;
}
}
static void next(String s) {
switch (s) {
case "su":
System.out.println("是星期日,Sunday。");
break;
case "sa":
System.out.println("是星期六,Saturday。");
break;
case "th":
System.out.println("是星期四,Thursday。");
break;
case "tu":
System.out.println("是星期三,Tuesday。");
break;
default:
System.out.println("输入有误");
break;
}
}
}
No27
这个竟然搞得我有点晕……public class No27 {
public static void main(String[] args) {
int num = 4;
for (int i = 0; i < num; i++) {
for (int m = 0; m < num - i - 1; m++) {
System.out.print(" ");
}
for (int n = 0; n < i * 2 + 1; n++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 0; i < num - 1; i++) {
for (int m = 0; m < i + 1; m++) {
System.out.print(" ");
}
for (int n = 0; n < (num - i - 1) * 2 - 1; n++) {
System.out.print("*");
}
System.out.println();
}
}
}
No28public class No28 {
public static void main(String[] args) {
int sum;
System.out.println("10000内的完数有:");
for (int i = 3, j; i < 10000; i++) {
sum = 1;
for (j = 2; j < Math.sqrt(i); j++) {
if (i % j == 0) {
sum += j + (i / j);
}
}
if (j == Math.sqrt(i)) {
sum += j;
}
if (sum == i) {
System.out.println(i);
}
}
}
}
No29import java.util.Scanner;
public class No29 {
public static void main(String[] args) {
int a, n, sum = 0, temp = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请以此输入a的值和相加的个数,用空格分开");
a = sc.nextInt();
n = sc.nextInt();
for (int i = 0; i < n; i++) {
temp = temp * 10 + 1;
sum += temp;
}
sum *= a;
System.out.println("最后结果为:" + sum);
sc.close();
}
}
No30public class No30 {
public static void main(String[] args) {
String s1 = "123";
String s2 = "123";
String s3 = new String("123");
String s4 = new String("123");
System.out.println("s1==s2 " + (s1 == s2));
System.out.println("s1==s3 " + (s1 == s3));
System.out.println("s3==s4 " + (s3 == s4));
System.out.println("s1.equals(s2) " + (s1.equals(s2)));
System.out.println("s1.equals(s3) " + (s1.equals(s3)));
System.out.println("s3.equals(s4) " + (s3.equals(s4)));
}
}
这次有几个写的有些乱- -,不会改了,就先这样吧……
另外,新年快乐~
|