JAVA--PTA

大一Java基础课pta作业汇总

test1:

给出一个月的总天数.java

//编写程序,提示用户输入月份和年份,然后显示这个月的天数。
//输入格式:
//
//输入任意符合范围(1月~12月)的月份和(1900年~9999年)年份,且两个值之间空格分隔。
//输出格式:
//
//输出给定年份和月份的天数。
//输入样例:
//
//2 2000
//
//输出样例:
//
//29
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
64 MB


import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		
		int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
		int b[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
		Scanner scan=new Scanner(System.in);
		int m=scan.nextInt();
		int y=scan.nextInt();
		if((y%4==0&&y%100!=0)||y%400==0){
			System.out.println(b[m]);
		}
		else System.out.println(a[m]);		

	}

}

判断闰年.java

//根据输入的正整数y所代表的年份,计算输出该年份是否为闰年
//闰年的判断标准:
//
//能够被4整除且不能被100整除的年份
//
//或者能够被400整除的年份
//输入格式:
//
//输入n取值范围是 【1..3000】
//输出格式:
//
//是闰年,输出 yes
//
//非闰年,输出 no
//输入样例:
//
//在这里给出一组输入。例如:
//
//100
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//no
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.Scanner;
public class 判断闰年 {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		if((n%4==0&&n%100!=0)||n%400==0) {
			System.out.println("yes");
			
		}
		else 
			System.out.println("no");
	}

}

输出所有大于平均值的数.java

//本题要求编写程序,将输入的n个整数存入数组a中,然后计算这些数的平均值,再输出所有大于平均值的数。
//输入格式:
//
//输入在第1行中给出一个正整数n(1≤n≤10),第2行输入n个整数,其间以空格分隔。题目保证数据不超过长整型整数的范围。
//输出格式:
//
//输出在第1行给出平均值,保留2位小数。在第2行输出所有大于平均值的数,每个数的后面有一个空格;如果没有满足条件的数,则输出空行。
//
//如果输入的n不在有效范围内,则在一行中输出"Invalid."。
//输入样例1:
//
//10
//55 23 8 11 22 89 0 -1 78 186
//
//输出样例1:
//
//在这里给出相应的输出。例如:
//
//47.10
//55 89 78 186 
//
//输入样例2:
//
//0
//
//输出样例2:
//
//在这里给出相应的输出。例如:
//
//Invalid.
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int a[]=new int[1000];
		int sum=0;
		if(n>=1&&n<=10){
			for(int i=0;i<n;i++){
				a[i]=scan.nextInt();
				sum+=a[i];
			}
			double f=sum*1.0/n;
			System.out.printf("%.2f\n",f);
			for(int i=0;i<n;i++){
				if(a[i]>f)System.out.print(a[i]+" ");
			}
			
		}
		else System.out.println("Invalid.");
		
	}

}

统计正数和负数的个数然后计算这些数的平均值.java

//编写程序,输入未指定个数的整数,判断读入的正数有多少个,读入的负数有多少个,然后计算这些输入值得总和及平均值(不对0计数)。当输入为0时,表明程序结束。将平均值以double型数据显示。
//输入格式:
//
//输入在一行中给出一系列整数,其间以空格分隔。当读到0时,表示输入结束,该数字不要处
//输出格式:
//
//在第一行中输出正整数的个数;
//在第二行中输出负整数的个数;
//在第三行中输出这些输入值的总和(不对0计数);
//在第四行中输出这些输入值的平均值(double型数据)。
//输入样例:
//
//1 2 -1 3 0
//
//输出样例:
//
//3
//1
//5
//1.25
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
64 MB


import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		
		Scanner scan=new Scanner(System.in);
		int a[]=new int[1000];
		int sum=0;
		int i;
		int poNum=0;
		int neNum=0;
		for(i=0;;i++){
			a[i]=scan.nextInt();
			if(a[i]==0)break;
			if(a[i]<0)neNum++;
			if(a[i]>0)poNum++;
			sum+=a[i];
			
		}
		if(poNum+neNum!=0) {
		double aver=sum*1.0/(poNum+neNum);
		System.out.println(poNum);
		System.out.println(neNum);
		System.out.println(sum);
		System.out.println(aver);
		}
		
		
	}

}

用foreach求数组之和.java

//定义一个整型数组a, 数组长度通过键盘给出,使用foreach语句,求数组所有元素之和。
//输入格式:
//
//输入在第一行给出数组的长度
//第二行给出数组每个元素的值,空格隔开
//输出格式:
//
//输出和的值
//输入样例:
//
//在这里给出一组输入。例如:
//
//5
//1 2 3 4 5
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//15
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		
		Scanner scan=new Scanner(System.in);
		int a[]=new int[1000];
		int sum=0;
		int l=scan.nextInt();
		for(int i=0;i<l;i++){
			a[i]=scan.nextInt();
			sum+=a[i];
		}
			System.out.println(sum);
		
	}

}

直角三角形.java

//编写一个应用程序,读取用户任意输入的3个非零数值,判断它们是否可以作为直角三角形的3条边,如果可以,则打印这3条边,计算并显示这个三角形的面积。
//输入格式:
//
//输入三个非零数值。
//输出格式:
//
//如果不是直角三角形的三条边,则输出0.0;否则,输出三角形的面积
//输入样例:
//
//3
//4 
//5
//
//输出样例:
//
//6.0
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
64 MB


import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		
		Scanner scan=new Scanner(System.in);
		int a=scan.nextInt();
		int b=scan.nextInt();
		int c=scan.nextInt();
		int s=(a+b+c)/2;
		
		if(a+b>c&&a+c>b&&b+c>a){
			if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){
			double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
				System.out.println(area);
			}
			else System.out.println("0.0");
		}
		else System.out.println("0.0");

	}

}

test2:

Circle类.java

//a 定义圆类Circle,其中包括:
//
//    成员变量定义 private int radius
//    方法定义 包括下列要求
//        定义无参构造方法 ,给radius赋值为2,并添加语句System.out.println("this is a constructor");
//        定义有参构造方法 ,接收用户给给radius赋值,如果用户输入半径为<=0,则让半径的值为2,并添加语句System.out.println("this is a constructor with para");
//        为radius半径添加setter方法,接收用户输入的半径,如果用户输入半径为<=0,则让半径的值为2
//        为radius半径添加getter方法,返回用户输入的半径
//        定义求面积方法public int gerArea(),π使用Math.PI代替,面积的结果强制转换为int返回
//        定义toString方法,public String toString( )方法体为:
//        return "Circle [radius=" + radius + "]";
//
//b定义Main类,在main方法中,完成下列操作
//
//    .定义并创建Circle的第一个对象c1,并使用println方法输出c1
//    求c1的面积并输出
//    定义并创建Circle的第一个对象c2,并使用println方法输出c2
//    从键盘接收整数半径,并赋值给c2的半径,使用println方法输出c2
//    求c2的面积并输出
//    从键盘接收整数半径,并创建Circle的第三个对象c3,并将用户输入整数半径通过有参构造方法传递给出c3,使用println方法输出c3
//    求c3的面积并输出
//
//### 输入格式: 从键盘输入一个整数半径
//输出格式:
//
//分别输出c1和c2对象的信息
//输入样例:
//
//在这里给出一组输入。例如:
//
//4
//5
//
//-4
//-2
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//this is a constructor
//Circle [radius=2]
//c1:area=12
//this is a constructor
//Circle [radius=2]
//Circle [radius=4]
//c2:area=50
//this is a constructor with para
//Circle [radius=5]
//c3:area=78
//
//this is a constructor
//Circle [radius=2]
//c1:area=12
//this is a constructor
//Circle [radius=2]
//Circle [radius=2]
//c2:area=12
//this is a constructor with para
//Circle [radius=2]
//c3:area=12
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.Scanner;
class Circle{
	private int radius;
	public Circle () {
		radius=2;
		System.out.println("this is a constructor");
	}

