面向对象第二章课后习题

1.根据输入的1到12的数字,判断是春夏秋冬哪个季节,要求使用方法定义四个季节
package exercises5;

import java.util.Scanner;

/**
 * 季节类
 * @author 段海锋
 *
 */
public class Season {
	//春季方法
	public void springtime() {
		System.out.println("该季节为春季");
	}
	//夏季方法
	public void summertime() {
		System.out.println("该季节为夏季");
	}
	//秋季方法
	public void autumn() {
		System.out.println("该季节为秋季");
	}
	//冬季方法
	public void wintertime() {
		System.out.println("该季节为冬季");
	}
	public static void main(String[] args) {
		Season sea = new Season();
		System.out.print("请输入月份:");
		Scanner input = new Scanner(System.in);
		int month=input.nextInt();
		if(month>=1 && month<=3) {
			System.out.println();
			sea.springtime();
		}else if (month>=4 && month<=6) {
			System.out.println();
			sea.summertime();
		}else if (month>=7 && month<=9) {
			System.out.println();
			sea.autumn();
		}else if (month>=10 && month<=12) {
			System.out.println();
			sea.wintertime();
		}
	}
}
2.现有电视商品竞猜活动,活动的规则:随机出现一个商品名,用户猜测它的价格,如果在规定的次数内猜对,便可获得此商品。
package exercises7;

import java.util.Scanner;

public class QuessMachine {
	String name;
	double conjecture;
	public void initial() {
		int random=(int)(Math.random()*10);
		   switch (random) {  
	        case 0:  
	        case 1:  
	        case 2:  
	        case 3:  
	            name = "公主电动车";  
	            conjecture = 3000;  
	            break;  
	        case 4:  
	        case 5:  
	        case 6:  
	            name = "海尔双开门冰箱";  
	            conjecture = 12000;  
	            break;  
	        case 7:  
	        case 8:  
	        case 9:  
	            name = "笔记本电脑";  
	            conjecture = 5388;  
	            break;  
	        default:  
	            break;  
	        }  
	    }  
	  
	    public void guess() {  
	        System.out.println("\n请猜测'" +name +"'的价格:");  
	        Scanner input = new Scanner(System.in);  
	        double a = input.nextDouble();  
	        boolean con = false;  
	        for (int i = 1; i < 4; i++) {  
	            if (a == conjecture) {  
	                System.out.println("恭喜您,猜对了!");  
	                con = true;  
	                break;  
	            } else if (a > conjecture) {  
	                System.out.println("再小点!");  
	            } else {  
	                System.out.println("再大点!");  
	            }  
	            System.out.println("再猜一次吧:");  
	            a = input.nextDouble();  
	        }  
	        if (!con) {  
	            System.out.println("4次内没有猜对,下次努力吧!");  
	        }  
	    }
	    public static void main(String[] args) {
	    	QuessMachine quess = new QuessMachine();
	    	quess.initial();
	    	quess.guess();
		}
	}

3.用带参的方法编写计算器实现:加,减,乘,除
package exercises6;

import java.util.Scanner;

/**
 * 计算器类
 * @author 段海锋
 *
 */
public class Calculator {
	int num1;//输入数字1
	int num2;//输入数字2
	double result;//结果
	//加法方法
	public double add(int num1,int num2) {
		result=num1+num2;
		return result;
	}
	//减法方法
	public double minus(int num1,int num2) {
		result=num1-num2;
		return result;
	}
	//乘法方法
	public double multiple(int num1,int num2) {
		result=num1*num2;
		return result;
	}
	public double divide(int num1,int num2) {
		result=num1/num2;
		return result;
	}
	public void operation() {
		Scanner input = new Scanner(System.in);
		System.out.println("请选择运算方法:1.(加法)2.(减法)3.(乘法)4.(除法)");
		int select=input.nextInt();
		switch(select) {
		case 1:
			add(num1, num2);
			break;
		case 2:
			minus(num1, num2);
			break;
		case 3:
			multiple(num1, num2);
			break;
		case 4:
			divide(num1, num2);
		}
	}
	public static void main(String[] args) {
		Calculator calc = new Calculator();
		Scanner input = new Scanner(System.in);
		System.out.println("请输入第一个数:");
		calc.num1=input.nextInt();
		System.out.println("请输入第二个数:");
		 calc.num2=input.nextInt();
		calc.operation();
		System.out.println("运算结果为:"+calc.result);
	}
}
4.一学员期末考试有三门课程的考试,即:JAVA,C#,SQL,编写方法计算每位学员的平均分
package exercises7;

import java.util.Scanner;

public class Student {
	double avgscore;
	double totalscore;
	int javascore;
	int cscore;
	int sqlscore;
	public double getavg() {
		totalscore=javascore+cscore+sqlscore;
		avgscore=totalscore/3.0;
		return avgscore;
	}
	public static void main(String[] args) {
		Student stu = new Student();
		Scanner input = new Scanner(System.in);
		double [] scoers = new double[1];
		for (int i = 0; i < scoers.length; i++) {
			System.out.println("请输入第"+(i+1)+"位同学的JAVA成绩:");
			stu.javascore=input.nextInt();
			System.out.println("请输入第"+(i+1)+"位同学的c#成绩:");
			stu.cscore=input.nextInt();
			System.out.println("请输入第"+(i+1)+"位同学的sql成绩:");
			stu.sqlscore=input.nextInt();
		}
		System.out.println("平均成绩为:"+stu.getavg());
	}
}

阅读更多
换一批

没有更多推荐了,返回首页