Java作业 第十二章
程序员文章站
2022-07-04 18:48:44
...
课后作业
1.根据输入1~12的数字,判断是春夏秋冬哪个季节,要求使用方法定义四个季节
代码如下:
import java.util.*;
public class Clas { //季节类
public void spring (){ //春季
System.out.println("该季节为春季");
}
public void summer(){ //夏季
System.out.println("该季节为夏季");
}
public void autumn(){ //秋季
System.out.println("该季节为秋季");
}
public void winter(){ //冬季
System.out.println("该季节为冬季");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Clas ae = new Clas(); //调用季节类
System.out.print("请输入月份:");
Scanner input = new Scanner (System.in);
int mom = input.nextInt();
switch (mom){
case 3: case 4: case 5: //switch的穿透性
System.out.println();
ae.spring();
break;
case 6: case 7: case 8:
System.out.println();
ae.summer();
break;
case 9: case 10: case 11:
System.out.println();
ae.autumn();
break;
case 12: case 1: case 2:
System.out.println();
ae.winter();
break;
default:
System.out.println("请输入正确的月份!");
break;
}
}
}
2.叙述如何编写类的JavaDoc注释
/**
*这是一个示例
*@author lyar
*/
public class LYAR(){
}
第一行用于描述注释的对象的主要功能,第二行@author是JavaDoc标签
3.用代码实现第十一章作业中简答题第2题的计算器类(Calculator),编写Calculator类,定义成员变量为运算数1(num1)和运算数2(num2),实施成员方法加(add),减(minus),乘(multiple),除(divide)
代码如下:
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.模拟ATM机进行账户余额查询,编写账户类,属性为账户余额,方法为查询余额,编写测试类,显示账户余额
代码如下:
public class Account {
//账户类
double Money; //账户余额
public void show(){
System.out.println("账户余额为:"+Money);
}
}
import java.util.*;
public class cesi {
//测试类
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
Account n = new Account();
int money = 100000;
System.out.println("账户总额:"+money);
System.out.print("取款:");
double a = input.nextDouble();
n.Money = money-a;
n.show();
}
}
5.现有电视商品价格竞猜活动,活动的规则,随机出现一个商品名,用户猜测他的价值,如果再规定次数内猜对,便可获得此商品,模拟竞猜活动
代码如下:
import java.util.*;
public class QuessMachine {
String name; // 商品名称
double money; // 商品价格
public void initial() { // QuessMachine类中initial方法定义商品信息
int random = (int) (Math.random() * 10); // 产生随机数,来选定一款商品
switch (random) {
case 0:
case 1:
case 2:
case 3:
name = "公主电动车";
money = 3000;
break;
case 4:
case 5:
case 6:
name = "雷神笔记本";
money = 7199;
break;
case 7:
case 8:
case 9:
name = "美的*空调";
money = 7999;
break;
default:
break;
}
}
public void guess() {
System.out.print("\n请猜测“" + name + "”的价格:");
Scanner input = new Scanner(System.in);
double jiage = input.nextDouble();
boolean can = false;
for (int i = 0; i < 4; i++) { // 循环4次
if (jiage == money) {
System.out.println("恭喜您,猜对了!获得" + name + "一台!");
can = true;
break;
} else if (jiage > money) {
System.out.println("再小点!");
} else {
System.out.println("再大点!");
}
System.out.print("再猜一次吧:");
jiage = input.nextInt();
}
if (!can) {
System.out.println("4次均没有猜对,下次再努力吧!");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
QuessMachine js = new QuessMachine();
js.initial();
js.guess();
}
}