	public Circle(int num) {
		radius=num;
		if(radius<=0) {
			radius=2;}
			System.out.println("this"
					+ " is a constructor with para");
		}
	public void setter(int num) {
		if(radius<=0) {
			radius=2;
		}
		
	}
	public int getter() {
		return radius;
	}
	public int gerArea() {
		return (int)(Math.PI*radius*radius);
	}
	public String toString() {
		return "Circle [radius=" + radius + "]";
	}
	public void setRadius(int num) {
		if(num<=0)num=2;
		radius=num;
	}
	
	
}
public class Main{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		Circle c1=new Circle();
		System.out.println(c1.toString());
		System.out.println("c1:area="+c1.gerArea());
		Circle c2=new Circle();
		System.out.println(c2);
		int num1=sc.nextInt();
		c2.setRadius(num1);
		System.out.println(c2.toString());
		System.out.println("c2:area="+c2.gerArea());
		Circle c3=new Circle(sc.nextInt());
		System.out.println(c3.toString());
		System.out.println("c3:area="+c3.gerArea());
		
		
		
	}
}

MyDate类.java

//构造日期类MyDate类,包含年月日,提供相应的get和set函数,提供void print()函数打印日期,提供int compare(MyDate d)测试当前对象和参数对象d的早晚,如果早则返回-1,晚则返回1,相等则返回0
//在main函数中,读入两个日期对象,输出第一个日期对象的信息,输出两个对象的比较结果
//输入格式:
//
//两个日期对象,第一个为当前日期对象的年月日,第二个为待比较日期对象的年月日
//输出格式:
//
//当前日期对象的信息,当前对象和待比较日期对象的比较结果
//输入样例:
//
//在这里给出一组输入。例如:
//
//2008 6 12 2009 6 22
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//6/12/2008 -1
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.Scanner;
class MyDate{
	private int year;
	private int month;
	private int day;
	public  MyDate(int year,int month,int day) {
		this.year=year;
		this.month=month;
		this.day=day;
	}
	
	public void print(){
		System.out.print(month+"/"+day+"/"+year+" ");
		
	}
	public int compare(MyDate d) {
		if(this.year<d.year) {
			return -1;
		}
		else if(this.year==d.year&&this.month<d.month) {
			return -1;
		}
		else if(this.year==d.year&&this.month==d.month&&this.day<d.day) {
			return -1;
		}
		else if(this.year==d.year&&this.month==d.month&&this.day==d.day) {
			return 0;
		}
		else return 1;
		}
	
	
}
public class Main{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		MyDate c=new MyDate(sc.nextInt(),sc.nextInt(),sc.nextInt());
		MyDate d=new MyDate(sc.nextInt(),sc.nextInt(),sc.nextInt());
		c.print();
		System.out.print(c.compare(d));
	}
}

构造方法.java

//请补充以下代码,完成输出要求。
//
//public class Main {
//
//    public Main(){
//
//        System.out.println("构造方法一被调用了");
//
//    }
//
//    public Main(int x){
//
//        this();
//
//        System.out.println("构造方法二被调用了");
//
//    }
//
//    public Main(boolean b){
//
//        this(1);
//
//        System.out.println("构造方法三被调用了");
//
//    }
//
//    public static void main(String[] args) {
//
//        
//
//    }
//
//}
//
//输入格式:
//
//无
//输出格式:
//
//输出以下三行:
//构造方法一被调用了
//构造方法二被调用了
//构造方法三被调用了
//输入样例:
//
//无
//
//输出样例:
//
//构造方法一被调用了
//构造方法二被调用了
//构造方法三被调用了
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


public class Main {
    public Main(){
        System.out.println("构造方法一被调用了");
    }
    public Main(int x){
        this();
        System.out.println("构造方法二被调用了");
    }
    public Main(boolean b){
        this(1);
        System.out.println("构造方法三被调用了");
    }
    public static void main(String[] args) {
        Main m=new Main(true);
    }
}

构造函数与toString.java

//定义一个有关人的Person类,内含属性:
//String name、int age、boolean gender、int id,所有的变量必须为私有(private)。
//注意:属性顺序请严格按照上述顺序依次出现。
//1.编写无参构造函数:
//
//    打印"This is constructor"。
//    将name,age,gender,id按照name,age,gender,id格式输出
//
//2.编写有参构造函数
//
//依次对name,age,gender赋值。
//3.覆盖toString函数:
//
//按照格式:类名 [name=, age=, gender=, id=]输出。建议使用Eclipse自动生成.
//4.对每个属性生成setter/getter方法
//5.main方法中
//
//    首先从屏幕读取n,代表要创建的对象个数。
//    然后输入n行name age gender , 调用上面2编写的有参构造函数新建对象。
//    然后将刚才创建的所有对象逆序输出。
//    接下来使用无参构造函数新建一个Person对象,并直接打印该对象。
//
//输入样例:
//
//3
//a 11 false
//b 12 true
//c 10 false
//
//输出样例:
//
//Person [name=c, age=10, gender=false, id=0]
//Person [name=b, age=12, gender=true, id=0]
//Person [name=a, age=11, gender=false, id=0]
//This is constructor
//null,0,false,0
//Person [name=null, age=0, gender=false, id=0]
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


 import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        Person p[] = new Person[n];
        for(int i=0; i<p.length; i++) {
            String name = sc.next();
            int age = sc.nextInt();
            boolean genter = sc.nextBoolean();
            p[i] = new Person(name,age,genter);
        }
        for(int i=p.length-1; i>=0;i--){
            p[i].toString();
        }      
        new Person();
        sc.close();
    } 
}
class Person{
    private String name = null;
    private int age = 0;
    private boolean gender = false;
    private int id = 0;
    Person(){
        System.out.println("This is constructor");
        System.out.println(name+","+age+","+gender+","+id);
        System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]");
    }
    Person(String name,int age,Boolean gender){
        this.name=name;
        this.age=age;
        this.gender=gender;
    }
    public String toString(){
        System.out.println("Person [name="+this.name+", age="+this.age+", gender="+this.gender+", id="+0+"]");
        return "1";
    }
}

设计一个BankAccount类.java

//设计一个BankAccount类,这个类包括:
//(1)一个int型的balance表时账户余额。
//(2)一个无参构造方法,将账户余额初始化为0。
//(3)一个带一个参数的构造方法,将账户余额初始化为该输入的参数。
//(4)一个getBlance()方法,返回账户余额。
//(5)一个withdraw()方法:带一个amount参数,并从账户余额中提取amount指定的款额。
//(6)一个deposit()方法:带一个amount参数,并将amount指定的款额存储到该银行账户上。
//设计一个Main类进行测试,分别输入账户余额、提取额度以及存款额度,并分别输出账户余额。
//输入格式:
//
//依次输入账户余额、提取额度、存款额度
//输出格式:
//
//依次输出初始账户余额、提取amount额度后的账户余额、存入amount后的账户余额
//输入样例:
//
//在这里给出一组输入。例如:
//
//700
//70
//7
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//700
//630
//637
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB



import java.util.Scanner;
class BankAccount{
	int balance;
	public BankAccount(){
		balance=0;
	}
	public BankAccount(int money){
		balance=money;
	}
	public int getBlance(){
		return balance;
	}
	public int withdraw(int amount){
		balance-=amount;
		return balance;
	}
	public int deposit(int amount){
		balance+=amount;
		return balance;
	}

	}
