目录
练习题36.求一个整数各位数字之和
练习题37.回文整数
练习题38.对三个数排序
练习题39.显示图案
练习题40.摄氏温度和华氏温度之间的转换
练习题41.数列求和
练习题42.估算π
练习题43.显示0和1构成的矩阵
练习题44.检测密码
练习题45.MyTriangle类
练习题46.数学:平方根的近似求法
练习题47.回文素数
练习题48.反素数
练习题49.梅森素数
练习题50.双素数
练习题51.游戏:双色子赌博
练习题52.几何问题:正多边形的面积
import java.util.Scanner;
public class Demo36 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a number:");
long number=scanner.nextLong();
int sum=sumDigits(number);
System.out.println(sum);
}
public static int sumDigits(long n){
int sum=0;
while(true){
sum+=n%10;
n=n/10;
if(n==0){
break;
}
}
return sum;
}
}
import java.util.Scanner;
public class Demo37 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a number:");
int number=scanner.nextInt();
System.out.println(isPailindrome(number));
}
public static int reverse(int number){
String numberStr=""+number;
String s="";
for(int i=numberStr.length()-1;i>=0;i--){
s+=numberStr.charAt(i);
}
return Integer.parseInt(s);
}
public static boolean isPailindrome(int number){
return reverse(number)==number;
}
}
import java.util.Scanner;
public class Demo38 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter three numbers:");
double num1=scanner.nextDouble();
double num2=scanner.nextDouble();
double num3=scanner.nextDouble();
//最终 num1存最小值 num2存中间值 num3存最大值
displaySortedNumbers(num1, num2, num3);
}
public static void displaySortedNumbers(double num1,double num2,double num3){
double temp=0.0;
if(num1>num2&&num1>num3){//num1最大
temp=num3;
num3=num1;
num1=temp;
if(num1>num2){
temp=num2;
num2=num1;
num1=temp;
}
}else if(num2>num1&&num2>num3){//num2最大
temp=num3;
num3=num2;
num2=temp;
if(num1>num2){
temp=num2;
num2=num1;
num1=temp;
}
}else if(num3>num1&&num3>num2){//num3最大
if(num1>num2){
temp=num2;
num2=num1;
num1=temp;
}
}
System.out.println(num1+"<"+num2+"<"+num3);
}
}
import java.util.Scanner;
public class Demo39 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the line:");
int line=scanner.nextInt();
displayPattern(line);
}
public static void displayPattern(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();
}
}
}
public class Demo40 {
public static void main(String[] args) {
for(int celsius=-10;celsius<=40;celsius++){
System.out.println(celsius+":"+celsiusToFahrenheit(celsius));
}
}
public static double celsiusToFahrenheit(double celsius){
return (9.0/5)*celsius+32;
}
public static double fahrenheitToCelsius(double fahrenheit){
return (5.0/9)*(fahrenheit-32);
}
}
public class Demo41 {
public static void main(String[] args) {
for(int i=1;i<=20;i++){
System.out.printf("%d : %8.4f\n",i,m(i));
}
}
public static double m(int i){
double sum=0.0;
for(int k=1;k<=i;k++){
sum+=1.0*k/(k+1);
}
return sum;
}
}
public class Demo42 {
public static void main(String[] args) {
for(int i=1;i<=901;i+=100){
System.out.println(i+":"+m(i));
}
}
public static double m(int i){
double pi=0.0;
double flag=1.0;
for(int k=1;k<=i;k++){
pi+=flag/(2*k-1);
flag=-flag;
}
return 4*pi;
}
}
import java.util.Random;
import java.util.Scanner;
public class Demo43 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the n:");
int n=scanner.nextInt();
printMatrix(n);
}
public static void printMatrix(int n){
Random random=new Random();//随机工具类Math.random()
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
System.out.print(random.nextInt(2)+" ");//[0,2)
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Demo44 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a password:");
String password=scanner.nextLine();
if(isPasswordValid(password)){
System.out.println("Valid Password");
}else{
System.out.println("Invalid Password");
}
}
public static boolean isPasswordValid(String password){
boolean condition1=isLengthMoreThanEight(password);
boolean condition2=isOnlyContainsLetterAndDigit(password);
boolean condition3=isContainsMoreThanTwoDigits(password);
if(condition1&&condition2&&condition3){
return true;
}else{
return false;
}
}
public static boolean isLengthMoreThanEight(String password){
return password.length()>=8;
}
public static boolean isOnlyContainsLetterAndDigit(String password){
for(int i=0;i='0'&&c<='9';
}
private static boolean isLetter(char c) {
return c>='a'&&c<='z'||c>='A'&&c<='Z';
}
public static boolean isContainsMoreThanTwoDigits(String password){
return getDigitCount(password)>=2;
}
private static int getDigitCount(String password) {
int count=0;
for(int i=0;i
import java.util.Scanner;
public class Demo45 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the three side of a triangle:");
double side1=scanner.nextDouble();
double side2=scanner.nextDouble();
double side3=scanner.nextDouble();
System.out.println(area(side1,side2,side3));
}
public static boolean isVaild(double side1,double side2,double side3){
if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1){
return true;
}else{
return false;
}
}
public static double area(double side1,double side2,double side3){
if(isVaild(side1, side2, side3)){
double s=(side1+side2+side3)/2;
double area=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}else{
throw new IllegalArgumentException("三边非法!");
}
}
}
import java.util.Scanner;
public class Demo46 {
public static void main(String[] args) {
System.out.print("Enter a number:");
System.out.println(sqrt(new Scanner(System.in).nextLong()));
}
public static double sqrt(long n){
double nextGuess=0.0;
double lastGuess=1.0;
while(true){
nextGuess=(lastGuess+n/lastGuess)/2;
if(Math.abs(nextGuess-lastGuess)<0.0001){
break;
}else{
lastGuess=nextGuess;
}
}
return nextGuess;
}
}
public class Demo47 {
public static void main(String[] args) {
int count=0;
int num=2;
while(true){
if(isSuShu(num)&&isHuiWen(num)){
System.out.print(num+" ");
count++;
if(count%10==0){
System.out.println();
}
if(count==100){
break;
}
}
num++;
}
}
public static boolean isSuShu(int num){
boolean flag=true;
for(int i=2;i
public class Demo48 {
public static void main(String[] args) {
int count=0;
int num=2;
while(true){
if(isSuShu(num)&&!isHuiWen(num)&&isSuShu(reverse(num))){
System.out.print(num+" ");
count++;
if(count%10==0){
System.out.println();
}
if(count==100){
break;
}
}
num++;
}
}
public static boolean isSuShu(int num){
boolean flag=true;
for(int i=2;i
public class Demo49 {
public static void main(String[] args) {
for(int p=1;p<=31;p++){
int num=(int) (Math.pow(2, p)-1);
if(isSuShu(num)){
System.out.println(p+":"+num);
}
}
}
public static boolean isSuShu(int num) {
boolean flag=true;
for(int i=2;i<=num/2;i++){
if(num%i==0){
flag=false;
break;
}
}
return flag;
}
}
public class Demo50 {
public static void main(String[] args) {
for(int i=2;i<998;i++){
if(Demo5_18.isSuShu(i)&&Demo5_18.isSuShu(i+2)){
System.out.println(i+":"+(i+2));
}
}
}
}
public class Demo51 {
public static void main(String[] args) {
//1.随机生成两个点数
//2.计算两个点数之和
//2.1如果和为 2 3 12 lose
//2.2如果和为 7 11 win
//2.3如果和为 4 5 6 8 9 10 继续随机生成两个点
//2.3.1 如果和为7 lose
//2.3.2 如果和为上次一样 win
//2.3.3 继续
boolean flag=true;//代表第一次
int lastSum=0;
while(true){
int p1=(int) (1+Math.random()*6);
int p2=(int) (1+Math.random()*6);
int sum=p1+p2;
System.out.println("You rolled "+p1+"+"+p2+"="+sum);
if(flag){
if(sum==2||sum==3||sum==12){
System.out.println("You lose");
break;
}else if(sum==7||sum==11){
System.out.println("You win");
break;
}else{
System.out.println("Point is "+sum);
lastSum=sum;
flag=false;
}
}else{
if(sum==lastSum){
System.out.println("You win");
break;
}else if(sum==7){
System.out.println("You lose");
break;
}else{
System.out.println("Point is "+sum);
lastSum=sum;
}
}
}
}
}
import java.util.Scanner;
public class Demo52 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the number of sides:");
int n=scanner.nextInt();
System.out.print("Enter the side:");
double side=scanner.nextDouble();
double area=area(n,side);
System.out.println(area);
}
public static double area(int n,double s){
double area=n*s*s/(4*Math.tan(Math.PI/n));
return area;
}
}