1.根据输入1~12的数字,判断是春夏秋冬哪个季节,要求使用方法定义四个季节,运行结果如下。
import java.util.Scanner;
public class lesson_12_homework2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
lesson_12_homework22 s = new lesson_12_homework22();
System.out.print("请输入月份:");
s.m = input.nextInt();
if (s.m > 0 && s.m < 4) {
System.out.println("现在是春季");
} else if (s.m > 3 && s.m < 7) {
System.out.println("现在是夏季");
public class lesson_12_homework22 {
int m;
public void show(){
}
}
2.叙述如何编写类的javaDoc注释。
注释形式统一 注释内容准确简洁 基本注释 特殊必加注释
3.用代码实现第11章作业中简答题第2题中的计算器类(CurrentTime)。
public class lesson_12_homework3 {
int f,l;
public int plus(){
return (f+l);
}
public int reduce(){
return (f-l);
}
public int multiply(){
return (f*l);
}
public int remove(){
return (f/l);
}
4.模拟ATM机进行账户余额查询。
public class lesson_12_homework4 {
double money=0.02;
public double bank(){
return money;
}
}
public class lesson_12_homework42 {
public static void main(String[] args) {
lesson_12_homework4 l =new lesson_12_homework4();
System.out.println("余额为"+l.money);
}
}
5.现有电视商品价格竞猜活动,活动的规则:随机出现一个商品名,用户猜测它的价值,如果在规定次数内猜对,便可获得此商品。模拟竞猜活动,运行结果如下。
import java.util.Scanner;
public class lesson_12_homework5 {
int gs;m'n;
String sp;
public void spinfo() {
int num = (int) (Math.random() * 2);
switch (num) {
case 0:
mn = 2500;
sp = "公主电动车";
break;
case 1:
mn = 15999;
sp = "iPhone";
break;
}
}
public void gsinfo() {
Scanner input = new Scanner(System.in);
System.out.print("请猜测“" + sp + "”的价格:");
gs = input.nextInt();
for (int i = 1; i <= 2; i++) {
if (gs == mn) {
System.out.println("恭喜您猜对了!");
System.exit(0);
break;
} else if (gs > mn) {
System.out.println("再小些!");
} else {
System.out.println("再大些!");
}
if (i <= 2) {
System.out.print("没有猜中 再猜一次吧:");
}
gs = input.nextInt();
}
System.out.println("3次内没有猜对 下次努力吧 ");
}
public static void main(String[] args) {
lesson_12_homework5 l = new lesson_12_homework5();
l.spinfo();
l.gsinfo();
}
}