1.判断回文数:
import java.util.Scanner;
public class HuiWenZhengShu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int num = sc.nextInt();
if(isPaildrome(num)){
System.out.println("是回文数");
}else{
System.out.println("不是回文数");
}
}
public static boolean isPildrome(int num){
return reverse(num) == num;
}
public static int reverse(int sum){
int sum = 0;
while(true){
sum = sum * 10 + num % 10;
num /= 10;
if(num == 0){
return sum;
}
}
}
}
2.求数字各项和:
import java.util.Scanner;
public class ZiChuan01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.pritln
int num = sc.nextInt();
int sum = sumDigtal(num);
System.out.println(num);
}
public static int sumDigtal(int n){
int sum = 0;
while(true){
sum += n % 10;
n /= 10;
if(n == 0){
return sum;
}
}
}
}
3.打印图形:
import java.util.Scanner;
public class PrintTuXing {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入行数:");
displayPaattern(scanner.nextInt());
}
private static void displayPaattern(int line) {
for (int i = 1; i <= line; i++){
for (int k = 1; k <= line - i; k++){
System.out.print(" ");
}
for (int j = i; j >= 1; j--){
System.out.print(j + " ");
}
System.out.println();
}
}
}
4.平方根:
public class PingFangGen {
public static void main(String[] args) {
System.out.println(sqrt(9));
}
public static double sqrt(long n){
double lastGuess = 1;
double nextGuess = (lastGuess + n / lastGuess) / 2;
while (true){
if (Math.abs(nextGuess - lastGuess) < 0.0000001){
return nextGuess;
}
lastGuess = nextGuess;
nextGuess = (lastGuess + n / lastGuess) / 2;
}
}
}