课后练习2:
package one_c2;
public class Calculator {
int a;//输入第一个数
int b;//输入第二个数
char operator;//输入运算符
public void operation() {
switch(operator) {
case '+':
System.out.println(a + "+" + b + "=" + (a+b));//加法运算
break;
case '-':
System.out.println(a + "-" + b + "=" + (a-b));//减法运算
break;
case '*':
System.out.println(a + "*" + b + "=" + (a*b));//乘法运算
break;
case '/':
System.out.println(a + "/" + b + "=" + (a/b));//除法运算
break;
}
}
}
package one_c2;
import java.util.Scanner;
public class Show {
public static void main(String[] args) {
Calculator show = new Calculator();
System.out.print("请输入第一个整数:");
Scanner input = new Scanner(System.in);
show.a = input.nextInt();
System.out.print("请输入算数运算符:");
show.operator = input.next().charAt(0);
System.out.print("请输入第二个整数:");
show.b = input.nextInt();
show.operation();//调用运算
}
}
课后练习3、4:
package one_c3;
public class CurrentTime {
String str = "2015年5月12日10点11分00秒";//设置初始时间
public void time() {
System.out.println(str);
}
}
package one_c3;
public class Show {
public static void main(String[] args) {
CurrentTime show = new CurrentTime();
show.time();//显示初始时间
}
}
package one_c3;
public class Show4 {
public static void main(String[] args) {
CurrentTime show4 = new CurrentTime();
show4.str = "2015年5月12日10点11分30秒";//更改设置的时间
show4.time();
}
}
课后练习5:
package one_c5;
public class Computer {
public void showInfo() {
System.out.println("CPU");
System.out.println("主板");
System.out.println("显示器");
System.out.println("硬盘");
System.out.println("内存");//设置电脑信息
}
}
package one_c5;
public class Show {
public static void main(String[] args) {
Computer show = new Computer();
show.showInfo();//显示电脑信息
}
}
课后练习6:
package one_c6;
public class Game {
String name;//输入名字
String info;//输入基本信息
public void infomation() {
System.out.println("我是" + name + ",我的基本信息如下:");
System.out.println(info);
}
}
package one_c6;
public class Show {
public static void main(String[] args) {
Game hero = new Game();
Game monster = new Game();
Game weapon = new Game();
hero.name = "英雄";
monster.name = "怪物";
weapon.name = "武器";
hero.info = "姓名:李小侠,生命值:300";
monster.info = "武器名:死神镰刀,攻击力:12";
weapon.info = "姓名:小龟,生命值:300,类型:潜水类";
hero.infomation();//显示怪物信息
monster.infomation();//显示怪物信息
weapon.infomation();//显示武器信息
}
}