public class Main{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		BankAccount n=new BankAccount(sc.nextInt());
		System.out.println(n.getBlance());
		n.withdraw(sc.nextInt());
		System.out.println(n.getBlance());
		n.deposit(sc.nextInt());
		System.out.println(n.getBlance());
		
		
		
		
	}
}

test3:

Shape类.java

//定义一个形状类Shape,提供计算周长getPerimeter()和面积getArea()的函数
//定义一个子类正方形类Square继承自Shape类,拥有边长属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
//定义一个子类长方形类Rectangle继承自Square类,拥有长、宽属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
//定义一个子类圆形类Circle继承自Shape,拥有半径属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
//
//在main函数中,分别构造三个Shape类的变量,分别指向一个Square、Rectangle、Circle对象,并输出他们的周长、面积.
//输入格式:
//
//正方形类的边长
//长方形类的长宽
//圆类的半径
//输出格式:
//
//正方形的周长、面积
//长方形的周长、面积
//圆形的周长、面积
//输入样例:
//
//在这里给出一组输入。例如:
//
//1
//1 2
//2
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//4.00 1.00
//6.00 2.00
//12.57 12.57
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB



import java.util.Scanner;
class Shape{
	public double getPerimeter() {
		return 1;
	}
	public double getArea() {
		return 1;
	}
}
class Square extends Shape{
	private double length;
	public Square() {
		
	}
	public Square(double length) {
		this.length=length;
	}
	public double getPerimeter() {
		return 4.0*length;
	}
	public double getArea() {
		return length*length;
	}
}
class Rectangle extends Square{
	private double length;
	double width;
	public Rectangle() {
		
	}
	public Rectangle(double length,double width) {
		this.length=length;
		this.width=width;
	}
	public double getPerimeter() {
		return 2.0*(length+width);
	}
	public double getArea() {
		return length*width;
	}
}
class Circle extends Shape{
	double radius;
	public Circle() {
		
	}
	public Circle(double radius) {
		this.radius=radius;
	}
	public double getPerimeter() {
		return 2.0*Math.PI*radius;
	}
	public double getArea() {
		return Math.PI*radius*radius;
	}
}
public class Main{
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		Shape p1=new Square(scan.nextDouble());
		Shape p2=new Rectangle(scan.nextDouble(),scan.nextDouble());
		Shape p3=new Circle(scan.nextDouble());
		System.out.printf("%.2f %.2f\n",p1.getPerimeter(),p1.getArea());
		System.out.printf("%.2f %.2f\n",p2.getPerimeter(),p2.getArea());
		System.out.printf("%.2f %.2f\n",p3.getPerimeter(),p3.getArea());
	}
}

覆盖.java

//Java每个对象都继承自Object,都有equals、toString等方法。
//现在需要定义PersonOverride类并覆盖其toString与equals方法。
//1. 新建PersonOverride类
//
//a. 属性:String name、int age、boolean gender,所有的变量必须为私有(private)。
//
//b. 有参构造方法,参数为name, age, gender
//
//c. 无参构造方法,使用this(name, age,gender)调用有参构造函数。参数值分别为"default",1,true
//
//d.toString()方法返回格式为:name-age-gender
//
//e. equals方法需比较name、age、gender,这三者内容都相同,才返回true.
//2. main方法
//
//2.1 输入n1,使用无参构造函数创建n1个对象,放入数组persons1。
//2.2 输入n2,然后指定name age gender。每创建一个对象都使用equals方法比较该对象是否已经在数组中存在,如果不存在,才将该对象放入数组persons2。
//2.3 输出persons1数组中的所有对象
//2.4 输出persons2数组中的所有对象
//2.5 输出persons2中实际包含的对象的数量
//2.5 使用System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));输出PersonOverride的所有构造函数。
//
//提示:使用ArrayList代替数组大幅复简化代码,请尝试重构你的代码。
//输入样例:
//
//1
//3
//zhang 10 true
//zhang 10 true
//zhang 10 false
//
//输出样例:
//
//default-1-true
//zhang-10-true
//zhang-10-false
//2
//[public PersonOverride(), public PersonOverride(java.lang.String,int,boolean)]
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.*;
class PersonOverride{
	private String name;
	private int age;
	private boolean gender;
	
	public PersonOverride() {
		this("default",1,true);	
	}
	public PersonOverride(String name,int age,boolean gender) {
		this.name=name;
		this.age=age;
		this.gender=gender;
		}
	public String toString() {
		return name+"-"+age+"-"+gender;
	}
	public boolean equals(PersonOverride Per) {
		if(this.name.equals(Per.name)&&this.age==Per.age&&this.gender==Per.gender)
			return true;
		return false;
	}
	
}
public class Main{
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int n1=scan.nextInt();
		PersonOverride persons1[]=new PersonOverride[n1];
		for(int i=0;i<n1;i++){
			persons1[i]=new PersonOverride();
			System.out.println(persons1[i].toString());
		}
		int n2=scan.nextInt();
		PersonOverride persons2[]=new PersonOverride[n2];
		ArrayList<PersonOverride>array=new ArrayList<>();
		persons2[0]=new PersonOverride(scan.next(),scan.nextInt(),scan.nextBoolean());
		array.add(persons2[0]);
		for(int i=1;i<n2;i++) {
			persons2[i]=new PersonOverride(scan.next(),scan.nextInt(),scan.nextBoolean());
			Boolean flag=true;
			for(int j=0;j<i;j++) {
			if(persons2[i].equals(persons2[j])) {
				flag=false;
				break;
			}
			
			}
			if(flag==true)array.add(persons2[i]);
			
			
			
			
			
		}
		
		int num=array.size();
		for(int i=0;i<array.size();i++) {
			PersonOverride p=array.get(i);
			System.out.println(p.toString());
		}
		System.out.println(num);
		System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
		
	}
}









图形继承.java

//编写程序,实现图形类的继承,并定义相应类对象并进行测试。
//
//    类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
//    类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
//    类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
//    类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积
//    类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积
//    注意:
//
//    每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名
//    每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
//    输出的数值均保留两位小数
//
//主方法内,主要实现四个功能(1-4):
//从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积;
//从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积;
//从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积;
//从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;
//
//假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format
//输入格式:
//
//共四种合法输入
//
//    1 圆半径
//    2 矩形宽、长
//    3 球半径
//    4 立方体宽、长、高
//
//输出格式:
//
//按照以上需求提示依次输出
//输入样例1:
//
//在这里给出一组输入。例如:
//
//1 1.0
//
//输出样例1:
//
//在这里给出相应的输出。例如:
//
//Constructing Shape
//Constructing Circle
//Circle's area:3.14
//
//输入样例2:
//
//在这里给出一组输入。例如:
//
//4 3.6 2.1 0.01211
//
//输出样例2:
//
//在这里给出相应的输出。例如:
//
//Constructing Shape
//Constructing Rectangle
//Constructing Box
//Box's surface area:15.26
//Box's volume:0.09
//
//输入样例3:
//
//在这里给出一组输入。例如:
//
//2 -2.3 5.110
//
//输出样例2:
//
//在这里给出相应的输出。例如:
//
//Wrong Format
//
//代码长度限制
//8 KB
//时间限制
//400 ms
//内存限制
//64 MB



import java.util.Scanner;
class Shape{
	public Shape() {
		System.out.println("Constructing Shape");
	}
	public double getArea() {
		return 0.0;
	}
}
class Circle extends Shape{
	private double radius;
	public Circle() {
		System.out.println("Constructing Circle");
	}
	public void setRadius(double radius) {
		this.radius=radius;
	}
	public double getArea() {
		return Math.PI*radius*radius;
	}

}
class Rectangle extends Shape{
	private double width;
	private double length;
	public Rectangle() {
		System.out.println("Constructing Rectangle");
	}
	public void setElement(double width,double length) {
		this.width=width;
		this.length=length;
	}
	public double getArea() {
		return width*length;
	}
}
class Ball extends Circle{
	private double radius;
	public Ball() {
		System.out.println("Constructing Ball");
	}
	public void setRadius(double radius) {
		this.radius=radius;
	}
	
