12.10Java

java异常处理

 public static void main(String[] args) {
    	String str[] = {"hello","world"};
    	int i =0 ;
    	
    	try{
    		while(i<3) {
    		System.out.println(str[i]);
    		i++;
    		}
    	}catch(IndexOutOfBoundsException e) {
    		System.out.println(e);
    	}
    	System.out.println("OK");
    }
 public static void main(String[] args) {
    	int a,b;
    	Scanner cin = new Scanner(System.in);
    	a=cin.nextInt();
    	b=cin.nextInt();
    	try{System.out.println(a/b);
    	}catch(ArithmeticException a1){
    		System.out.println(a1);
            //or
            System.out.println(a1.getMessage());
    		
    	}
    }

复数运算

package sdau;

import java.util.Scanner;

public class Hello {

	public static void main(String[] args) {
		
		Scanner cin = new Scanner(System.in);
		Complex a = new Complex();
		System.out.println("请输入a的实部和虚部:");
		a.setReal(cin.nextDouble());
		a.setIm(cin.nextDouble());
		System.out.println("a:"+"("+a.getReal()+","+a.getIm()+")");
		Complex b = new Complex(1,2);
		Complex c = a.plus(b);
		Complex d = a.minus(b);
		System.out.println("c:"+c.print());
		System.out.println("d:"+d.print());
	
	}
}


class Complex{
	public Complex() {};
	private double real,im;
	public Complex(double real,double im) {
		this.real = real;
		this.im = im;
	}
	public double getReal() {
		return real;
	}
	public void setReal(double real) {
		this.real = real;
	}
	public double getIm() {
		return im;
	}
	public void setIm(double im) {
		this.im = im;
	}
	
	public Complex plus(Complex c) {
		return new Complex(this.real+c.real,this.im+c.im);
	}
	public Complex minus(Complex c) {
		return new Complex(this.real-c.real,this.im-c.im);
	}
	public String print() {
		return "("+this.real+","+this.im+")";
	}
	
}

学生成绩打分 

package sdau;

import java.util.Scanner;


class Student{
	private String ID,name;
	private double math,eng,java,total;
	private int order;
	public Student() {};
	public Student(String ID,String name,double math,double eng,double java) {
		this.ID = ID;
		this.name = name;
		this.math = math;
		this.eng = eng;
		this.java = java;
		this.total = math+eng+java;
	}
	
	public String getID() {
		return ID;
	}
	public void setID(String iD) {
		ID = iD;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMath() {
		return math;
	}
	public void setMath(double math) {
		this.math = math;
	}
	public double getEng() {
		return eng;
	}
	public void setEng(double eng) {
		this.eng = eng;
	}
	public double getJava() {
		return java;
	}
	public void setJava(double java) {
		this.java = java;
	}
	public double getTotal() {
		return total;
	}
	public void setTotal(double total) {
		this.total = total;
	}
	
	public void print(Student stu[]) {
		System.out.println("名次\t学号\t姓名\t数学\t英语\tJAVA\t总分");
		for(int i=0;i<stu.length;i++)
			System.out.println(stu[i].order+"\t"+stu[i].ID+"\t"+stu[i].name+"\t"+
		stu[i].math+"\t"+stu[i].eng+"\t"+stu[i].java+"\t"+stu[i].total);
	}
	
	public void sort(Student stu[]) {
		Student s;
		for (int i = 0; i < stu.length-1; i++) {
			for(int j = i+1; j < stu.length; j++) {
				if(stu[i].getTotal()<stu[j].getTotal()) {
					s = stu[i];
					stu[i] = stu[j];
					stu[j] = s;
				}
			}
		}
		
		for (int i = 0; i < stu.length; i++) {
			stu[i].order = i+1;
		}
		
	}
	
}



public class Hello {

	public static void main(String[] args) {
		Student s[] = new Student[4];
		Student q = new Student();
		s[0] = new Student("001","张三",80,90,86);
		s[1] = new Student("002","赵四",85,49,98);
		s[2] = new Student("003","王五",69,95,92);
		s[3] = new Student("004","老六",87,78,86);
		q.sort(s);
		q.print(s);
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值