PTA JAVA编程题汇总(三)


设计一个矩形类Rectangle
class Rectangle
{
    double w,h;
    public Rectangle(double w1,double h1){w=w1;h=h1;}
    public double getArea()
    {
        return w*h;
    }
    public double getPerimeter()
    {
        return 2*(w+h);
    }
}

从抽象类shape类扩展出一个正五边形类
class RPentagon extends shape
{
private double a;
    @Override
    public double getArea() {
        return 5*Math.pow(a,2)/(4*Math.tan(36*Math.PI/180));
    }
    @Override
    public double getPerimeter() {
        return 5*a;
    }
    public RPentagon(double a1)
    {
        a=a1;
    }
 }


电话同步
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        TreeMap<String,Long> map = new TreeMap<>(((o1, o2) -> {
            String s1,s2;
            s1 = (String)o1;
            s2 = (String)o2;
            return s1.compareTo(s2);
        }));
        String pinyin;
        String[] data = new String[2];
        while(true){
            pinyin = cin.nextLine();
            if(pinyin.substring(0,3).equals("end")){
                break;
            }
            data = pinyin.split(" ");
            map.put(data[0],Long.parseLong(data[1]));
        }
        while(true){
            pinyin = cin.nextLine();
            if(pinyin.substring(0,3).equals("end")){
                break;
            }
            data = pinyin.split(" ");
            map.put(data[0],Long.parseLong(data[1]));
        }
        Set<String> keys = map.keySet();
        for(String key:keys){
            System.out.println(key+" "+map.get(key));
        }
    }
}
人名地名
import java.util.Scanner;
import java.util.TreeSet;
public class Main {


	public static void main(String[] args) {
		TreeSet<String> st = new TreeSet<>();
		Scanner sc = new Scanner(System.in);
		int count = sc.nextInt();
		while (count > 0) {
			String th = sc.next();
			st.add(th);
			sc.nextLine();
			count--;
		}
		for (String string : st) {
			System.out.println(string);
		}
	}
}


从抽象类shape类扩展出一个圆形类Circle
class  Circle extends shape
{
private double a;
    @Override
    public double getArea() {
        // TODO 自动生成的方法存根
        return Math.PI*a*a;
    }
    @Override
    public double getPerimeter() {
        // TODO 自动生成的方法存根
        return 2*a*Math.PI;
    }
    public  Circle(double r)
    {
        a=r;
    }
 }
数字格式异常   

import java.util.InputMismatchException;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
    public static void main(String[] args) {
        Scanner in =new Scanner(System.in);
        int m,n;
        while(true){
        try{
            m=in.nextInt();
            n=in.nextInt();
            System.out.println("Sum is "+(m+n));
            break;
            
        }catch(InputMismatchException e){
            System.out.println("Incorrect input and re-enter two integers:");
            in.nextLine();
            continue;
        }
     }
    }
}


猜你喜欢

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