	public double getArea() {
		return 4.0*Math.PI*radius*radius;
		
	}
	public double getVolume() {
		return 4.0/3.0*Math.PI*radius*radius*radius;
	}
}
class Box extends Rectangle{
	private double length;
	private double width;
	private double height;
	public Box() {
		System.out.println("Constructing Box");
	}
	public void setElements(double length,double width,double height) {	
		this.length=length;
		this.width=width;
	this.height=height;
	}
	public double getArea() {	

		return 2.0*(length*width+length*height+width*height);
	}
	public double getVolume() {
	return length*width*height;
	}
	
}
public class Main{
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int num=scan.nextInt();
		if(num==1) {
			double radius=scan.nextDouble();
			if(radius<=0)System.out.println("Wrong Format");
			else {
			Circle circle=new Circle();
			circle.setRadius(radius);
			System.out.printf("Circle's area:%.2f\n",circle.getArea());
		}}
		else if(num==2) {
			double length=scan.nextDouble();
			double width=scan.nextDouble();
			if(length<=0||width<=0)System.out.println("Wrong Format");
			else {
			Rectangle rectangle=new Rectangle();
			rectangle.setElement(width,length);
			System.out.printf("Rectangle's area:%.2f\n",rectangle.getArea());
		}}
		else if(num==3) {
			double radius=scan.nextDouble();
			if(radius<=0)System.out.println("Wrong Format");
			else {
			Ball ball=new Ball();
			ball.setRadius(radius);
			System.out.printf("Ball's surface area:%.2f\n",ball.getArea());
			System.out.printf("Ball's volume:%.2f\n",ball.getVolume());
		}}
		else if(num==4) {
			double length=scan.nextDouble();
			double width=scan.nextDouble();
			double height=scan.nextDouble();
			if(length<=0||width<=0||height<=0)System.out.println("Wrong Format");
			else {
			Box box=new Box();
			box.setElements(length,width,height);
			System.out.printf("Box's surface area:%.2f\n",box.getArea());
			System.out.printf("Box's volume:%.2f\n",box.getVolume());
		}}
		else {
			System.out.println("Wrong Format");
		}
		
	}
}

test4:

USB接口的定义.java

//定义一个USB接口,并通过Mouse和U盘类实现它,具体要求是:
//
//1.接口名字为USB,里面包括两个抽象方法:
//
//void work();描述可以工作
//
//void stop(); 描述停止工作
//
//2.完成类Mouse,实现接口USB,实现两个方法:
//
//work方法输出“我点点点”;
//
//stop方法输出 “我不能点了”;
//
//3.完成类UPan,实现接口USB,实现两个方法:
//
//work方法输出“我存存存”;
//
//stop方法输出 “我走了”;
//
//4测试类Main中,main方法中
//
//定义接口变量usb1 ,存放鼠标对象,然后调用work和stop方法
//
//定义接口数组usbs,包含两个元素,第0个元素存放一个Upan对象,第1个元素存放Mouse对象,循环数组,对每一个元素都调用work和stop方法。
//输入格式:
//输出格式:
//
//输出方法调用的结果
//输入样例:
//
//在这里给出一组输入。例如:
//
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//我点点点
//我不能点了
//我存存存
//我走了
//我点点点
//我不能点了
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


interface USB{
	public void work();
	public void stop();
}
class Mouse implements USB{
	public void work() {
		System.out.println("我点点点");
	}
	public void stop() {
		System.out.println("我不能点了");
	}
}
class UPan implements USB{
	public void work() {
		System.out.println("我存存存");
	}
	public void stop() {
		System.out.println("我走了");
	}
}
public class Main{
	public static void main(String[] args) {
		USB usb1;
		usb1=new Mouse();
		usb1.work();
		usb1.stop();
		USB usbs[] = new USB[2];
		usbs[0]=new UPan();
		usbs[1]=new Mouse();
		for(int i=0;i<2;i++) {
			usbs[i].work();
			usbs[i].stop();
		}
		
	}
}

接口四则计算器.java

//
//    利用接口做参数,写个计算器,能完成加减乘除运算。
//
//    定义一个接口ICompute含有一个方法int computer(int n, int m)。
//    定义Add类实现接口ICompute,实现computer方法,求m,n之和
//    定义Sub类实现接口ICompute,实现computer方法,求n-m之差
//    定义Main类,在里面输入两个整数a, b,利用Add类和Sub类的computer方法,求第一个数a和第二个数b之和,输出和,第一个数a和第二个数b之差,输出差。
//
//输入格式:
//
//输入在一行中给出2个整数
//输出格式:
//
//输出两个数的和、差
//输入样例:
//
//在这里给出一组输入。例如:
//
//6 7
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//13
//-1
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB



import java.util.*;
interface ICompute{
	
	public abstract int computer();
}
class Add implements ICompute{
	int n,m;
	public Add(){
		
}
	public Add(int n,int m){
		this.n=n;
		this.m=m;
	}
	@Override
	public int computer() {
		return n+m;
	}
	
}
class Sub implements ICompute{
	int n,m;
	public Sub(){
		
	}
	public Sub(int n,int m){
		this.n=n;
		this.m=m;
	}
	@Override
	public int computer() {
		return n-m;
	}
	
}
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int m=in.nextInt();
		ICompute icompute1=new Add(n,m);
		ICompute icompute2=new Sub(n,m);
		System.out.println(icompute1.computer());
		System.out.println(icompute2.computer());
	}
}

嵌套类静态嵌套类.java

//定义类ArrayUtils,在该类内部创建一个静态嵌套类PairResult,该嵌套类包含:
//属性: private double min与private double max,用于存放最小值最大值
//方法:toString方法,格式见下面的输出样例。
//
//为ArrayUtils类创建一个静态方法PairResult findMinMax(double[] values),对传递进来的数组找到其中的最大值和最小值并返回PairResult对象。
//main方法说明
//
//    输入n,创建大小为n的double型数组
//    依次输入n个double型数值放入数组
//    调用findMinMax方法得到结果,并输出。
//    最后使用System.out.println(ArrayUtils.PairResult.class)打印标识信息
//
//输入样例
//
//5
//0 -1 1 1.1 1.1
//
//输出样例
//
//PairResult [min=-1.0, max=1.1]
//\\这里打印标识
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.*;
class ArrayUtils{
	static class PairResult{
		private double min;
		private double max;
		@Override
		public String toString() {
			return "PairResult [min=" + min + ", max=" + max + "]";
		}
		public double getMin() {
			return min;
		}
		public void setMin(double min) {
			this.min = min;
		}
		public double getMax() {
			return max;
		}
		public void setMax(double max) {
			this.max = max;
		}
		
	}
	PairResult findMinMax(Double[] str){
		PairResult p=new PairResult();
		double max=str[0];
		double min=str[0];
		for(int i=0;i<str.length;i++){
			if(max<str[i])max=str[i];
			else if(min>str[i])min=str[i];
		}
		p.setMax(max);
		p.setMin(min);
		return p;
	}
}
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		Double str[]=new Double[n];
		for(int i=0;i<n;i++){
			double num=in.nextDouble();
			str[i]=num;
		}
		ArrayUtils m=new ArrayUtils();
		System.out.println(m.findMinMax(str).toString());
		System.out.println(ArrayUtils.PairResult.class);
	}
}

