本文共 9447 字,大约阅读时间需要 31 分钟。
public class book { public static void main(String[] args) { int count=0; int base=0; int dick = 2; while(count<100) { if(isPrime(dick)&&!isPalindrome(dick)&&isPrime(reverse(dick))) { System.out.print(dick+" "); count++; } if(count%10==0&&count!=base) { System.out.print('\n'); base+=10; } dick++; } } public static boolean isPrime(int n) { for(int i=2;i<=n/2;i++) { if(n%i==0) return false; } return true; } public static boolean isPalindrome(int n) { String ass = Integer.toString(n); for(int i=0;i=0;i--) dick+=ass.charAt(i); return Integer.parseInt(dick); }}
public class book { public static void main(String[] args) { System.out.println("p 2^p-1"); System.out.println("-----------------"); for(int i=2;i<=31;i++) { int jb = (int)Math.pow(2,i)-1; if(isPrime(jb)) System.out.printf("%d\t\t%-2d\n",i,jb); } } public static boolean isPrime(int n) { for(int i=2;i<=n/2;i++) { if(n%i==0) return false; } return true; }}
public class book { public static void main(String[] args) { for(int i=2;i<=997;i++) { if(isPrime(i)&&isPrime(i+2)) System.out.println("("+i+", "+(i+2)+")"); } } public static boolean isPrime(int n) { for(int i=2;i<=n/2;i++) { if(n%i==0) return false; } return true; }}
public class book { public static void main(String[] args) { dice(); } public static boolean dice() { int d1 = (int)(Math.random()*6)+1; int d2 = (int)(Math.random()*6)+1; int sum = d1+d2; if(sum==2||sum==3||sum==12) { System.out.printf("You rolled %d+%d = %d\nYou lose\n",d1,d2,sum); return false; } else if(sum==7||sum==11) { System.out.printf("You rolled %d+%d = %d\nYou win\n",d1,d2,sum); return true; } else { System.out.printf("You rolled %d+%d = %d\n",d1,d2,sum); System.out.println("point is "+sum); int old = sum; do{ int j1 = (int)(Math.random()*6)+1; int j2 = (int)(Math.random()*6)+1; sum=j1+j2; System.out.printf("You rolled %d+%d = %d\n",j1,j2,sum); if(sum==7) { System.out.println("You lose"); return false; } else if(sum!=old) System.out.println("point is "+sum); }while(sum!=old); System.out.println("You win"); return true; } }}
import java.util.Scanner;public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a credit card number: "); String ass = input.next(); if(isValid(ass)) System.out.println(ass+" is valid"); else System.out.println(ass+" is invalid"); } public static boolean isValid(String number) { if(number.length()<13||number.length()>16) return false; else { return (sumOfDoubleEvenPlace(number)+sumOfOddPlace(number))%10==0; } } public static int sumOfDoubleEvenPlace(String number) { int sum=0; for(int i=number.length()-2;i>=0;i-=2) sum+=getDigit(2*(number.charAt(i)-'0')); return sum; } public static int getDigit(int number) { if(number<10) return number; else return number/10+number%10; } public static int sumOfOddPlace(String number) { int sum=0; for(int i=number.length()-1;i>=0;i-=2) sum+=(int)(number.charAt(i)-'0'); return sum; } public static boolean prefixMatched(String number) { char c1=number.charAt(0); char c2=number.charAt(1); return c1 == '4' || c1 == '5' || c1 == '6' || (c1 == '3') && (c2 == 7); }}
public class book { public static void main(String[] args) { int winCount=0; for(int i=0;i<10000;i++) { if(dice()) winCount++; } System.out.println("You won "+winCount+" times"); } public static boolean dice() { int d1 = (int)(Math.random()*6)+1; int d2 = (int)(Math.random()*6)+1; int sum = d1+d2; if(sum==2||sum==3||sum==12) { return false; } else if(sum==7||sum==11) { return true; } else { int old = sum; do{ int j1 = (int)(Math.random()*6)+1; int j2 = (int)(Math.random()*6)+1; sum=j1+j2; if(sum==7) { return false; } }while(sum!=old); return true; } }}
见3.21
import java.util.Scanner;public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the side: "); double ass = input.nextDouble(); System.out.println("The area of the pentagon is "+area(ass)); } public static double area(double side) { return 5*side*side/(4*Math.tan(3.141592654/5)); }}
import java.util.Scanner;public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter thr number of sides: "); int n=input.nextInt(); System.out.print("Enter the side: "); double ass = input.nextDouble(); System.out.println("The area of the polygon is "+area(n,ass)); } public static double area(int n,double side) { return n*side*side/(4*Math.tan(3.141592654/n)); }}
import java.util.Scanner;public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number : "); int n=input.nextInt(); System.out.print("Enter the width : "); int w = input.nextInt(); System.out.println("The format of the number is "+format(n,w)); } public static String format(int number,int width) { String ass = Integer.toString(number); int len = ass.length(); if(len>=width) return ass; else { String dick=""; for(int i=1;i<=width-len;i++) dick+='0'; return dick+ass; } }}
public class book { public static void main(String[] args) { for(int i=1;i<=100;i++) { System.out.print(getRandomUpperCaseLetter()+" "); if(i%10==0) System.out.print('\n'); } for(int i=1;i<=100;i++) { System.out.print(getRandomDigitCharacter()+" "); if(i%10==0) System.out.print('\n'); } } public static char getRandomCharacter(char ch1,char ch2) { return (char)(ch1+Math.random()*(ch2-ch1+1)); } public static char getRandomUpperCaseLetter() { return getRandomCharacter('A','Z'); } public static char getRandomDigitCharacter() { return getRandomCharacter('0','9'); }}
import java.util.Scanner;public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three points for p0,p1,p2: "); double x0 = input.nextDouble(); double y0 = input.nextDouble(); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); if(leftOfTheLine(x0,y0,x1,y1,x2,y2)) System.out.println("On the left"); else if(onTheLineSegment(x0,y0,x1,y1,x2,y2)) System.out.println("On the line segment"); else if(onTheSameLine(x0,y0,x1,y1,x2,y2)) System.out.println("On the same line"); else System.out.println("On the right"); } public static boolean leftOfTheLine(double x0,double y0, double x1,double y1,double x2,double y2) { return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)>0; } public static boolean onTheSameLine(double x0,double y0, double x1,double y1,double x2,double y2) { return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)==0; } public static boolean onTheLineSegment(double x0,double y0, double x1,double y1,double x2,double y2) { return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)==0&&x2>=x0&&x2<=x1; }}
转载地址:http://cuwai.baihongyu.com/