第十二章
1.根据输入1~12的数字,判断是春夏秋冬哪个季节,要求使用方法定义四个季节
代码如下:
package com.hylcbdql.www;
import java.util.Scanner;
public class shujijie {
public static void main(String[] args) {
jijie au =new jijie();
Scanner input = new Scanner(System.in);
System.out.println(“请输入现在的月份:”);
int yue = input.nextInt();
switch(yue){
case 1:
case 2:
case 3:
System.out.println(au.spring());
break;
case 4:
case 5:
case 6:
System.out.println(au.summer());
break;
case 7:
case 8:
case 9:
System.out.println(au.autumn());
break;
case 10:
case 11:
case 12:
System.out.println(au.winter());
break;
default:
System.out.println(“请重新输入:”);
break;
}
}
}
2.叙述如何编写类的JavaDoc注释:
javadoc注释以“/*”开头,以“/”结尾。每个注释包含一些描述性的文本及若干个javadoc标签,javadoc标签一般以“@”为前缀。
3.用代码实现第十一章作业中简答题第2题的计算器类(Calculator),编写Calculator类,定义成员变量为运算数1(num1)和运算数2(num2),实施成员方法加(add),减(minus),乘(multiple),除(divide)
代码如下:
package com.hylcbdql.www;
public class jisuanqi {
int numa;
int numb;
int sun;
public int jia(){
return(numa+numb);
}
public int jian(){
return(numa-numb);
}
public int cheng(){
return(numa*numb);
}
public int chu(){
return(numa/numb);
}
}
package com.hylcbdql.www;
import java.util.Scanner;
public class shujijie {
public static void main(String[] args) {
jisuanqi hu =new jisuanqi();
Scanner input = new Scanner(System.in);
System.out.println(“请输入第一个数”);
hu.numa =input.nextInt();
System.out.println(“请输入第二个数”);
hu.numb =input.nextInt();
System.out.println(“请选择运算方法1.加,2.减.3.乘.4.除”);
int sun =input.nextInt();
switch(sun){
case 1:
hu.jia();
break;
case 2:
hu.jian();
break;
case 3:
hu.cheng();
break;
case 4:
hu.chu();
break;
default:
System.out.println(“输入错误!”);
}
if(sun==1){
System.out.println(“结果为:”+hu.jia());
}else if(sun==2){
System.out.println(“结果为:”+hu.jian());
}else if(sun==3){
System.out.println(“结果为:”+hu.cheng());
}else if(sun>3||sun<=4){
System.out.println(“结果为:”+hu.chu());
}else{
System.out.println(“”);
}
}
}
4.模拟ATM机进行账户余额查询,编写账户类,属性为账户余额,方法为查询余额,编写测试类,显示账户余额
代码如下:
package com.hylcbdql.www;
public class atm {
int balance = 1000;
int account =25526;
int password =123456;
public int getbalance(){
return balance;
}
}
package com.hylcbdql.www;
import java.io.InputStream;
import java.util.Scanner;
import javax.print.DocFlavor.INPUT_STREAM;
public class shujijie {
public static void main(String[] args) {
atm cu =new atm();
Scanner input = new Scanner(System.in);
System.out.println(“请输入您的用户名:”);
int a =input.nextInt();
System.out.println(“请输入您的密码:”);
int b =input.nextInt();
if(a==cu.account||b==cu.password){
System.out.println(“欢迎您!”);
System.out.println(“”);
System.out.println(“您的账户余额为:”+cu.balance);
}else{
System.out.println(“您的用户名或者密码不匹配,请重新输入”);
}
}
}
5.现有电视商品价格竞猜活动,活动的规则,随机出现一个商品名,用户猜测他的价值,如果再规定次数内猜对,便可获得此商品,模拟竞猜活动
代码如下:
package com.hylcbdql.www;
import java.util.*;
public class GuessMachine {
String name ;
double price;
public void initial(){
int random = (int)(Math.random()*10);
switch(random){
case 0:
case 1:
case 2:
case 3:
name = “亚索”;
price = 6300;
break;
case 4:
case 5:
case 6:
name = “虚空恐惧”;
price = 4800;
break;
case 7:
case 8:
case 9:
name = “无极剑圣”;
price = 450;
break;
default:
break;
}
}
public void guess(){
System.out.print(“\n请猜测\”“+ name + “\”的价格: ” );
Scanner input = new Scanner(System.in);
double guessPrice = input.nextDouble();
boolean isCorrect = false;
for(int i = 0; i<3; i++){
if(guessPrice == price){
System.out.println(“猜对了!”);
isCorrect = true;
break;
}else if(guessPrice > price){
System.out.println(“再小点!”);
}else{
System.out.println(“再大点!”);
}
System.out.print(“\n再猜一次吧: “);
guessPrice = input.nextDouble();
}
if(!isCorrect){
System.out.println(“\n4次内没有猜对,下次努力吧!”);
}
}
}
package com.hylcbdql.www;
import java.util.Scanner;
public class shujijie {
public static void main(String[] args) {
// TODO Auto-generated method stub
GuessMachine js = new GuessMachine();
js.initial();
js.guess();
}
}
上一篇: 初识LINQ