设计一个Shape及其子类Oval.java

//编写一个完整的Java Application 程序。包含类Shape、类Oval、类ShapeTest,具体要求如下:
//(1)编写一个抽象类Shape表示形状对象,包含以下成员
//①属性:
//PI:double型常数,值为3.1415926;
//②方法:
//
//    double area(), 抽象方法;
//    double perimeter(),抽象方法;
//    (2)编写一个Shape类的子类Oval,表示椭圆对象,包含以下成员
//    ①属性:
//    a:私有,double型,长轴半径;
//    b:私有,double型,短轴半径;
//    ②方法:
//    Oval(double a,double b), 构造方法,用参数设置椭圆的长轴半径和短轴半径
//    Oval(),构造方法,将椭圆的长轴半径和短轴半径都初始化为0。
//    double area(),重写Shape类中的area方法,返回椭圆的面积( )
//    double perimeter(),重写Shape类中的perimeter方法,返回椭圆的周长( )
//    public String toString( ),将把当前椭圆对象的转换成字符串形式,例如长轴半径为10.0,短轴半径为5,返回字符串"Oval(a:10.0,b:5.0)"。
//    (3)编写公共类Main,实现如下功能
//    输入长轴半径和短轴半径,并创建一个椭圆对象;
//    分别用area和perimeter方法,求出以上椭圆的面积和宽度并输出,输出过程中要求使用到toString方法,输出格式如下:
//
//输入格式:
//
//输入长轴半径和短轴半径
//输出格式:
//
//输出椭圆的面积和周长。
//输入样例:
//
//8 6
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//The area of Oval(a:8.0,b:6.0) is 150.79644480000002
//The perimeterof Oval(a:8.0,b:6.0) is 44.42882862370954
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.*;
 abstract class Shape{
	public final static double PI=3.1415926;
	public abstract double area();
	public abstract double perimeter();
}
class Oval extends Shape{
	private double a;
	private double b;
	public Oval(){
		a=0.0;
		b=0.0;
	}
	public Oval(double a,double b){
		this.a=a;
		this.b=b;
	}
	@Override
	public double area() {
		return PI*a*b;
	}
	@Override
	public double perimeter() {
		return 2.0*PI*Math.sqrt((this.a*this.a+this.b*this.b)/2);
	}
	public String toString(){
		return "Oval(a:"+a+",b:"+b+")";
	}
	
}
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		double a=in.nextDouble();
		double b=in.nextDouble();
		Oval oval=new Oval(a,b);
		System.out.println("The area of "+oval.toString()+" is "+oval.area());
		System.out.println("The perimeterof "+oval.toString()+" is "+oval.perimeter());
	}
}

形状继承.java

//本题描述
//
//1.定义抽象类Shape
//属性:不可变静态常量double PI,值为3.14,
//抽象方法:public double getPerimeter(),public double getArea()
//
//2.Rectangle与Circle类均继承自Shape类。
//Rectangle类(属性:int width,length)、Circle类(属性:int radius)。
//带参构造方法为Rectangle(int width,int length),Circle(int radius)。
//toString方法(Eclipse自动生成)
//
//3.编写double sumAllArea方法计算并返回传入的形状数组中所有对象的面积和与
//double sumAllPerimeter方法计算并返回传入的形状数组中所有对象的周长和。
//
//4.main方法
//4.1 输入整型值n,然后建立n个不同的形状。如果输入rect,则依次输入宽、长。如果输入cir,则输入半径。
//4.2 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。 提示:使用Arrays.toString。
//4.3 最后输出每个形状的类型与父类型.使用类似shape.getClass() //获得类型, shape.getClass().getSuperclass() //获得父类型;
//
//注意:处理输入的时候使用混合使用nextInt与nextLine需注意行尾回车换行问题。
//思考
//
//    你觉得sumAllArea和sumAllPerimeter方法放在哪个类中更合适?
//    是否应该声明为static?
//
//输入样例:
//
//4
//rect
//3 1
//rect
//1 5
//cir
//1
//cir
//2
//
//输出样例:
//
//38.84
//23.700000000000003
//[Rectangle [width=3, length=1], Rectangle [width=1, length=5], Circle [radius=1], Circle [radius=2]]
//class Rectangle,class Shape
//class Rectangle,class Shape
//class Circle,class Shape
//class Circle,class Shape
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.*;
abstract class Shape{
	public final static double PI=3.14;
	public abstract double getPerimeter();
	public abstract double getArea();
}
class Rectangle extends Shape{
	int width,length;

	public Rectangle(int width, int length) {
		super();
		this.width = width;
		this.length = length;
	}
	

	@Override
	public String toString() {
		return "Rectangle [width=" + width + ", length=" + length + "]";
	}


	@Override
	public double getPerimeter() {
		return 2.0*(width+length);
	}

	@Override
	public double getArea() {
		return width*length;
	}
	
}
class Circle extends Shape{
	int radius;

	public Circle(int radius) {
		super();
		this.radius = radius;
	}

	@Override
	public String toString() {
		return "Circle [radius=" + radius + "]";
	}

	@Override
	public double getPerimeter() {
		return 2.0*PI*radius;
	}

	@Override
	public double getArea() {
		return PI*radius*radius;
	}
	
	
}
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		in.nextLine();
		Shape s[]=new Shape[n]; 
		double sumAllPerimeter=0.0,sumAllArea=0.0;
		for(int i=0;i<n;i++) {
			String str=in.next();
			if(str.equals("rect")) {
				int width,length;
				width=in.nextInt();
				length=in.nextInt();
				in.nextLine();
				s[i]=new Rectangle(width,length);
			}else if(str.equals("cir")) {
				int radius;
				radius=in.nextInt();
				in.nextLine();
				s[i]=new Circle(radius);
			}
			
			sumAllArea+=s[i].getArea();
			sumAllPerimeter+=s[i].getPerimeter();
		}
		in.close();
		System.out.println(sumAllPerimeter);
		System.out.println(sumAllArea);
		System.out.print("[");
		for(int i=0;i<n;i++) {
			if(i!=0)System.out.print(", ");
			System.out.print(s[i].toString());
		}
		System.out.println("]");
		for(int i=0;i<n;i++) {
			if(s[i] instanceof Rectangle) {
				System.out.println("class Rectangle,class Shape");
			}else if(s[i] instanceof Circle) {
				System.out.println("class Circle,class Shape");
			}
		}
	}
}

test5:

成绩录入时的及格与不及格人数统计.java

//编写一个程序进行一个班某门课程成绩的录入,能够控制录入成绩总人数,对录入成绩统计其及格人数和不及格人数。设计一个异常类,当输入的成绩小0分或大于100分时,抛出该异常类对象,程序将捕捉这个异常对象,并调用执行该异常类对象的toString()方法,该方法获取当前无效分数值,并返回一个此分数无效的字符串。
//输入格式:
//
//从键盘中输入学生人数n
//
//从键盘中输入第1个学生的成绩
//
//从键盘中输入第2个学生的成绩
//
//...
//
//从键盘中输入第n个学生的成绩
//
//(注:当输入的成绩无效时(即分数为小于0或大于100)可重新输入,且输出端会输出此分数无效的提醒。)
//输出格式:
//
//显示及格总人数
//
//显示不及格总人数
//输入样例:
//
//在这里给出一组输入。例如:
//
//3
//100
//30
//60
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//2
//1
//
//输入样例:
//
//在这里给出一组输入。例如:
//
//2
//200
//69
//30
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//200invalid!
//1
//1
//
//代码长度限制
//16 KB
//时间限制
//6000 ms
//内存限制
//64 MB


