引言
System.out.println("Hellow word")
水仙花数
水仙花数。水仙花数,又称自恋数、阿姆斯特朗数,是指一个n位数,它的每个位上的数字的n次幂之和等于它本身。例如,一个三位的水仙花数是153,因为
public class DaffodilNumbers {
public static void main(String[] args) {
// 下限
int lowerBound = 100;
// 上限
int upperBound = 999;
for (int i = lowerBound; i <= upperBound; i++) {
if (isNarcissisticNumber(i)) {
System.out.println(i);
}
}
}
// 判断一个数是否为水仙花数
public static boolean isNarcissisticNumber(int number) {
int sum = 0;
int temp = number;
while (temp != 0) {
// 取出最低位的数字
int digit = temp % 10;
// 求和
sum += (int) Math.pow(digit, getDigitCount(number));
// 去掉最低位的数字
temp /= 10;
}
return sum == number;
}
// 计算一个数的位数
public static int getDigitCount(int number) {
int count = 0;
while (number != 0) {
count++;
number /= 10;
}
return count;
}
}
在上述代码中,首先定义了一个范围,即lowerBound和upperBound,然后使用一个循环遍历这个范围内的所有数字。对于每个数字,调用isNarcissisticNumber()
方法来判断是否为水仙花数。
在isNarcissisticNumber()
方法中,首先定义了一个变量sum
用来保存每个位上数字的n次幂之和,然后使用一个循环将该数字的每一位取出,并求和。通过取余和除以10的方式来获取每一位的数字,并利用Math.pow()
方法来进行幂运算。最后,将求和的结果与原数字进行比较,如果相等,则返回true
,表示该数字是水仙花数。
在getDigitCount()
方法中,用一个循环计算一个数的位数,即通过每次除以10来去掉最低位的数字,并且在每次循环中计数器加一,直到该数变为0为止。
通过以上的方法,可以实现水仙花数的判断并将结果打印出来。
判断当前数字是否是素数
public class JudgmentPrime {
// 输入一个整数,判断是不是素数
// 第一种
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
// 第二种
public static boolean isPrime2(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
// 目标数字
int number = 1;
boolean isPrime = JudgmentPrime.isPrime2(number);
System.out.println(number + " 是素数吗:" + isPrime);
}
}
首先,从输入中获得一个整数。然后调用isPrime
方法判断该整数是否为素数。该方法使用循环从2开始到该整数的平方根进行遍历,如果发现能被整除的数,则该整数不是素数。如果没有找到能被整除的数,则该整数是素数。最后根据判断结果输出相应的信息。
获取当前数字的下标
public class Subscript {
public static String findIndex(int[] array, int target) {
for (int i = 0; i < array.length; i++) {
if (array[i] == target) {
// 返回当前数字在数组中的下标
return "当前数字在数组中的下标是:" + i;
}
}
// 如果数组中不存在该数字,则返回-1
return "当前数组中没有这个数字,无法获取下标";
}
public static void main(String[] args) {
// int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// 数组中 前面的接受对象为数组 后面就可以不写 new int[],可以省略
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int target = 4;
String index = findIndex(array, target);
System.out.println("目标数字在数组中的下标是:" + index);
}
}
这段代码定义了一个名为Subscript(下标)的公共类。findIndex方法的输入参数是一个int型数组array和一个int型目标数字target。方法中使用for循环遍历数组中的每个元素,如果找到了与目标数字相等的元素,则返回该元素在数组中的索引值,就是字符串"当前数字在数组中的下标是:i",其中i为目标数字的索引值。
如果整个数组遍历完没有找到与目标数字相等的元素,则返回字符串"当前数组中没有这个数字,无法获取下标"。
九九乘法表
这个就没什么过多介绍的了,各位初入编程的时候应该不是在课堂上就是在网络上看到过这道题,应该算是比较经典的入门题目了。
public class MultiplicationTable {
// 正序 九九乘法表
public static void main1(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
}
System.out.println();
}
}
// 逆序 九九乘法表
public static void main(String[] args) {
for (int i = 9; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
}
System.out.println();
}
}
}
统计字母,空格,数字,其他字符
输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数
public class CountTheNumber {
// 输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数
public static void main(String[] args) {
System.out.println("请输入一行字符: ");
String str = new Scanner(System.in).nextLine();
System.out.println("您输入的字符串为:" + str);
// int类型后面可以使用逗号定义多个变量或者恒量
int letter = 0, space = 0, number = 0, other = 0;
for (int i = 0; i < str.length(); i++) {
// 判断是否为字母,空格,数字,其他字符,统计个数
// 使用了ASCII码,a-z为97-122,A-Z为65-90,0-9为48-57
if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {
// letter++ 的意思是 letter = letter + 1
letter++;
} else if (str.charAt(i) == ' ') {
space++;
} else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
number++;
} else {
other++;
}
}
System.out.println("字母: " + letter);
System.out.println("空格: " + space);
System.out.println("数字: " + number);
System.out.println("其他字符: " + other);
}
}
输入两个正整数m和n,求其最大公约数和最小公倍数。
public class CommonDivisor {
// 输入两个正整数m和n,求其最大公约数和最小公倍数。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个正整数m: ");
int m = scanner.nextInt();
System.out.print("请输入第二个正整数n: ");
int n = scanner.nextInt();
int gcd = findGCD(m, n);
int lcm = findLCM(m, n, gcd);
System.out.println("最大公约数是: " + gcd);
System.out.println("最小公倍数是: " + lcm);
scanner.close();
}
// 计算最大公约数(使用欧几里得算法)
public static int findGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// 计算最小公倍数
public static int findLCM(int a, int b, int gcd) {
return (a * b) / gcd;
}
}