PTA Java编程题汇总(二)

1、定义一个股票类Stock (10分) 

class Stock{
    private String s;
    public String name;
    public double previousClosingPrice;
    public double currentPrice;
    public Stock(String s,String n) {
        this.s = s;
        this.name = n;
    }
    public double changePercent(){
        return (currentPrice-previousClosingPrice)/previousClosingPrice;
    }
}
2、 求阶乘factorial  

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int yuan = in.nextInt();
        BigInteger jiecheng = new BigInteger("1");
        while ( yuan>0) {
            jiecheng=jiecheng.multiply(new BigInteger(yuan+""));
            yuan--;
        }
        System.out.println(jiecheng);
        in.close();
    }
}
3. 给定两个点的坐标,求解两个点的距离。

import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        DecimalFormat mFormat = new DecimalFormat("#.##");
        Scanner in = new Scanner(System.in);
        double a = in.nextDouble();
        double b = in.nextDouble();
        //System.out.println("The distance is "+mFormat.format(Math.hypot((in.nextDouble()-a), (in.nextDouble()-b))));
System.out.printf("The distance is %.2f",Math.hypot((in.nextDouble()-a), (in.nextDouble()-b)));
        in.close();
    }
}

4. 找出最大的对象  

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner in =new Scanner(System.in);
        String[] strings = new String[5];
        Integer[] ints = new Integer[5];
        int count = 0;
        while (count < 5) {
            strings[count] = in.next();
            count++;
        }
        count = 0;
        while (count < 5) {
            ints[count] = in.nextInt();
            count++;
        }
        System.out.println("Max string is "+max(strings));
        System.out.println("Max integer is "+max(ints));
        in.close();
    }
    public static Object max(@SuppressWarnings("rawtypes") Comparable[] a){
        Arrays.sort(a);
        return a[a.length-1];
    }
}

5. 从抽象类shape类扩展出一个正n边形  

class RegularPolygon extends shape {

    private int n;
    private double side;
    public RegularPolygon(int n, double side) {
        this.n = n;
        this.side = side;
    }

    public double getArea() {
        return n*side*side/(Math.tan(Math.toRadians(180/n))*4);
    }
    public double getPerimeter() {
        return n*side;
    }

}
6. 查找电话号码  

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<String, String> map = new HashMap<String, String>();
        String str = null;
        while (in.hasNext()){
            if (!(str = in.next()).equals("noname")) {
                map.put(str, in.next());
            } else {
                break;
            }
        }
        String s = in.next();
        if (map.containsKey(s)) {
            System.out.println((map.get(s)));
        } else
            System.out.println("Not found.");

        in.close();

    }

}
7. 日期加减  

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Calendar date = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        long time = in.nextLong();
        date.setTimeInMillis(time);
        System.out.println(format.format(date.getTime()));
        int day = in.nextInt();
        date.add(Calendar.DAY_OF_MONTH, day);
        System.out.println(format.format(date.getTime()));
        in.close();

    }

}

创建一个正六边形类实现接口IShape

class RHexagon implements IShape{
	double a;	
	public RHexagon(double a){
		this.a=a;
	}	
	@Override
	public double getArea() {
		// TODO 自动生成的方法存根
		return 6*a*a/(Math.tan(Math.toRadians(180/6))*4);
	}
	@Override
	public double getPerimeter() {
		// TODO 自动生成的方法存根
		return a*6;
	}
}


找出足球赛对阵方(10 分)
package ttms;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class PtaAnDongaini {
	public static void main(String[] args){
		Map<String,String> andong = new HashMap<>();
		andong.put("Australian", "Spain");
		andong.put("Holland", "Chile");
		andong.put("Cameroon", "Brazil");
		andong.put("Croatia", "Mexico");
		andong.put("Mexico", "Croatia");
		andong.put("Brazil", "Cameroon");
		andong.put("Chile", "Holland");
		andong.put("Spain", "Australian");
		Scanner in = new Scanner(System.in);
		String name = in.nextLine();
		//System.out.println(name+"......");
		if(andong.containsKey(name)){
			System.out.println(name+" team's rival is "+andong.get(name)+".");
		}else{
			System.out.println(name+"'s team has no match today.");
		}
	}
}




猜你喜欢

转载自blog.csdn.net/lulubaby_/article/details/78998257