import java.util.*;
class MyException extends Exception{
	int score;
	
	public MyException() {
		
	}
	public MyException(int score) {
		this.score=score;
		System.out.println(score+"invalid!");
		
	}
	
	
}
public class Main{
	public static void main(String[] args) {
			Scanner in=new Scanner(System.in);
			int good=0;
			int bad=0;
			int n=in.nextInt();
			for(int i=0;i<n;i++) {
				try {
				int score=in.nextInt();
				if(score<0||score>100) {
					throw new MyException(score);
					
				}
				else if(score<60&&score>=0)bad++;
				else good++;
			}
			catch(MyException e){
				i--;
			}
		}
		System.out.println(good);	
		System.out.println(bad);
	}
}

求圆面积自定义异常类.java

//计算圆的面积,其中PI取3.14,圆半径为负数时应抛出异常,输出相应提示。根据提供的主类信息,编写Circle类和CircleException类,以及在相关方法中抛出异常。
//函数接口定义:
//裁判测试程序:
//
//在这里给出主类
//
//import java.util.*;
//
//public class Main {
//
//
// public static void main(String[] args) {
//
//        double s=0;
//
//        Scanner sc=new Scanner(System.in);
//
//        double r1,r2;
//
//        r1=sc.nextDouble();
//
//        r2=sc.nextDouble();
//
//        Circle c1=new Circle(r1);
//
//        Circle c2=new Circle(r2);
//
//        try{
//
//        s = c1.area();
//
//        System.out.println(s);
//
//        s = c2.area();
//
//        System.out.println(s);
//
//        }
//
//        catch (CircleException e){
//
//            e.print();
//
//        }
//
//   }
//
//}
//
//
//
///* 请在这里填写答案 编写Circle 和CircleException类*/
//
//输入样例:
//
//在这里给出一组输入。例如:
//
//3.5 -3.5
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//38.465
//圆半径为-3.5不合理
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB



class Circle{
	public double radius;
	static final double PI=3.14;
	public Circle(){
		
	}
	public Circle(double radius){
		this.radius=radius;
	}
	public double area()throws CircleException {
		
		if(radius<0)throw new CircleException(radius); 
		
		return PI*radius*radius;
	}
	
}
class CircleException extends Exception{
		double radius;
		public CircleException(){
			
		}
		public CircleException(double radius){
			this.radius=radius;
		}
		public void print(){
			System.out.println("圆半径为"+radius+"不合理");
		}
	}

异常多种类型异常的捕获.java

//如果try块中的代码有可能抛出多种异常,且这些异常之间可能存在继承关系,那么在捕获异常的时候需要注意捕获顺序。
//
//补全下列代码,使得程序正常运行。
//裁判测试程序:
//
//public static void main(String[] args) {
//
//    Scanner sc = new Scanner(System.in);
//
//    while (sc.hasNext()) {
//
//        String choice = sc.next();
//
//        try {
//
//            if (choice.equals("number"))
//
//                throw new NumberFormatException();
//
//            else if (choice.equals("illegal")) {
//
//                throw new IllegalArgumentException();
//
//            } else if (choice.equals("except")) {
//
//                throw new Exception();
//
//            } else
//
//            break;
//
//        }
//
//        /*这里放置你的答案*/
//
//    }//end while
//
//    sc.close();
//
//}
//
//###输出说明
//在catch块中要输出两行信息:
//第1行:要输出自定义信息。如下面输出样例的number format exception
//第2行:使用System.out.println(e)输出异常信息,e是所产生的异常。
//输入样例
//
//number illegal except quit
//
//输出样例
//
//number format exception
//java.lang.NumberFormatException
//illegal argument exception
//java.lang.IllegalArgumentException
//other exception
//java.lang.Exception
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


 catch(Exception e){//注意catch中的异常类型如果满足子父类关系,子类必须声明在父类前面
	        	if(choice.equals("number"))System.out.println("number format exception");
	        	else if(choice.equals("illegal"))System.out.println("illegal argument exception");
	        	else if(choice.equals("except"))System.out.println("other exception");
	        	System.out.println(e);
	        }

test6:

StringBuilder.java

//输入3个整数n、begin、end。
//首先,使用如下代码:
//
//for(int i=0;i<n;i++)
//
//将从0到n-1的数字拼接为字符串str。如,n=12,则拼接出来的字符串为01234567891011
//
//最后截取字符串str从begin到end(包括begin,但不包括end)之间的字符串,并输出。
//输入样例:
//
//10
//5
//8
//1000
//800
//900
//
//输出样例:
//
//567
//0330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533
//
//代码长度限制
//16 KB
//时间限制
//800 ms
//内存限制
//64 MB



import java.util.*;
public class Main{
    public static void main(String[] args){
    Scanner in=new Scanner(System.in);
    while(in.hasNext()){
    int n=in.nextInt();
    int begin=in.nextInt();
    int end=in.nextInt();
    StringBuilder sb=new StringBuilder();
    for(int i=0;i<n;i++){
    sb.append(i);
    }
    
    
    System.out.println(sb.substring(begin,end));
    }
    }
}

单词替换.java

//设计一个对字符串中的单词查找替换方法,实现对英文字符串中所有待替换单词的查找与替换。
//输入格式:
//
//首行输入母字符串,第二行输入查询的单词,第三行输入替换后的单词。
//输出格式:
//
//完成查找替换后的完整字符串
//输入样例:
//
//在这里给出一组输入。例如:
//
//Although I am without you, I will always be ou you
//ou
//with
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//Although I am without you, I will always be with you
//
//代码长度限制
//16 KB
//时间限制
//1000 ms
//内存限制
//64 MB


import java.util.*;
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		String str=in.nextLine();
		String strOld=in.next();
		String strNew=in.next();
		String str1[]=str.split(" ");
		for(int i=0;i<str1.length;i++){
			if(str1[i].equals(strOld)){
				str1[i]=strNew;
			}
			if(i!=str1.length-1){
				System.out.print(str1[i]+" ");
			}else{
				System.out.print(str1[i]);
			}
		}
		
		System.out.println();
		in.close();
	}
	
}

键盘输入一行字符统计.java

//统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数
//输入格式:
//
//通过键盘输入一行字符(任意字符)
//输出格式:
//
//统计一行字符串中的中英文字母个数、空格个数、数字个数、其他字符个数
//输入样例:
//
//rwrwewre2345asdJSJQI%^&(&   *&sdf YY( 2342-k'
//
//输出样例:
//
//字母个数:22
//数字个数:8
//空格个数:5
//其他字符个数:10
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


import java.util.*;
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		String str=in.nextLine();
		int letter=0;
		int blank=0;
		int num=0;
		int others=0;
		for(int i=0;i<str.length();i++) {
			if(str.charAt(i)>='a'&&str.charAt(i)<='z'||str.charAt(i)>='A'&&str.charAt(i)<='Z') {
				letter++;
			}else if(str.charAt(i)>='0'&&str.charAt(i)<='9') {
				num++;
			}else if(str.charAt(i)==' ') {
				blank++;
			}else {
				others++;
			}
		}
		System.out.println("字母个数:"+letter);
		System.out.println("数字个数:"+num);
		System.out.println("空格个数:"+blank);
		System.out.println("其他字符个数:"+others);
		
	}
}

面向对象基础Object.java

