课后练习1:
package two_c1;
public class Season {
public void spring() {
System.out.println();
System.out.println("该季节为春季");
}
public void summer() {
System.out.println();
System.out.println("该季节为夏季");
}
public void autumn() {
System.out.println();
System.out.println("该季节为秋季");
}
public void winter() {
System.out.println();
System.out.println("该季节为冬季");
}
}
package two_c1;
import java.util.Scanner;
public class Show {
public static void main(String[] args) {
System.out.print("请输入月份:");
Scanner input = new Scanner(System.in);//输入月份
int month = input.nextInt();
Season show = new Season();
switch(month) {
case 1:
case 2:
case 3:
show.spring();
break;//1~3月为春天
case 4:
case 5:
case 6:
show.summer();
break;//4~6月为夏天
case 7:
case 8:
case 9:
show.autumn();
break;//7~9月为秋天
case 10:
case 11:
case 12:
show.winter();
break;//10~12月为冬天
}
}
}
课后练习3:
package two_c3;
public class Calculator {
public void add(int a,int b) {
System.out.println(a + "+" + b + "=" + (a+b));//加法
}
public void minus(int a,int b) {
System.out.println(a + "-" + b + "=" + (a-b));//减法
}
public void multiple(int a,int b) {
System.out.println(a + "*" + b + "=" + (a*b));//乘法
}
public void divide(int a,int b) {
System.out.println(a + "/" + b + "=" + ((double)a/b));//除法
}
}
package two_c3;
import java.util.Scanner;
public class Show {
public static void main(String[] args) {
System.out.print("请输入第一个数:");
Scanner input = new Scanner(System.in);
int a = input.nextInt();//输入第一个整数a
System.out.print("请输入运算符:");
char operator = input.next().charAt(0);//输入运算符
System.out.print("请输入第二个数:");
int b = input.nextInt();//输入第二个整数b
Calculator show = new Calculator();
switch(operator) {
case '+':
show.add(a,b);//调用加法方法
break;
case '-':
show.minus(a,b);//调用减法方法
break;
case '*':
show.multiple(a,b);//调用乘法方法
break;
case '/':
show.divide(a,b);//调用除法方法
break;
}
}
}
课后练习4:
package two_c4;
import java.util.Scanner;
public class QuessMachine {
String name;
int money;
int guessMoney;
public void goods(int random) {
switch(random) {
case 0:
name = "洗衣机";
money = 1000;
break;
case 1:
name = "电视机";
money = 3000;
break;
case 2:
name = "自行车";//定义三个奖品信息
money = 400;
break;
}
}
public void guess() {
System.out.print("请猜测“"+name+"”的价格:");
Scanner input = new Scanner(System.in);
guessMoney = input.nextInt();
for (int i = 0; i < 3; i++) {
if (guessMoney == money) {
System.out.println("恭喜你,获得"+name);//如果猜对,获得奖品
break;
} else if(guessMoney > money) {
System.out.println("再小点!");//如果猜大了,提示再小点
} else {
System.out.println("再大点!");//如果猜小了,提示再大点
}
System.out.print("再猜一次吧:");
guessMoney = input.nextInt();
if(i == 2 && guessMoney == money) {
System.out.println("恭喜你,获得"+name);//最后一次猜对,获得奖品
break;
}
}
if (guessMoney != money) {
System.out.println("4次内没有猜对,下次努力吧!");//全部猜错,提示下次努力
}
}
}
package two_c4;
public class Show {
public static void main(String[] args) {
QuessMachine show = new QuessMachine();
int random = (int)(Math.random()*3);//产生0、1、2三个随机数,对应三个商品
show.goods(random);
show.guess();
}
}
课后练习5:
package two_c5;
public class Calculator {
public double ope(int op,int num1,int num2) {
double result = 0;
switch(op) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = (double)num1 / num2;//根据输入的数字,进行加减乘除四种运算
break;
}
return result;
}
}
package two_c5;
import java.util.Scanner;
public class Show {
public static void main(String[] args) {
System.out.print("请选择运算:1.加法 2.减法 3.乘法 4.除法:");
Scanner input = new Scanner(System.in);//手动选择运算
int op = input.nextInt();
System.out.print("请输入第一个数:");
int num1 = input.nextInt();
System.out.print("请输入第二个数:");
int num2 = input.nextInt();
Calculator show = new Calculator();
System.out.println("***运算结果为:"+show.ope(op, num1, num2));//输出结果
}
}
课后练习6:
package two_c6;
public class InsertArray {
public void insertArray(int[]arr,int index,int value) {
if(index == arr.length-1) {
arr[index] = value;//如果选择插入的位置为最后一位,特殊处理
}else {
for (int i = arr.length-1; i > index; i--) {
arr[i] = arr[i-1];//选择插入位置之后的位置元素后移
}
arr[index] = value;//把输入的元素插入到指定位置
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if(i < arr.length-1) {
System.out.print(",");//输出插入后的数组
}
}
}
}
package two_c6;
import java.util.Scanner;
public class Show {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
InsertArray show = new InsertArray();
int[] arr = new int[6];
arr[0] = 9;
arr[1] = 5;
arr[2] = 5;
arr[3] = 2;
arr[4] = 1;
System.out.print("请输入需要插入的整数:");//输入插入的数
int value = input.nextInt();
System.out.print("请输入需要插入的下标:");//输入插入的下标
int index = input.nextInt();
show.insertArray(arr, index, value);
}
}
课后练习7:
package two_c7;
public class Student {
int java;
int c;
int sql;//定义三门科目属性
public double getAvg(Student stu) {
double avg = (double)(java+c+sql)/3;//计算平均成绩
return avg;
}
}
package two_c7;
import java.util.Scanner;
public class StudentBiz {
public static void main(String[] args) {
Student stu = new Student();
Scanner input = new Scanner(System.in);
System.out.print("请输入Java成绩:");
stu.java = input.nextInt();
System.out.print("请输入C#成绩:");
stu.c = input.nextInt();
System.out.print("请输入SQL成绩:");//输入成绩
stu.sql = input.nextInt();
System.out.println("三门科目的平均分是:"+stu.getAvg(stu));//调用并显示方法
}
}