1.显示从2开始的20个回文素数 要求每十个换一行 间距一定
public class PrimeNumber {
// //判断是否为素数
// public static boolean isPrime(int n){
// for(int divisor = 2; divisor*divisor<=n; divisor++){
// if(n%divisor==0){
// return false;
// }
// }
// return true;
// }
//判断素数的另一种方法
public static boolean isPrime(int n){
for(int divisor=2; divisor <= n/2; divisor++){
if(n%divisor==0){
return false;
}
}
return true;
}
//判断回文数
public static boolean isPalindrome(int n){
String s = String.valueOf(n);//数字转字符串
int low = 0;
int heigh = s.length() - 1;
while(low<heigh){
if(s.charAt(low)!=s.charAt(heigh)){
return false;
}
low++;
heigh--;
}
return true;
}
public static void main(String[] args) {
int count = 0;
int n = 2;
do{
if(isPrime(n) && isPalindrome(n)){
count++;
if(count%10==0){
System.out.printf("%-5s%n", n);//%n:用在printf语句里的换行符 %5s:向前补齐5位
}else {
System.out.printf("%-5s", n);
}
}
n++;
}while(count<20);
}
}
2.递归方法求斐波那契数列的前20项
public class NumberUtil {
public static long fibo(int n){
if(n==1 || n==2){
return 1;
}else{
return fibo(n-1)+fibo(n-2);
}
}
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
System.out.println("fibo("+i+")="+fibo(i));
}
}
}
3在该类中实现两个静态函数
parseInt(char[]):int 将字符数组转换为整数
pardeInt(String):int 将字符串数组转换为整数
public class MyInteger {
public static int parseInt(char[] c){
int result = 0;
int scale = 1;
for(int i = c.length-1; i>=0; i--){
int d = c[i] -'0';
result = d*scale+result;
scale*=10;
}
return result;
}
public static int parseInt(String s){
int result = 0;
int scale = 1;
for (int i = s.length()-1; i >=0 ; i--) {
int d = s.charAt(i)-'0';
result = result+d*scale;
scale*=10;
}
return result;
}
public static void main(String[] args) {
char[] c = {'3','4','5'};//字符:'' char类型
char c1 = c[0];
int i = c[0] - '0'; //char - '0' 转变成int类型
System.out.println(c[0]);
System.out.println(MyInteger.parseInt(c));
String s = "1234";
System.out.println(MyInteger.parseInt(s));
}
}
4.一元二次方程计算类
public class QuadraticEquation {
private double a;
private double b;
private double c;
public QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
public double getDiscriminant(){
double discriminant;
discriminant = b*b - 4*a*c; //本类下可以直接访问私有对象
return discriminant;
}
public double getRoot1(){
return (-b+Math.sqrt(getDiscriminant()))/(2*a);
}
public double getRoot2(){
return (-b-Math.sqrt(getDiscriminant()))/(2*a);
}
public static void main(String[] args) {
double a, b, c;
Scanner sc = new Scanner(System.in);
System.out.println("请输入方程的三个系数:");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
QuadraticEquation q = new QuadraticEquation(a, b, c);
double discriminant = q.getDiscriminant();
System.out.println(discriminant);
if(discriminant<0){
System.out.println("该方程无解");
}else if(discriminant==0){
System.out.println("该方程有一个根:"+q.getRoot1());
}
else{
System.out.println("该方程有两个根:"+q.getRoot1()+" 和 "+q.getRoot2());
}
}
}
5.创建一个三角形类,包括计算面积的方法
```java
public class Triangle {
double a, b, c;//三角形三边边长
public Triangle(){
this.a = 0;
this.b = 0;
this.c = 0;
}
public Triangle(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;
}
public double area(){
double s = (a+b+c)/2.0;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));//海伦公式求解三角形面积
}
public static void main(String[] args) {
Triangle t = new Triangle(3, 4, 5);
System.out.println(t.area());
}
}