//输入整数n,创建n个对象,放入同一个数组中。
//
//如果输入c,则new Computer(); //注意:Computer是系统中已有的类,无需自己编写
//
//如果输入d,则根据随后的输入创建Double类型对象。
//
//如果输入i,则根据随后的输入创建Integer类型对象。
//
//如果输入s,则根据随后的输入创建String类型对象。
//
//如果不是以上这些输入,则不创建对象,而是将null存入数组相应位置。
//
//最后倒序输出数组中的所有对象,如果数组中相应位置的元素为null则不输出。
//裁判测试程序:
//
//public static void main(String[] args) {
//    Scanner sc = new Scanner(System.in);
//    //这边是你的代码
//    sc.close();
//}
//
//输入样例:
//
//5
//c
//d 2.3
//other
//i 10
//s Test
//
//输出样例:
//
//Test
//10
//2.3
这行显示Computer对象toString方法
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


int n=sc.nextInt();
	    Object obj[]=new Object[n];
	    for(int i=0;i<n;i++) {
	    	String str=new String(sc.next());
	    	char ch=str.charAt(0);
	    	if(ch=='c') {
	    		obj[i]=new Computer();
	    	}
	    	else if(ch=='d') {
	    		Double dou=sc.nextDouble();
	    		obj[i]=dou;
	    		
	    	}
	    	else if(ch=='i') {
	    		Integer in=sc.nextInt();
	    		obj[i]=in;
	    	}
	    	else if(ch=='s') {
	    		String st=sc.next();
	    		obj[i]=st;
	    	}
	    	else {
	    		obj[i]=null;
	    	}
	    }
	    for(int i=n-1;i>=0;i--) {
	    	if(obj[i]==null)continue;
	    	System.out.println(obj[i]);
	    }

人口统计.java

//本题运行时要求键盘输入10个人员的信息(每一个人信息包括:姓名,性别,年龄,民族),要求同学实现一个函数,统计民族是“汉族”的人数。
//函数接口定义:
//
//public static int numofHan(String data[])
//
//其中 data[] 是传入的参数。 data[]中的每一个元素都是一个完整的人员信息字符串,该字符串由“姓名,性别,年龄,民族”,各项之间用英文半角的逗号分隔。函数须返回 值是汉族的人数。
//裁判测试程序样例:
//
//import java.util.Scanner;
//
//
//public class Main {
//
//
//    public static void main(String[] args) {
//
//        final int HUMANNUM=10;
//
//        String persons[]=new String[HUMANNUM];
//
//        Scanner in=new Scanner(System.in);
//
//        for(int i=0;i<persons.length;i++)
//
//            persons[i]=in.nextLine();
//
//        int result=numofHan(persons);
//
//        System.out.println(result);
//
//    
//
//    }
//
//    
//
//    /*在此处给出函数numofHan()*/
//
//    
//
//
//}
//
//
//输入样例:
//
//Tom_1,男,19,汉族
//Tom_2,女,18,汉族
//Tom_3,男,20,满族
//Tom_4,男,18,汉族
//Tom_5,男,19,汉族人
//Tom_6,女,17,汉族
//Tom_7,男,19,蒙古族
//汉族朋友_1,男,18,汉族
//Tom_8,male,19,老外
//Tom_9,female,20,汉族
//
//输出样例:
//
//7
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB



 public static int numofHan(String data[]){
      int n=0;

       for(int i=0 ; i<data.length ; i++){
           if(data[i].indexOf("汉族")>=0){//也可以写成data[i].indexOf("汉族",5)>=0
                 n++;
           }
       }

       return n;   
   }

随机数使用蒙特卡罗法计算圆周率的值.java

package test6;

//灏濊瘯浣跨敤钂欑壒鍗$綏娉曡绠楀渾鍛ㄧ巼锛埾�锛夌殑鍊笺�傚師鐞嗗涓嬶細
//
//浠ュ師鐐�(0, 0)浣滀负鍦嗗績锛屽崐寰勪负1鐢讳竴涓渾銆傝鍦嗙殑澶栧垏姝f柟褰紝杈归暱涓�2銆�
//
//鐜板線璇ユ鏂瑰舰鍐呴殢鏈烘姇鐐癸紝鏁伴噺瓒冲澶氱殑鎯呭喌涓嬶紝钀藉叆鍦嗗唴鐨勭偣涓庤惤鍏ユ暣涓�
//澶栧垏姝f柟褰㈢殑鐐圭殑鏁伴噺姣斿�煎ぇ姒備负锛� 4鈭梤2蟺鈭梤2鈥嬶紝鐒跺悗灏卞彲浠ュ緱鍒跋�鐨勫�笺��
//
//娉ㄦ剰
//
//    璇蜂娇鐢╦dk搴撲腑鐨凴andom瀵硅薄鏉ョ敓鎴愰殢鏈烘暟銆�
//    浣跨敤Math绫讳腑鐨剆qrt涓巔ow鍑芥暟鏉ヨ绠楀紑鏍瑰彿涓庡钩鏂瑰�笺��
//    璁╃偣(x,y)鎶曞湪鏁翠釜鐭╁舰涓紝x涓巠鐨勫彇鍊艰寖鍥翠负(-1鈮<1, -1鈮<1)銆�
//
//杈撳叆鏍煎紡:
//
//闅忔満鏁扮瀛恠eed 鎶曠偣涓暟n
//娉ㄦ剰锛歴eed涓簂ong鍨嬶紝n涓篿nt鍨�
//杈撳嚭鏍煎紡:
//
//璁$畻鍑虹殑鍊煎渾鍛ㄧ巼鐨勫��
//杈撳叆鏍蜂緥:
//
//2 100000
//
//杈撳嚭鏍蜂緥:
//
//3.14684
//
//浠g爜闀垮害闄愬埗
//16 KB
//鏃堕棿闄愬埗
//400 ms
//鍐呭瓨闄愬埗
//64 MB


import java.util.*;
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		long seed=in.nextLong();
		int n=in.nextInt();
		
		long num=0;
		Random random=new Random(seed);
		for(long i=0;i<n;i++){
			double x=random.nextDouble()*2-1;
			double y=random.nextDouble()*2-1;
			if(x*x+y*y<=1)num++;
		}
		System.out.println(4.0*(double)num/n);
	}
}



test7:

ArrayList入门.java

//本习题主要用于练习如何使用ArrayList来替换数组。
//新建1个ArrayList<String> strList用来存放字符串,然后进行如下操作。
//
//提示: 查询Jdk文档中的ArrayList。
//注意: 请使用System.out.println(strList)输出列表元素。
//输入格式
//
//    输入: n个字符串,放入strList。直到输入为!!end!!时,结束输入。
//
//    在strList头部新增一个begin,尾部新增一个end。
//
//    输出列表元素
//
//    输入: 字符串str
//
//    判断strList中有无包含字符串str,如包含输出true,否则输出false。并且输出下标,没包含返回-1。
//
//    在strList中从后往前找。返回其下标,找不到返回-1。
//
//    移除掉第1个(下标为0)元素,并输出。然后输出列表元素。
//
//    输入: 字符串str
//
//    将第2个(下标为1)元素设置为字符串str.
//
//    输出列表元素
//
//    输入: 字符串str
//
//    遍历strList,将字符串中包含str的元素放入另外一个ArrayList strList1,然后输出strList1。
//
//    在strList中使用remove方法,移除第一个和str相等的元素。
//
//    输出strList列表元素。
//
//    使用clear方法,清空strList。然后输出strList的内容,size()与isEmpty(),3者之间用,连接。
//
//输入样例:
//
//a1 b1 3b a2 b2 12b c d !!end!!
//b1
//second
//b
//
//输出样例:
//
//[begin, a1, b1, 3b, a2, b2, 12b, c, d, end]
//true
//2
//2
//begin
//[a1, b1, 3b, a2, b2, 12b, c, d, end]
//[a1, second, 3b, a2, b2, 12b, c, d, end]
//[3b, b2, 12b]
//[a1, second, 3b, a2, b2, 12b, c, d, end]
//[],0,true
//
//代码长度限制
//16 KB
//时间限制
//400 ms



import java.util.*;
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		List<String>strlist=new ArrayList<String>();
		String str_=in.nextLine();
		String str_1[]=str_.split(" ");
		for(int i=0;i<str_1.length-1;i++) {
			strlist.add(i,str_1[i]);
		}
		strlist.add(0,"begin");
		strlist.add("end");
		System.out.println(strlist);
		String str=in.next();
		System.out.println(strlist.contains(str));
		System.out.println(strlist.indexOf(str));
		System.out.println(strlist.lastIndexOf(str));
        System.out.println(strlist.get(0));
		strlist.remove(0);
		System.out.println(strlist);
		String str_2=in.next();
		strlist.set(1, str_2);
		System.out.println(strlist);
		String str_3=in.next();
		ArrayList<String>strlist1=new ArrayList<String>();
		for(int i=0;i<strlist.size();i++) {
			if(strlist.get(i).contains(str_3))strlist1.add(strlist.get(i));
		}
		System.out.println(strlist1);
		strlist.remove(str_3);
		System.out.println(strlist);
		strlist.clear();
		System.out.println(strlist+","+strlist.size()+","+strlist.isEmpty());
		
		in.close();
	}
}

List中指定元素的删除.java

//编写以下两个函数
//
以空格(单个或多个)为分隔符,将line中的元素抽取出来,放入一个List
//public static List<String> convertStringToList(String line) 
在list中移除掉与str内容相同的元素
//public static void remove(List<String> list, String str)
//
//裁判测试程序:
//
//public class Main {
//
//
//    /*covnertStringToList函数代码*/   
//
//        
//
//    /*remove函数代码*/
//
//        
//
//     public static void main(String[] args) {
//
//        Scanner sc = new Scanner(System.in);
//
//        while(sc.hasNextLine()){
//
//            List<String> list = convertStringToList(sc.nextLine());
//
//            System.out.println(list);
//
//            String word = sc.nextLine();
//
//            remove(list,word);
//
//            System.out.println(list);
//
//        }
//
//        sc.close();
//
//    }
//
//
//}
//
//样例说明:底下展示了4组测试数据。
//输入样例
//
//1 2 1 2 1 1 1 2
//1
//11 1 11 1 11
//11
//2 2 2 
//1
//1   2 3 4 1 3 1
//1
//
//输出样例
//
//[1, 2, 1, 2, 1, 1, 1, 2]
//[2, 2, 2]
//[11, 1, 11, 1, 11]
//[1, 1]
//[2, 2, 2]
//[2, 2, 2]
//[1, 2, 3, 4, 1, 3, 1]
//[2, 3, 4, 3]
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


public static List<String> convertStringToList(String line){
		List<String>list=new ArrayList<String>();
		String str[]=line.split("\\s+");
		for(int i=0;i<str.length;i++){
			list.add(str[i]);
			}
		return list;
	}
	public static void remove(List<String> list, String str){
		for(int i=0;i<list.size();) {
			if(list.get(i).equals(str)) {
				list.remove(i);
				
			}else i++;
		}
	}

找到出勤最多的人.java

//根据教师的花名册,找到出勤最多的人。
//输入格式:
//
//出勤记录单行给出,数据直接使用空格分割。
//输出格式:
//
//单行输出(若有多人,人名直接使用空格分割,结尾处没有空格)。
//输入样例:
//
//在这里给出一组输入。例如:
//
//zs ls ww ml zs ls ml zs ww
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//zs
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB



import java.util.*;
public class Main{
    public static void main(String[] args){
    Scanner in=new Scanner(System.in);
    String str=in.nextLine();
    String str_[]=str.split(" ");
    ArrayList<String>strlist=new ArrayList<String>();
    for(int i=0;i<str_.length;i++){
       strlist.add(str_[i]); 
    }
    int max=0;
    int max_index=0;
    int num=0;
    for(int i=0;i<strlist.size();i++) {
    	for(int j=0;j<strlist.size();j++) {
    		if(strlist.get(j).equals(strlist.get(i))) {
    			num++;
    			}
    		}
    	if(max<num) {
    		max=num;
    		max_index=i;
    	}
    	num=0;
    }
    System.out.println(strlist.get(max_index));
    
    
    }
}

test8:

PrintTask类.java

//编写PrintTask类实现Runnable接口。
//功能:输出从0到n-1的整数(n在创建PrintTask对象的时候指定)。并在最后使用System.out.println(Thread.currentThread().getName());输出标识信息。
//裁判测试程序:
//
//import java.util.Scanner;
//
//
///*这里放置你的答案,即PrintTask类的代码*/
//
//
//public class Main {
//
//    public static void main(String[] args) {
//
//        Scanner sc = new Scanner(System.in);
//
//        PrintTask task = new PrintTask(Integer.parseInt(sc.next()));
//
//        Thread t1 = new Thread(task);
//
//        t1.start();
//
//        sc.close();
//
//    }
//
//}
//
//输入样例:
//
//3
//
//输出样例:
//
//0
//1
//2
//标识信息
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


class PrintTask implements Runnable{
	int n;
	public PrintTask() {
		
	}
	public PrintTask(int n) {
		this.n=n;
	}
	@Override
	public void run() {
		for(int i=0;i<n;i++) {
			System.out.println(i);
		}
		System.out.println(Thread.currentThread().getName());
	}
	
}

创建一个倒数计数线程.java

//创建一个倒数计数线程。要求:1.该线程使用实现Runnable接口的写法;2.程序该线程每隔0.5秒打印输出一次倒数数值(数值为上一次数值减1)。
//输入格式:
//
//N(键盘输入一个整数)
//输出格式:
//
//每隔0.5秒打印输出一次剩余数
//输入样例:
//
//6
//
//输出样例:
//
//在这里给出相应的输出。例如:
//
//6
//5
//4
//3
//2
//1
//0
//
//代码长度限制
//16 KB
//时间限制
//9000 ms
//内存限制
//64 MB



import java.util.Scanner;
class PrintTask implements Runnable{
	int n;
	public PrintTask() {
		
	}
	public PrintTask(int n) {
		this.n=n;
	}
	@Override
	public void run() {
		for(int i=n;i>=0;i--) {
			System.out.println(i);
		}try {
			Thread.sleep(500);
		}catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
}


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintTask task = new PrintTask(Integer.parseInt(sc.next()));
        Thread t1 = new Thread(task);
        t1.start();
        sc.close();
    }
}

多线程Thread.java

//编写MyThread类继承自Thread。创建MyThread类对象时可指定循环次数n。
//功能:输出从0到n-1的整数。 并在最后使用System.out.println(Thread.currentThread().getName()+" "+isAlive())打印标识信息
//裁判测试程序:
//
//import java.util.Scanner;
//
//
///*这里放置你的答案,即MyThread类的代码*/
//
//
//public class Main {
//
//    public static void main(String[] args) {
//
//        Scanner sc = new Scanner(System.in);
//
//        Thread t1 = new MyThread(Integer.parseInt(sc.next()));
//
//        t1.start();
//
//    }
//
//}
//
//输入样例:
//
//3
//
//输出样例:
//
//0
//1
//2
//标识信息
//
//代码长度限制
//16 KB
//时间限制
//400 ms
//内存限制
//64 MB


class MyThread extends Thread{ 
	int n;
	public MyThread() {
		
	}
	public MyThread(int n) {
		this.n=n;
	}
	@Override
	public void run() {
		for(int i=0;i<n;i++) {
			System.out.println(i);
		}
		System.out.println(Thread.currentThread().getName()+" "+isAlive());
	}
	
}

  • 9
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值