第一章-初识java

本章学习内容:java基础

答案略

第二章-变量,数据类型和运算符

本章学习内容:变量,数据类型和运算符

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.mjc;

public class Lx1 {
public static void main(String[] args) {
//第二章上机练习1(第47页)
int tShort = 245;
int shoe = 570;
int pad = 320;
int tShorts = 2;
int shoes = 1;
int pads = 1;
double zheKou = 0.8;
double sum = (tShort * tShorts + shoe * shoes + pad * pads) * zheKou;//折扣
System.out.println("消费总金额为:" + sum);
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.mjc;

public class Lx2 {

public static void main(String[] args) {
//第二章上机练习2(第47页)
int tShort = 245;
int shoe = 570;
int pad = 320;
int tShorts = 2;
int shoes = 1;
int pads = 1;
double ZheKou = 0.8;
double sum = (tShort * tShorts + shoe * shoes + pad * pads) * ZheKou;//折扣
int sore = (int)sum * 3 / 100;
System.out.println("*************消费单*************");
System.out.println("购买物品\t\t单价\t个数\t\t金额");
System.out.println("T恤\t\t¥245\t2\t\t" + "¥" + tShort*2);
System.out.println("网球鞋\t\t¥70\t1\t\t" + "¥" + shoe);
System.out.println("网球拍\t\t¥20\t1\t\t" + "¥"+ pad + "\n");
System.out.println("折扣:\t\t" + ZheKou);
System.out.println("消费总金额:\t" + "¥" + sum);
System.out.println("实际交费:\t" + "¥1500");
System.out.println("拨钱:\t\t" + "¥396.0");
System.out.println("本次购物获得的积分是:" + sore);

}

}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.mjc;

import java.util.Scanner;

public class Lx3 {

public static void main(String[] args) {
//第二章上机练习3(第49页)
int cusNo;
System.out.println("请输入会员卡号:");
Scanner input = new Scanner(System.in);
cusNo = input.nextInt();

int gewei = cusNo % 10;
int shiwei = cusNo / 10 % 10;
int baiwei = cusNo / 100 % 10;
int qianwei = cusNo / 1000;

int sum = gewei + shiwei + baiwei + qianwei;
System.out.println("会员卡号" + cusNo + "各位之和" + sum);
boolean isLuck = sum > 20;
System.out.println("是幸运用户吗?" + isLuck);

}

}

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.mjc;

import java.util.Scanner;

public class Lx4 {

public static void main(String[] args) {
int tShort = 245;
int shoe = 570;
int pad = 320;
Scanner input = new Scanner(System.in);
System.out.println("请输入折扣:");
double dis = input.nextDouble();
double shirt = dis * tShort;
double shoeS = dis * shoe;
double padP = dis * pad;
boolean shirtB = shirt < 100;
boolean shoeB = shoeS < 100;
boolean podB = padP < 100;
System.out.println("T恤折扣价低于100吗?" + shirtB);
System.out.println("网球鞋折扣价低于100吗?" + shoeB);
System.out.println("网球拍折扣价低于100吗?" + podB);

}

}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.mjc;

public class Jd3 {

public static void main(String[] args) {
//第二章简答题3(第52页)
int a = 10;
int b = 8;
System.out.println("交换前的手牌");
System.out.println("右手中的手牌:" + a);
System.out.println("左手中的手牌:" + b + "\n");
int t = a;
a = b;
b = t;
System.out.println("交换后的手牌");
System.out.println("右手中的手牌:" + a);
System.out.println("左手中的手牌:" + b);

}

}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.mjc;

import java.util.Scanner;

public class Jd4 {

public static void main(String[] args) {
//第二章简答题4(第52页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入华氏度我来转换摄氏度:");
double f = sc.nextDouble();//华氏度
double c = 5 / 9.0 * (f - 32);//华氏度转为摄氏度公式
System.out.printf("摄氏度为:%.2f℃\n华氏度为:%.2f℃",f,c);
}

}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.mjc;

import java.util.Scanner;

public class Jd5 {

public static void main(String[] args) {
//第二章简答题5(第52页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入本金:");
double co = sc.nextDouble();
System.out.println("本金为:"+co);
System.out.print("\n");
//计算第一年的本息并打印
double sum1 = co+(co*1*0.0225);//本息计算公式
System.out.println("存款一年后的本息是:"+sum1);
System.out.print("\n");
double sum2 = co+(co*2*0.027);
System.out.println("存款两年后的本息是:"+sum2);
System.out.print("\n");
double sum3 = co+(co*3*0.0324);
System.out.println("存款三年后的本息是:"+sum3);
System.out.print("\n");
double sum5 = co+(co*5*0.036);
System.out.println("存款五年后的本息是:"+sum5);

}

}

第三章-选择结构(一)

本章学习内容:if,if-else,多重if结构

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.mjc;

import java.util.Scanner;

public class Lx1 {
public static void main(String[] args) {
//第三章上机练习1(第63页)
//产生随机数,求a-b公式:(int)(Math.random() * 1000) % (b - a + 1) + a
int luck = (int)(Math.random() * 10);
System.out.println("我行我素购物管理系统 > 幸运抽奖\n");
System.out.print("请输入四位会员卡号:");
Scanner input = new Scanner(System.in);
int cueNo = input.nextInt();
int baiwei = cueNo / 100 % 10;
if (luck == baiwei) {
System.out.println(cueNo + "是幸运用户,获得精美Mp3一个!");
}else {
System.out.println(cueNo + "谢谢您的支持!");
}
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.mjc;

import java.util.Scanner;

public class Lx2 {
public static void main(String[] args) {
//第三章上机练习2(第64页)
System.out.println("我行我素购物管理系统 > 客户信息管理 > 添加客户信息\n");
Scanner sc = new Scanner(System.in);
System.out.print("请输入会员号(四位整数):");
int custNO = sc.nextInt();
System.out.print("请输入会员生日:(月/日(用两位数表示)):");
String custBirth = sc.next();
System.out.print("请输入会员积分:");
int custSore = sc.nextInt();
System.out.println();
if (custNO > 999 && custNO < 10000) {
System.out.println("已录入的会员信息是");
System.out.print(custNO + "\t" + custBirth + "\t" + custSore);
}else {
System.out.println("抱歉,信息录入失败!");
}
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.mjc;

import java.util.Scanner;

public class Lx3 {
public static void main(String[] args) {
//第三章上机练习3(第70页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入是否会员:是(y)/否(n)");
String identity = sc.next();
System.out.println("请输入购物金额:");
double money = sc.nextDouble();
if (identity.equals("y")) {
if(money>200) {
money *= 0.75;
}else {
money *= 0.8;
}
}else {
if(money>200) {
money *= 0.9;
}
}
System.out.println("实际支付:" + money);
}
}

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.mjc;

import java.util.Scanner;

public class Lx4 {
public static void main(String[] args) {
//第三章上机练习4(第71页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入购物金额:");
double money = sc.nextDouble();
String nu = "该会员享受的折扣是:";
if (money>=8000) {
money = 0.6;
}else if (money>=4000) {
money = 0.7;
}else if (money>2000) {
money = 0.8;
}else {
money = 0.9;
}
System.out.println(nu + money);
}
}

简答题1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.mjc;

import java.util.Scanner;

public class Jd1 {
public static void main(String[] args) {
//第三章简答题1(第73页)
Scanner sc = new Scanner(System.in);
System.out.print("请输入用户名:");
String str = sc.next();
char user = str.charAt(0);
//等价写法:
//char user = sc.next().charAt(0);

System.out.print("请输入密码:");
int password = sc.nextInt();

if(password==123&&user=='青'){
System.out.print("欢迎你,青");
}else{
System.out.print("对不起,你不是青");
}
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.mjc;

import java.util.Scanner;

public class Jd2 {
public static void main(String[] args) {
//第三章简答题2(第73页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入值:");
int a = sc.nextInt();

if(a>=7){
System.out.println("yes");
}else if(a>=5){
System.out.print("请输入性别:");
String str = sc.next();
char sex = str.charAt(0);
if(sex=='男'){
System.out.println("yes");
}else{
System.out.println("no");
}
}else{
System.out.println("no");
}
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.mjc;

import java.util.Scanner;

public class Jd3 {
public static void main(String[] args) {
//第三章简答题3(第74页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入三个数:");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int t = 0;
if(a > b){
t = a;
a = b;
b = t;
}
if(a > c){
t = c;
c = a;
a = t;
}
if(b > c){
t = b;
b = c;
c = t;
}
System.out.println("从小到大结果为:" + a + " " + b + " " + c);
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.mjc;

import java.util.Scanner;

public class Jd4 {
public static void main(String[] args) {
//第三章简答题4(第74页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入值:");
int a = sc.nextInt();

if(a%3==0 || a%5==0){
System.out.println("该数是3或5的倍数");
}else{
System.out.println("你输入的值不能被3或5整除");
}
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.mjc;

import java.util.Scanner;

public class Jd5 {
public static void main(String[] args) {
//第三章简答题5(第74页)
//输入分数:
Scanner sc = new Scanner(System.in);
System.out.print("请输入分数:");

//先判断键盘录入的数据是不是int类型的
if(sc.hasNextInt()==true){//是int类型数据:
//将这个int类型的数据接收:
int score = sc.nextInt();
//判断这个分数是否是正数:
if(score>=0){
//根据分数判断奖品:
if(score>=100){
System.out.println("奖励一辆车!");
}else if(score>=90){
System.out.println("奖励一部笔记本电脑!");
}else if(score>=60){
System.out.println("奖励一部手机!");
}else{
System.out.println("没有礼物!");
}
}else{
System.out.println("你输入的数据为负数,不符合运算!");
}
}else{//不是int类型的数据
System.out.println("你录入的分数不是整数!");
}
}
}

简答题6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.mjc;

import java.util.Scanner;

public class Jd6 {
public static void main(String[] args) {
//第三章简答题6(第74页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入您出行的月份:1~12");
int m=sc.nextInt(); //录入月份;
int yuan=5000; //机票原价;
double sum; //打完折的金额;
if (m>=1&&m<=12) {
System.out.println("请问您选择头等舱还是经济舱?头等舱输入1,经济舱输入2。");
int n=sc.nextInt(); //录入头等舱还是经济舱;
if (m>=4&&m<=10) {
if(n==1) {
sum=yuan*0.9;
System.out.print("您的机票价格为:"+sum);
}
else if(n==2) {
sum=yuan*0.8;
System.out.print("您的机票价格为:"+sum);
}
else {
System.out.println("输入的舱位有误!!!");
}
}else if(m>=11||m<=3) {
if(n==1) {
sum=yuan*0.5;
System.out.print("您的机票价格为:"+sum);
}
else if(n==2) {
sum=yuan*0.4;
System.out.print("您的机票价格为:"+sum);
}
else {
System.out.println("输入的舱位有误!!!");
}
}
}else {
System.out.println("请输入有效的月份!!!");
}
}
}

第四章-选择结构(二)

本章学习内容:switch结构

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.mjc;

import java.util.Scanner;

public class Lx1 {
public static void main(String[] args) {
//第四章上机练习1(第82页)
Scanner sc = new Scanner(System.in);
System.out.println("欢迎使用我行我素购物管理系统");
System.out.println("\t1.登录系统");
System.out.println("\t2.退出");
System.out.println("**************************");
System.out.println("请选择:");
int num = sc.nextInt();
switch(num) {
case 1:
System.out.println("**************************");
System.out.println("\t1.客户管理信息");
System.out.println("\t2.购物结算");
System.out.println("\t3.真情回顾");
System.out.println("\t4.注销");
System.out.println("**************************");
break;
case 2:
System.out.println("谢谢您的使用!");
break;
default:System.out.println("输入错误!");
}
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.mjc;

import java.util.Scanner;

public class Lx2 {
public static void main(String[] args) {
//第四章上机练习2(第87页)
Scanner sc = new Scanner(System.in);
System.out.print("请输入消费金额:");
int money = sc.nextInt();
if (money >= 50) {
System.out.println("是否参加此优惠活动:");
System.out.println("1:满50元,加2元换购百事可乐饮料1瓶");
System.out.println("2:满100元,加3元换购500ml可乐1瓶");
System.out.println("3:满100元,加10元换购5公斤面粉");
System.out.println("4:满200元,加10元换购1个苏泊尔炒菜锅");
System.out.println("5:满200元,加20元换购欧莱雅爽肤水一瓶");
System.out.println("0:不换购");
System.out.print("请选择:");
int num = sc.nextInt();
switch (num) {
case 1:
money += 2;
System.out.println("本次消费金额为:" + money);
System.out.println("换购成功:百事可乐饮料1瓶");
break;
case 2:
if(money >= 100) {
money += 3;
System.out.println("本次消费金额为:" + money);
System.out.println("换购成功:500ml可乐1瓶");
}else {
System.out.println("不满足换购条件!");
}
break;
case 3:
if (money >= 100) {
money += 10;
System.out.println("本次消费金额为:" + money);
System.out.println("换购成功:5公斤面粉");
}else {
System.out.println("不满足换购条件!");
}
break;
case 4:
if(money >= 200) {
money += 10;
System.out.println("本次消费金额为:" + money);
System.out.println("换购成功:1个苏泊尔炒菜锅");
}else {
System.out.println("不满足换购条件!");
}
break;
case 5:
if(money >= 200) {
money += 20;
System.out.println("本次消费金额为:" + money);
System.out.println("换购成功:欧莱雅爽肤水一瓶");
}else {
System.out.println("不满足换购条件!");
}
break;
case 0:System.exit(0);
default:System.out.println("参数错误!");
break;
}
}else {
System.out.println("抱歉,不满足换购优惠金额!!!");
}
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.mjc;

import java.util.Scanner;

public class Jd2 {
public static void main(String[] args) {
//第四章简答题2(第89页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入星期几:");
int a = sc.nextInt();

switch(a){
case 7 : System.out.println("休息");break;
case 6 :
case 4 :
case 2 : System.out.println("英语");break;
case 5 :
case 3 :
case 1 : System.out.println("学习编程");break;
default:System.out.println("错误的值!");
}
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.mjc;

import java.util.Scanner;

public class Jd3 {
public static void main(String[] args) {
//第四章简答题3(第90页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入成绩:");
int a = sc.nextInt();
switch(a/10){
case 10 : System.out.println("奖励一辆车");break;
case 9 : System.out.println("奖励一台笔记本电脑");break;
case 8 :
case 7 :
case 6 : System.out.println("奖励一部手机");break;
case 5 :
case 4 :
case 3 :
case 2 :
case 1 : System.out.println("没有礼物");break;
default : System.out.println("参数错误!");
}
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.mjc;

import java.util.Scanner;
/**
* 题目(90页):假如你准备去海南旅游,现在要订购机票。
* 机票的价格受季节旺季、淡季影响,而且头等舱和经济舱价格也不同。
* 假设机票原价为5000元,
* 4~ 10月为旺季,旺季头等舱打9折,经济舱打8折,其他月份为淡季,
* 淡季头等舱打5折,经济舱打4折。编写程序,根据出行的月份和选择的舱位输出实际的机票价格
* 用switch写此题目
*/
public class Jd4 {
public static void main(String[] args) {
//第四章简答题4(第90页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入您出行的月份:1~12");
int m=sc.nextInt(); //录入月份;
int yuan=5000; //机票原价;
double sum; //打完折的金额;
switch (m) {
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
System.out.println("请问您选择头等舱还是经济舱?头等舱输入1,经济舱输入2。");
int n=sc.nextInt(); //录入头等舱还是经济舱;
if(n==1) {
sum=yuan*0.9;
System.out.print("您的机票价格为:"+sum);
}
else if(n==2) {
sum=yuan*0.8;
System.out.print("您的机票价格为:"+sum);
}
else {
System.err.println("输入的舱位有误!!!");
}
break;
case 1:
case 2:
case 3:
case 11:
case 12:
System.out.println("请问您选择头等舱还是经济舱?头等舱输入1,经济舱输入2。");
n=sc.nextInt(); //录入头等舱还是经济舱;
if(n==1) {
sum=yuan*0.5;
System.out.print("您的机票价格为:"+sum);
}
else if(n==2) {
sum=yuan*0.4;
System.out.print("您的机票价格为:"+sum);
}else {
System.err.println("输入的舱位有误!!!");
}
break;
default:
System.err.println("请输入有效的月份!!!");
break;
}
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.mjc;

import java.util.Scanner;

public class Jd5 {
public static void main(String[] args) {
//第四章简答题5(第90页)
Scanner sc = new Scanner(System.in);
System.out.println("欢迎使用本计算器");
System.out.print("请输入第一个数字:");
double num1= sc.nextDouble();
System.out.print("请输入运算符号:");
String i= sc.next();
System.out.print("请输入第二个数字:");
double num2= sc.nextDouble();

switch(i) {
case "+":
double result = num1 + num2;
System.out.println("运算结果为:"+result);
break;
case "-":
result = num1 - num2;
System.out.println("运算结果为:"+result);
break;
case "*":
result = num1 * num2;
System.out.println("运算结果为:"+result);
break;
case "/":
if (num2 == 0) {
result = 0;
}else{
result = num1 / num2;
}
System.out.println("运算结果为:"+result);
break;
default:System.out.println("错误的参数");
}
}
}

第五章-循环结构(一)

本章学习内容:while循环,do-while循环

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.mjc;

public class Lx1 {
public static void main(String[] args) {
//第五章上机练习1(第104页)
int sum = 0; //当前之和
int num = 2; //加数
while (num <= 100){
sum = sum + num; //累加
num = num + 2;
}
System.out.println("100以内的偶数和为:" + sum);
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.mjc;

import java.util.Scanner;

public class Lx2 {
public static void main(String[] args) {
//第五章上机练习2(第105页)
String name = ""; //商品名称
double price = 0.0; //商品价格
int goodsNo = 0; //商品编号
System.out.println("MyShopping 管理系统 > 购物结算\n");
//商品清单
System.out.println("*****************************");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
System.out.println("*****************************");
Scanner input = new Scanner(System.in);
String answer = "y"; //标识是否继续
while ("y".equals(answer)){
System.out.print("请输入商品编号:");
goodsNo = input.nextInt();
switch (goodsNo){
case 1:
name = "T恤";
price = 245.0;
break;
case 2:
name = "网球鞋";
price = 570.0;
break;
case 3:
name = "网球拍";
price = 320.0;
break;
}
System.out.println(name + "\t" + "¥" + price + "\n");
System.out.println("是否继续?(y/n)");
answer = input.next();
}
System.out.println("程序结束!");
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.mjc;

import java.util.Scanner;

public class Lx3 {
public static void main(String[] args) {
//第五章上机练习3(第107页)
String name = ""; //商品名称
double price = 0.0; //商品价格
int goodsNo = 0; //商品编号
int amount = 0; //购买数量
double dis = 0.8; //折扣比例
double total = 0.0; //商品总价
double pay = 0.0; //实付金额
double sum = 0;
System.out.println("*****************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
System.out.println("*****************************");
Scanner input = new Scanner(System.in);
String answer = "y";
while ("y".equals(answer)){
System.out.print("请输入商品编号:");
goodsNo = input.nextInt();
System.out.print("请输入商品数量:");
amount = input.nextInt();
switch (goodsNo){
case 1:
name = "T恤";
price = 245.0;
break;
case 2:
name = "网球鞋";
price = 570.0;
break;
case 3:
name = "网球拍";
price = 320.0;
break;
default:
break;
}
if (goodsNo <= 3) {
System.out.println(name + "¥" + price + "\t" + "数量" + amount + "\t" + "合计" + (price*amount));
System.out.print("是否继续?(y/n):");
answer = input.next();
System.out.println();
} else {
System.out.println("输入错误");
}

total += price*amount;
}
sum = total * dis;
System.out.println("本次购物你享受的折扣:" + dis);
System.out.println("应付金额:" + sum);
System.err.println("商品原价:" + total);
System.out.print("请给我钱钱!");
pay = input.nextInt();
if (pay < sum) {
System.out.println("抱歉 !输入的金额无法购买本次商品哦!");
} else {
System.out.println("购买成功!谢谢惠顾!");
System.out.println("找你钱钱:" + (pay - sum));
}
}
}

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.mjc;

import java.util.Scanner;

public class Lx4 {
public static void main(String[] args) {
//第五章上机练习4(第110页)
Scanner input = new Scanner(System.in);
boolean isRight = true;
System.out.println("欢迎使用MyShopping管理系统\n");
System.out.println("*****************************");
System.out.println("1.客户信息管理");
System.out.println("2.购物结算");
System.out.println("3.真情回顾");
System.out.println("4.注销");
System.out.println("*****************************");
System.out.print("请选择,输入数字:");
do {
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("执行客户信息管理\n");
isRight = false;
break;
case 2:
System.out.println("执行购物结算\n");
isRight = false;
break;
case 3:
System.out.println("执行真情回顾\n");
isRight = false;
break;
case 4:
isRight = false;
break;
default:
System.out.print("输入有误,请重新输入数字:");
}
} while (isRight);
System.out.println("结束");
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.mjc;

public class Jd2 {
public static void main(String[] args) {
//第五章简答题2(第112页)
//100,95,90,。。5
int i = 105;
while(i>=10){
i = i-5;
System.out.println(i);
}
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.mjc;

public class Jd3 {
public static void main(String[] args) {
//第五章简答题3(第113页)
int i = 0;
int sum = 0;
do{
if(i%7==0){
sum += i;
}
i++;
}while(i<50);
System.out.println(sum);
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.mjc;

import java.util.Scanner;

public class Jd4 {
public static void main(String[] args) {
//第五章简答题4(第113页)
Scanner sc = new Scanner(System.in);
int num;
int max;
int min;
System.out.print("请输入一个整数(输入0结束):");
max=min=num=sc.nextInt();
while (num!=0)
{
System.out.print("请输入一个整数(输入0结束):");
num=sc.nextInt();
if(num>max) {
max=num;
}
if(num<min && num!=0) {
min=num;
}
}
System.out.println("最大值是:"+max+"\t"+"最小值是:"+min+"\n");
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.mjc;

import java.util.Scanner;

public class Jd5 {
public static void main(String[] args) {
//第五章简答题5(第113页)
Scanner sc = new Scanner(System.in);
int num;
String day = "";
System.out.print("请输入数字1-7(输入0结束):");
num = sc.nextInt();
while (num != 0){
switch (num) {
case 1:
day = "MON";
System.out.println("今天是 " + day);
break;
case 2:
day = "TUE";
System.out.println("今天是 " + day);
break;
case 3:
day = "WED";
System.out.println("今天是 " + day);
break;
case 4:
day = "THU";
System.out.println("今天是 " + day);
break;
case 5:
day = "FRI";
System.out.println("今天是 " + day);
break;
case 6:
day = "SAT";
System.out.println("今天是 " + day);
break;
case 7:
day = "SUN";
System.out.println("今天是 " + day);
break;
default:
System.out.println("输入错诶!");
}
System.out.print("请输入数字1-7(输入0结束):");
num = sc.nextInt();
}
System.out.println("程序结束!");
}
}

第六章-循环结构(二)

本章学习内容for循环,关键字:break,continue

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.mjc;

public class Lx1 {
public static void main(String[] args) {
//第六章上机练习1(第123页)
int sum = 0;
for (int num = 0; num <= 100; num++) {
if (num % 2 == 1) {
sum += num;
}
}
System.out.println("100以内奇数和为:" + sum);
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.mjc;

import java.util.Scanner;

public class Lx2 {
public static void main(String[] args) {
//第六章上机练习2(第123页)
int young = 0;
int age = 0;
Scanner input = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
System.out.print("请输入第" + (i+1) + "位顾客年龄:");
age = input.nextInt();
if (age >= 0 && age <= 30) {
young++;
}
}
System.out.println("30岁以下的比例是:" + young/10.0*100 + "%");
System.out.println("30岁以上的比例是:" + (1-young/10.0)*100 + "%");
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.mjc;

import java.util.Scanner;

public class Lx3 {
public static void main(String[] args) {
//第六章上机练习3(第129页)
System.out.println("MyShooping管理系统>客户信息管理>添加客户信息");
int custNo = 0; //会员号
String birthday; //会员生日
int points = 0; //会员积分
Scanner input = new Scanner(System.in);
for (int i = 0; i < 3; i++) {//录入会员信息
System.out.print("请输入会员号:(<4位整数>)");
custNo = input.nextInt();
System.out.print("请输入会员生日(月/日<用两位整数表示>)");
birthday = input.next();
System.out.print("请输入会员积分:");
points = input.nextInt();
if (custNo < 1000 || custNo > 9999) {//会员号无效则跳出
System.out.println("客户会员号" + custNo + "是无效会员号!");
System.out.println("录入会员信息失败!\n");
continue;
}
System.out.println("您录入会员信息是:");
System.out.println(custNo + " " + birthday + " " + points + "\n");
}
System.out.println("程序结束!");
}
}

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.mjc;

import java.util.Scanner;

public class Lx4 {
public static void main(String[] args) {
//第六章上机练习4(第129页)
Scanner sc = new Scanner(System.in);
int password;
String user;
int count = 3;
for (count = 0; count < 3; count++) {
System.out.print("请输入用户名:");
user = sc.next();
System.out.print("请输入密码:");
password = sc.nextInt();
if (user.equals("xc") && password == 123456) {
System.out.println("登录成功!");
break;
} else {
System.out.println("输入错误!你还有" + (2-count) + "次机会");
}
}
if (count == 3) {
System.out.println();
System.out.println("对不起!您三次均输入失败");
}
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.mjc;

import java.util.Scanner;

public class Jd2 {
public static void main(String[] args) {
//第六章简答2(第134页)
Scanner sc = new Scanner(System.in);
double sum = 0;
for (int i = 0; i < 5; i++) {
System.out.print("请输入周" + (i+1) + "的学习时间:");
int day = sc.nextInt();
sum += day;
}
System.out.println("周一到周五平均学习时间是:" + (sum/5) + "小时");
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.mjc;

public class Jd3 {
public static void main(String[] args) {
//第六章简答3(第134页)
int chookNum; //鸡子
int rabbitNum; //兔子
/*
从题目中已知鸡子+兔子的数量=35只,那么兔子或鸡子的数量不会超过35只,
利用兔子数量和鸡子数量这两个条件,使用两个for循环,第一个for循环判断鸡子,第二个for循环判断兔子
因为鸡子有两只脚,兔子有四只脚,所以用if判断这两个条件,即可得到鸡子和兔子的数量
*/
for (chookNum = 0;chookNum <= 35; chookNum++) {
for (rabbitNum = 0; rabbitNum<35; rabbitNum++) {
if (rabbitNum + chookNum == 35 && 2 * chookNum + 4 * rabbitNum == 94) {
System.out.println("鸡子有:" + chookNum + "只" + "兔子有:" + rabbitNum + "只");
}
}
}
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.mjc;

import java.util.Scanner;

public class Jd4 {
public static void main(String[] args) {
//第六章简答4(第135页)
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 100; i++) {
System.out.print("请输入数字:");
int sum = sc.nextInt();
if (sum % 3 == 0 && sum % 5 == 0) {
System.out.println("FlipFlop");
}else if (sum % 3 == 0) {
System.out.println("Flip");
}else if (sum % 5 == 0) {
System.out.println("Flop");
}else {
System.out.println(sum);
}
}
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.mjc;

public class Jd5 {
public static void main(String[] args) {
//第六章简答5(第135页)
int men; //男人
int women; //女人
int kids; //小孩

for (men = 0; men <=10; men++)
for (women = 0; women <= 30; women++)
for (kids = 0; kids <= 30; kids++) {
if (kids + women + men == 30 && 3 * men + 2 * women + kids == 50) {
System.out.println("男人可能有: " + men + "女人可能有:" + women + "小孩可能有:" + kids);
}
}
}
}

第七章-循环练习

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.mjc;

import java.util.Scanner;

public class Lx1 {
public static void main(String[] args) {
//第七章上机练习1(第139页)
Scanner sc = new Scanner(System.in);
boolean flag = true;
System.out.println("欢迎进入青鸟游戏迷你平台");
System.out.println("********************");
System.out.println("\t1.斗地主");
System.out.println("\t2.斗牛");
System.out.println("\t3.泡泡龙");
System.out.println("\t4.连连看");
System.out.println("********************");
System.out.print("请选择,输入数字:");
int num = sc.nextInt();
do {
switch (num) {
case 1:
System.out.println("您已进入斗地主游戏!\n");
flag = false;
break;
case 2:
System.out.println("您已进入斗牛游戏!\n");
flag = false;
break;
case 3:
System.out.println("您已进入泡泡龙游戏!\n");
flag = false;
break;
case 4:
System.out.println("您已进入连连看游戏!\n");
flag = false;
break;
default:
System.out.print("输入有误,请重新输入数字:");
num = sc.nextInt();
}
} while (flag);
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.mjc;

import java.util.Scanner;

public class Lx2 {
public static void main(String[] args) {
//第七章上机练习2(第140页)
Scanner sc = new Scanner(System.in);
int count = 0;
int n = 1;
do {
System.out.print("您正在玩第" + n + "局,成绩为:");
int score = sc.nextInt();
if (score > 80) {
count++;
}
n++;
if (n > 5) {
System.out.print("游戏结束!");
} else {
System.out.print("继续玩下一局吗?(yes/no)");
String answer = sc.next();
if (answer.equals("no")) {
System.out.print("您已中途退出游戏。");
break;
} else {
System.out.print("进入下一局");
}
}
}while (n <= 5);
double rate = count / 5.0;
if (n > 5) {
if (rate > 0.8) {
System.out.println("\n恭喜!通过一级");
} else if (rate > 0.6) {
System.out.println("\n恭喜!通过二级");
} else {
System.out.println("\n对不起!您未通过晋级,继续加油啊!");
}
} else {
System.out.println("\n对不起!您未通过晋级,继续加油啊!");
}
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.mjc;

import java.util.Scanner;

public class Lx3 {
public static void main(String[] args) {
//第七章上机练习3(第141页)
Scanner sc = new Scanner(System.in);
System.out.println("青鸟迷你游戏平台>游戏币支付");
System.out.println("请选择您玩的游戏类型:");
System.out.println("\t1.牌类");
System.out.println("\t2.休闲竞技类");
int n = sc.nextInt();
System.out.println("请输入您的游玩时长:");
int time = sc.nextInt();
int sum = 0;
switch (n) {
case 1:
sum = time * 10;
break;
case 2:
sum = time * 20;
break;
default:
System.out.println("输入有误!");
break;
}
if (time > 10) {
System.out.println("您玩的是牌类游戏,时长是:" + time + ",可享受5折优惠");
System.out.println("您需支付" + sum * 0.5);
}
if (time < 10 && n <= 2){
System.out.println("您玩的是休闲竞技类游戏,时长是:" + time + ",可享受8折优惠");
System.out.println("您需支付" + sum * 0.8);
}
}
}

此处有bug,自行调试

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.mjc;

import java.util.Scanner;

public class Lx4 {
public static void main(String[] args) {
//第七章上机练习4(第142页)
Scanner sc = new Scanner(System.in);
int num = 0;
int count = 0;
double reat;
System.out.println("青鸟游戏迷你游戏平台 > 游戏点击率\n");
for (int i = 0; i < 4; i++) {
System.out.print("请输入第" + (i+1) + "个点击率:");
count = sc.nextInt();
if (count > 100) {
num++;
}
}
reat = (num / 4.0) * 100;
System.out.println("点击率大于100的个数:" + num);
System.out.println("点击率大于100所占比:" + reat + "%");
}
}

上机练习5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.mjc;

import java.util.Scanner;

public class Lx5 {
public static void main(String[] args) {
//第七章上机练习5(第143页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户数量:");
int user = sc.nextInt();
int custNo = 0; //用户编号
int birthday; //会员生日
int points = 0; //会员积分
for (int i = 0; i < user; i++) {
System.out.print("请输入会员编号:(<4位整数>)");
custNo = sc.nextInt();
System.out.print("请输入会员年龄(月/日<用两位整数表示>)");
birthday = sc.nextInt();
System.out.print("请输入会员积分:");
points = sc.nextInt();
if ((birthday < 10)) {//年龄无效则跳出
System.out.println("很抱歉,你的年龄不适合玩游戏!");
System.out.println("录入会员信息失败!\n");
continue;
}
if (custNo < 1000 || custNo > 9999) {//会员号无效则跳出
System.out.println("客户会员号" + custNo + "是无效会员号!");
System.out.println("录入会员信息失败!\n");
continue;
}
System.out.println("您录入会员信息是:");
System.out.println("用户编号:" + custNo + "\t年龄:" + birthday + " " + "积分:" + points);
}
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.mjc;

import java.util.Scanner;

public class Jd2 {
public static void main(String[] args) {
//第七章简答题2(第144页)
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个正整数(1~10)");
if (sc.hasNextInt()) {
int n = sc.nextInt();
if(n <= 10 && n >= 1){
System.out.print(n + "!=");
int result = 1;
for(int i = 1;i <= n;i++){
result *= i;
if(i == n){
System.out.print(i + "=");
} else {
System.out.print(i + "*");
}
}
System.out.print(result);
} else {
System.out.println("无效数据!");
}
} else {
System.out.println("请输入整数!");
}
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.mjc;

public class Jd3 {
public static void main(String[] args) {
//第七章简答题3(第145页)
int sum = 0;
int count = 0;
for (int i = 1; i <= 100; i++) {
if (i % 7 != 0) {
System.out.print(i + "\t");
sum += i;
count++;
if (count == 4) {
System.out.print("\n");
count = 0;
}
}
}
System.out.println("\n数值之和为:" + sum);
}
}

第八章-数组

本章学习内容:数组的初始化,分配空间,声明,比较大小,插入,遍历数组,查找下标

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.mjc;

public class Lx1 {
public static void main(String[] args) {
//第八章上机练习1(第156页)
String[] goods = new String[]{"Nike背包","Adidas运动衫","李宁运动鞋","Kappa外套","361°腰包"};
System.out.println("本次活动特价商品有:");
//使用for循环遍历数组
for (int i = 0; i < goods.length; i++) {
System.out.println(goods[i]);
}
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.mjc;

import java.util.Scanner;

public class Lx2 {
public static void main(String[] args) {
//第八章上机练习2(第157页)
Scanner sc = new Scanner(System.in);
double[] money = new double[5]; //购物金额
double sum = 0; //总金额
System.out.println("请输入会员本月消费记录");
for (int i = 0; i < money.length; i++) {
System.out.print("请输入第" + (i+1) + "笔购物金额:");
money[i] = sc.nextDouble();
sum += money[i]; //累加
}
System.out.println("序号\t\t\t" + "金额(元)");
for (int i = 0; i < money.length; i++) {
System.out.print(i+1 + "\t\t\t");
System.out.println(money[i]); //输出数组money的值
}
System.out.println("总金额为:" + "\t" + sum);
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.mjc;

import java.util.Arrays;

public class Lx3 {
public static void main(String[] args) {
//第八章上机练习3(第162页)
String[] ch = new String[]{"a", "c", "u", "b", "e", "p", "f", "z",};
System.out.print("原字符序列:");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}
Arrays.sort(ch);
System.out.print("\n升序排序后:");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}
System.out.print("\n逆序输出为:");
for (int i = ch.length - 1; i >= 0; i--) {
System.out.print(ch[i] + " ");
}
}
}

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.mjc;

import java.util.Scanner;

public class Lx4 {
public static void main(String[] args) {
//第八章上机练习4(第163页)
char[] chars = new char[9];
chars[0] = 'a';
chars[1] = 'b';
chars[2] = 'c';
chars[3] = 'e';
chars[4] = 'f';
chars[5] = 'p';
chars[6] = 'u';
chars[7] = 'z';

//打印原字符
System.out.print("原字符序列:");
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i] + " ");
}

//输入插入的元素
System.out.println();
Scanner input = new Scanner(System.in);
System.out.print("待输入的字符是:");
char ele = input.next().charAt(0);

//找到插入的位置(下标)
int index = chars.length;
for(int i = 0; i < chars.length; i++){
if(chars[i] > ele){
index = i;
break;
}
}
System.out.println("插入字符的下标是:" + index);

//元素后移
for (int i = chars.length-1; i > index; i--) {
chars[i] = chars[i-1];
}

//打印添加后的元素
chars[index] = ele;
System.out.print("插入后的字符序列是:");
for (int k = 0; k < chars.length; k++) {
System.out.print(chars[k] + " ");
}

}
}

上机练习5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.mjc;

import java.util.Scanner;

public class Lx5 {
public static void main(String[] args) {
//第八章上机练习5(第164页)
Scanner sc = new Scanner(System.in);
int[] money = new int[4];
System.out.println("请输入4家店的价格");
for (int i = 0; i < money.length; i++) {
System.out.print("请输入第" + (i+1) + "家金额:");
money[i] = sc.nextInt();
}
int min = money[0];
for (int i = 1; i < money.length; i++) {
if (min > money[i]) {
min = money[i];
}
}
System.out.println("最低价格是:" + min);
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.mjc;

import java.util.Scanner;

public class Jd2 {
public static void main(String[] args) {
//第八章简答题2(第166页)
Scanner sc = new Scanner(System.in);
String[] ju = new String[5];
System.out.println("请输入五句话");
for (int i = 0; i < ju.length; i++) {
System.out.print("第" + (i+1) + "句话:");
ju[i] = sc.next();
}
System.out.println("\n逆序输出为:");
for (int i = ju.length - 1; i >= 0; i--) {
System.out.println(ju[i]);
}
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.mjc;

public class Jd3 {
public static void main(String[] args) {
//第八章简答题3(第166页)
int[] points = {18, 25, 7, 36, 13, 2, 89, 63};
int min = points[0];
int index = points.length;
for (int i = 1; i < points.length; i++) {
if (min > points[i]) {
min = points[i];
index = i;
}
}
System.out.println("最低价格是:" + min + "\t该值的下标为:" + index);
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.mjc;

import java.util.Scanner;

public class Jd4 {
public static void main(String[] args) {
//第八章简答题4(第167页)
Scanner sc = new Scanner(System.in);
int[] nums = new int[10];
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
System.out.println("请输入10个数字");
for (int i = 0; i < nums.length; i++) {
nums[i] = sc.nextInt();
switch (nums[i]) {
case 1: count1++;
break;
case 2: count2++;
break;
case 3: count3++;
break;
default: count4++;
}
}
System.out.println("数字1的个数:" + count1);
System.out.println("数字2的个数:" + count2);
System.out.println("数字3的个数:" + count3);
System.out.println("非法数字的个数:" + count4);
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.mjc;

public class Jd5 {
public static void main(String[] args) {
//第八章简答题5(第167页)
int[] Array = new int[]{1, 3, -1, 5, -2}; //原数组
int[] newArray = new int[5]; //新数组可写为Array.length
System.out.println("原数组为:"); //使用for循环遍历数组
for (int i = 0; i < Array.length; i++) { //数字用逗号隔开
if (i != Array.length - 1) { //表示不是最后一位,打印值和,
System.out.print(Array[i] + ", ");
} else { //当i是末尾的时候打印值
System.out.print(Array[i]);
}
}
System.out.println(); //换行
for (int j = Array.length - 1; j >= 0; j--) { //逆序排列并处理,j从4开始递减
//Array.length:5
if (Array[j] < 0) {
Array[j] = 0;
}
for ( int k = 0 ; k <= Array.length-j-1 ; k++ ) { //新值赋给新数组,升序
//k:从0到4
newArray[j] = Array[k]; //赋值给新数组,得到处理后的值
//4:1,3:3,2:0,1:5,0:0
}
continue;
}
//打印新值
System.out.println("逆序并处理后的数组为:");
for (int k = 0; k <= Array.length - 1; k++) {
if (k != Array.length - 1) {
System.out.print(newArray[k] + ", ");
} else {
System.out.print(newArray[k]);
}
}
}
}

简答题6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.mjc;

import java.util.Scanner;

public class Jd6 {
public static void main(String[] args) {
//第八章简答题6(第168页)
Scanner sc = new Scanner(System.in);
String[] music = {"Island","Ocean","Pretty","Sun"}; //原数组
String[] newMusic = new String[5]; //新数组

System.out.print("插入前的数组为:");
for (int i = 0; i < music.length; i++) {
System.out.print(music[i] + "\t");
}//将原数组赋值给新数组,以便后续的插入
for(int i = 0; i < music.length;i++){
newMusic[i] = music[i];
}

System.out.print("\n请输入歌曲名称:");
String name = sc.nextLine();
int index = newMusic.length; //保存新增下标
for (int j = 0; j < newMusic.length;j++) {
if(newMusic[j].compareToIgnoreCase(name) > 0){ //比较字符串大小
index = j; //找到下标,退出循环
break;
}
}

for (int i = newMusic.length - 1; i > index; i--) {
newMusic[i] = newMusic[i-1];
}
newMusic[index] = name;

System.out.print("插入后的数组为:");
for (int i = 0; i < newMusic.length; i++) {
System.out.print(newMusic[i] + " ");
}
}
}

第九章-循环结构进阶

本章学习内容:双重循环,三重循环,经典题目99乘法表,杨辉三角,打印图案

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.mjc;

import java.util.Scanner;

public class Lx1 {
public static void main(String[] args) {
//第九章上机练习1(第171页)
double[] sours = new double[4];
double sum = 0; //总成绩
Scanner sc = new Scanner(System.in);
System.out.println("请输入四位同学成绩:");
for (int i = 0; i < sours.length; i++) {
System.out.print("第" + (i+1) + "位同学成绩:");
sours[i] = sc.nextDouble();
sum += sours[i];
}
System.out.println("参赛学员的平均分是:" + sum / sours.length);
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.mjc;

import java.util.Scanner;

public class Lx2 {
public static void main(String[] args) {
//第九章上机练习2(第176页)
Scanner sc = new Scanner(System.in);
System.out.print("请输入行号:");
int row = sc.nextInt();
for (int i = 0; i < row; i++) {
for (int j = 0; j <= 2 * i; j++) {
System.out.print("*");
}
//换行
System.out.println();
}
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.mjc;

import java.util.Scanner;

public class Lx3 {
public static void main(String[] args) {
//第九章上机练习3(第177页)
Scanner sc = new Scanner(System.in);
System.out.print("请输入行号:");
int row = sc.nextInt();
//外层循环控制行,换行
for (int i = 0; i < row; i++) {
//内层循环控制列,符号
for (int j = 1; j <= row - i; j++) {
System.out.print("*");
}
//换行
System.out.println();
}
}
}

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.mjc;

import java.util.Scanner;

public class Lx4 {
public static void main(String[] args) {
//第九章上机练习4(第178页)
Scanner sc = new Scanner(System.in);
Scanner input = new Scanner(System.in);
System.out.print("输入等腰三角形的行数:");
int rows = input.nextInt();
//打印等腰三角形
for (int i = 0; i <= rows; i++) {
//空格分布
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
//符号分布
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}

上机练习5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.mjc;

public class Lx5 {
public static void main(String[] args) {
//第九章上机练习5(第179页)
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i + "*" + j + "=" + i * j + "\t");
}
//换行
System.out.println();
}
}
}

上机练习6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.mjc;

import java.util.Scanner;

public class Lx6 {
public static void main(String[] args) {
//第九章上机练习6(第183页)
Scanner sc = new Scanner(System.in);
double price;
int count = 0;
for (int i = 0; i < 3; i++) {
System.out.println("请输入第" + (i+1) + "个人所购的三件商品价格:");
for (int j = 0; j < 3; j++) {
price = sc.nextInt();
if (price >= 300) {
count++;
}
}
System.out.println("第" + (i+1) + "个人共有" + count + "件商品享受8折优惠!");
count = 0;
}
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.mjc;

import java.util.Scanner;

public class Jd2 {
public static void main(String[] args) {
//第九章简答题2(第186页)
Scanner sc = new Scanner(System.in);
System.out.print("请输入行数:");
int num = sc.nextInt();
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "\t");
}
System.out.println();
}
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.mjc;

public class Jd3 {
public static void main(String[] args) {
//第九章简答题3(第186页)
for (int x = 1; x <= 19; x++) {
for (int y = 1; y <= 31; y++) {
int z = 100 - x - y;
if ((5 * x + 3 * y + z / 3 == 100) && ( z % 3 == 0)) {
System.out.println("可能买到公鸡:" + x + "只" + "\t" + "可能买到母鸡:"+ y + "只" + "\t" + "可能买到小鸡:" + z + "只");
}
}
}
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.mjc;

import java.util.Scanner;

public class Jd4 {
public static void main(String[] args) {
//第九章简答题4(第186页)
Scanner sc = new Scanner(System.in);
double sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("请输入学员成绩:");
double sour = sc.nextDouble();
if (sour > 85) {
sum += sour;
}
}
}
System.out.println("各班大于85分学员平均分为:" + sum / 12);
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.mjc;

import java.util.Scanner;

public class Jd5 {
public static void main(String[] args) {
//第九章简答题5(第186页)
Scanner sc = new Scanner(System.in);
boolean flag = true;
for (int i = 0; i < 3; i++) {
System.out.print("请输入密码:");
int password = sc.nextInt();
if (password == 111111) {
System.out.print("输入金额:");
int amount = sc.nextInt();
while (flag) {
if (amount % 100 == 0 && amount >= 0 && amount <= 1000) {
System.out.println("您需要取出的钱数为:" + amount + "元");
flag = false;
} else {
System.out.print("您输入的金额不合法,请重新输入:");
amount = sc.nextInt();
}
}
break;
}
}
if (flag) {
System.out.println("密码错误,请取卡");
} else {
System.out.println("交易完成,请取卡");
}
}
}

简答题6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.mjc;

import java.util.Scanner;

public class Jd6 {
public static void main(String[] args) {
//第九章简答题6(第187页)
Scanner sc = new Scanner(System.in);
System.out.print("请输入菱形行数:");
int rows = sc.nextInt();
while (rows % 2 == 0) {
System.out.print("请输入奇数:");
rows = sc.nextInt();
}
int n = (rows + 1) / 2;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= rows - 2 * i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}

第十章-幸运抽奖

幸运抽奖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.mjc;

import java.util.Scanner;

public class Demo {
public static void main(String[] args) {
String cishu;
String username = "";
int password = 0;
int[] luckynumber = new int[5];
//生成随机数:例:求a-b (int) (Math.random * 10000) % (b - a + 1) + a
int cardNumber = (int) (Math.random() * 10000) % (9999 - 1000 + 1) + 1000;
boolean zc = false;
boolean dl = false;
do {
System.out.println("********欢迎进入奖客富翁系统********");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.抽奖");
System.out.println("*******************************");
Scanner in = new Scanner(System.in);
System.out.print("请选择菜单:");
int num = in.nextInt();
switch(num) {
case 1:
System.out.println("[奖客富翁系统 > 注册]");
System.out.println("请填写个人注册信息:");
System.out.print("用户名:");
username = in.next();
System.out.print("密码:");
password = in.nextInt();
System.out.println();
System.out.println("注册成功,请记好您的会员号");
System.out.println("用户名\t密码\t会员号");
System.out.println(username + "\t" + password + "\t" + cardNumber);
zc = true;
break;
case 2:
if (zc) {
for (int i = 1; i < 4; i++) {
System.out.println("[奖客富翁系统 > 登录]");
System.out.print("请输入用户名:");
String username1 = in.next();
System.out.print("请输入密码:");
int password1 = in.nextInt();
if ((username1.equals(username))&&(password1==password)) {
System.out.println("欢迎您:"+username);
dl = true;
break;
} else if ((3 - i) == 0) {
System.out.println("抱歉,您三次输入均失败!");
} else {
System.out.println("用户名或密码错误!请重新输入当前第"+i+"次!");
}
}
} else {
System.out.println("抱歉,您还未注册!");
}
break;
case 3:
if (dl) {
System.out.println("[奖客富翁系统 > 抽奖]");
for(int i = 1;i < 4;i++) {
System.out.print("请输入您的卡号:");
int cardNumber1 = in.nextInt();
if(cardNumber1==cardNumber) {

for (int j = 0; j < luckynumber.length; j++) {
luckynumber[j] = (int) (Math.random() * 10000) % (9999 - 1000 + 1) + 1000;
}

System.out.println("本日的幸运数字为:");
for (int j = 0; j < luckynumber.length; j++) {
System.out.print(luckynumber[j] + " ");
}

System.out.println();
for (int j = 0; j < luckynumber.length; j++) {
if (cardNumber == luckynumber[j]) {
System.out.println("恭喜你成为本日幸运用户!");
} else {
System.out.println("抱歉您不是本日幸运用户!");
}
break;
}
}else {
System.out.println("会员卡号不正确,您还有"+(3-i)+"次机会!");
}
}
break;
} else {
System.out.println("您必须先进行登录,才可抽奖");
}
break;
default:
System.out.println("您的输入有误!");
}
System.out.print("\n继续吗?(y/n):");
cishu = in.next();
System.out.println();
} while (cishu.equals("y"));
System.out.println();
System.out.println("系统退出,谢谢使用!");
}
}

第十一章-面向对象-类和对象

本想学习内容:创建对象,创建类,赋值,方法的使用,调用

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.mjc;

public class Administrator {
//第十一章上机练习1(第214页)
String name = "xc";
String password = "xc1245";
public void show() {
System.out.println("姓名:" + name + ",密码:" + password);
}

public static void main(String[] args) {
Administrator admin = new Administrator();
admin.show();
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.mjc;

public class Customer {
//第十一章上机练习2(第215页)
int score = 2000;
String type = "金卡";
public void show() {
System.out.println("积分:" + score + ",卡类型:" + type);
}

public static void main(String[] args) {
Customer ji = new Customer();
ji.show();
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.mjc;

public class AdministratorTest {
public static void main(String[] args) {
//第十一章上机练习3(第215页)
Administrator admin1 = new Administrator();
Administrator admin2 = new Administrator();
admin1.name = "admin1";
admin1.password = "111111";
admin1.show();
admin2.name = "admin2";
admin2.password = "222222";
admin2.show();
}
}

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.mjc;

import java.util.Scanner;

public class ChangePassword {
public static void main(String[] args) {
//第十一章上机练习4(第216页)
Scanner input = new Scanner(System.in);
Administrator admin = new Administrator(); //创建管理员对象
admin.name = "admin1"; //给name属性赋值
admin.password = "111111"; //给password属性赋值
//输入旧的用户名和密码
System.out.print("请输入用户名:");
String nameinput = input.next();
System.out.print("请输入密码:");
String pwd = input.next();
//判断用户输入的用户名或密码是否正确
if (admin.name.equals(nameinput) && admin.password.equals(pwd)) {
System.out.print("\n请输入新密码:");
admin.password = input.next(); //修改密码
System.out.println("修改密码成功,您的新密码为:" + admin.password);
} else {
System.out.println("用户名和密码不匹配!您没有权限更新管理员信息。");
}
}
}

上机练习5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.mjc;

public class Score {
String type = "金卡"; //卡类型
int num = 3050; //积分
public void show() {
//第十一章上机练习5(第217页)
System.out.println("积分:" + num + ",卡类型:" + type);
if ("金卡".equals(type) && num >= 1000 || "普卡".equals(type) && num >= 5000){
System.out.println("回馈500积分!");
} else {
System.out.println("抱歉,您没有积分!");
}
}
public static void main(String[] args) {
Score sc = new Score();
sc.show();
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.mjc;

import java.util.Scanner;

public class Calculation {
//第十一章简答题2(第219页)
double num1;
String i;
double num2;
public void show() {
switch (i) {
case "+":
double result = num1 + num2;
System.out.println("运算结果为:" + result);
break;
case "-":
result = num1 - num2;
System.out.println("运算结果为:" + result);
break;
case "*":
result = num1 * num2;
System.out.println("运算结果为:" + result);
break;
case "/":
if (num2 == 0) {
result = 0;
}else{
result = num1 / num2;
}
System.out.println("运算结果为:" + result);
break;
default:System.out.println("错误的参数");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Calculation js = new Calculation();
System.out.print("请输入第一个数字:");
js.num1 = sc.nextDouble();
System.out.print("请输入运算符号:");
js.i = sc.next();
System.out.print("请输入第二个数字:");
js.num2 = sc.nextDouble();
js.show();
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.mjc;

public class CurrentTime {
//第十一章简答题3(第220页)
String curTime = "2015年5月12日10点11分00秒";
public void show() {
System.out.println(curTime);
}
public static void main(String[] args) {
CurrentTime times = new CurrentTime();
times.show();
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.mjc;

import java.util.Scanner;

public class Demo {
//第十一章简答题4(第220页)
String curTime = "2015年5月12日10点11分00秒";
public void show() {
System.out.println(curTime);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CurrentTime times = new CurrentTime();
times.curTime = "2015年5月12日10点11分30秒";
times.show();
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.mjc;

public class Computer {
//第十一章简答题5(第220页)
String cpu = "无敌AMD 线程撕裂者3990X";
String mainboard = "华硕玩家过度 Z590";
String display = "三星(SAMSUNG)LH98QPR8BGCXXF 98英寸HDR壁挂8K超高清显示屏";
String harddisk = "三星(SAMSUNG)32GB DDR4 ";
String dish = "西部数据 10TB";

public void showInfo() {
System.out.println("*******************************");
System.out.println("CPU:" + cpu);
System.out.println("显示器:" + mainboard);
System.out.println("主板:" + display);
System.out.println("内存:" + harddisk);
System.out.println("硬盘:" + dish);
System.out.println("*******************************");
}
public static void main(String[] args) {
Computer cop = new Computer();
cop.showInfo();
}
}

简答题6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.mjc;

public class GameTest {
//第十一章简答题6(第220页)
String leagueName = "疾风剑豪";
int life = 9999999;
private void show1() {
System.out.println("我是英雄,我的基本信息如下");
System.out.println("姓名:" + leagueName + ",生命值:" + life + "\n");
}

String monsters = "黑暗银河之剑";
int avg = 999999;
private void show2() {
System.out.println("我是武器,我的基本信息如下");
System.out.println("武器名:" + monsters + ",攻击力:" + avg + "\n");
}

String weapons = "虚空精灵";
int life1 = 500000;
String type = "飞行类";
private void show3() {
System.out.println("我是怪物,我的基本信息如下");
System.out.println("姓名:" + weapons + ",生命值:" + life1 + ",类型:" + type);
}

public static void main(String[] args) {
GameTest league = new GameTest();
GameTest weapon = new GameTest();
GameTest monster = new GameTest();
league.show1();
weapon.show2();
monster.show3();
}
}

第十二章-类的无参方法

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.mjc;

import java.util.Scanner;

public class ScoreCalc {
int java;
int c;
int db;
public int intcalcTotalScore() {
return java + c + db;
}

public void shoeTotalScore() {
System.out.println("总成绩是:" + intcalcTotalScore());
}

public double calcAvg() {
return (java + c + db) / 3.0;
}

public void showAvg() {
System.out.println("平均成绩是:" + calcAvg());
}
}

class TestScoreCalc {

public static void main(String[] args) {
//第十二章上机练习1(第229页)
Scanner sc = new Scanner(System.in);
ScoreCalc jv = new ScoreCalc();
System.out.print("请输入Java成绩:");
jv.java = sc.nextInt();
System.out.print("请输入C#成绩:");
jv.c = sc.nextInt();
System.out.print("请输入DB成绩:");
jv.db = sc.nextInt();
jv.shoeTotalScore();
jv.showAvg();
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.mjc;

public class Manager {
//第十二章上机练习2(第234页)
String show() {
return "管理员信息用户名为:JadeBird\t密码为:0000";
}

public static void main(String[] args) {
Manager mg = new Manager();
System.out.println(mg.show());
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @BelongsProject: Top12
* @BelongsPackage: com.mjc
* @Author: xc
* @Date: 2021/10/1 22:39:43
* @Description: 菜单类
*/
package com.mjc;

import java.util.Scanner;

public class Menu {
public void showLoginMenu() {
System.out.println("\n欢迎使用我行我素购物管理系统");
System.out.println("\t1.登录系统");
System.out.println("\t2.退出");
System.out.println("* ** * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字:");

}

public void showMainMenu() {
System.out.println("\n我行我素购物管理系统主菜单");
System.out.println("\t1.客户信息管理");
System.out.println("\t2.真情回顾");
System.out.println("* ** * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字:");
boolean con;
do {
con = false;
Scanner input = new Scanner(System.in);
int no = input.nextInt();
if (no == 1) {
showCustMMenu();
} else if (no == 2) {
showSendGMenu();
} else if (no == 0) {
showLoginMenu();
} else {
System.out.println("输入错误,请重新选择数字:");
con = true;
}
} while (con);
}

public void showCustMMenu() {
System.out.println("\n我行我素购物管理系统 > 客户信息管理");
System.out.println("\t1.查询客户信息");
System.out.println("\t2.修改客户信息");
System.out.println("\t3.添加客户信息");
System.out.println("\t4.显示所有客户信息");
System.out.println("* ** * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字:");
boolean con;
do {
con = false;
Scanner input = new Scanner(System.in);
int no = input.nextInt();
if (no == 1) {
System.out.println("执行查询客户信息");
} else if (no == 2) {
System.out.println("执行修改客户信息");
} else if (no == 3) {
System.out.println("执行添加客户信息");
} else if (no == 4) {
System.out.println("执行显示所有客户信息");
} else if (no == 0) {
showMainMenu();
} else {
System.out.println("输入错误,请重新选择数字:");
con = true;
}
} while (con);
}

public void showSendGMenu() {
System.out.println("\n我行我素购物管理系统 > 真情回顾");
System.out.println("\t1.幸运大放送");
System.out.println("\t2.幸运抽奖");
System.out.println("\t3.生日问候");
System.out.println("* ** * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字:");
boolean con;
do {
con = false;
Scanner input = new Scanner(System.in);
int no = input.nextInt();
if (no == 1) {
System.out.println("执行幸运大放送");
} else if (no == 2) {
System.out.println("执行幸运抽奖");
} else if (no == 3) {
System.out.println("执行生日问候");
} else if (no == 0) {
showMainMenu();
} else {
System.out.println("输入错误,请重新选择数字:");
con = true;
}
} while (con);
}
}


class TestMenu {
public static void main(String[] args) {
//第十二章上机练习3(第233页)
boolean con = true;
do {
Menu menu = new Menu();
menu.showLoginMenu();
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch (choice) {
case 1:
menu.showMainMenu();
break;
case 2:
System.out.println("谢谢您的使用!");
con = false;
break;
default:
break;
}
} while (con);
}
}

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.mjc;

import java.util.Scanner;

public class StartSMS {
String user;
int password;
public static void main(String[] args) {
//第十二章上机练习4(第237页)
Scanner sc = new Scanner(System.in);
boolean con = true;
do {
StartSMS admin = new StartSMS();
admin.password = 0000;
admin.user = "JadeBird";

Menu sy = new Menu();
sy.showLoginMenu();
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("请输入用户名:");
String userName = sc.next();
System.out.print("请输入密码:");
int pwd = sc.nextInt();
if (userName.equals(admin.user) && pwd == admin.password) {
System.out.println("@@登陆成功,欢迎你" + userName + "@@");
sy.showMainMenu();
break;
} else {
System.out.println("@@您没有权限进入系统,请重新登录@@");
}
break;
case 2:
System.out.println("谢谢您的使用!");
con = false;
break;
default:
break;
}
} while (con);
}
}

简答题1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.mjc;

import java.util.Scanner;

public class Jd1 {
public void Season() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入月份:");
int month = sc.nextInt();
if (month >= 1 && month <= 3) {
System.out.println("\n该季节为春季");
} else if (month >= 4 && month <= 6) {
System.out.println("\n该季节为夏季");
} else if (month >= 7 && month <= 9) {
System.out.println("\n该季节为秋季");
} else if (month >= 10 && month <= 12) {
System.out.println("\n该季节为冬季");
} else {
System.out.println("\n输入有误!");
}
}
public static void main(String[] args) {
//第十二章简答题1(第242页)
Jd1 sc = new Jd1();
sc.Season();
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.mjc;

import java.util.Scanner;

public class Calculator {
double num1; //数字1
double num2; //数字2

/**
* @return 加法运算
*/
double add() {
return num1 + num2;
}

/**
* @return 减法运算
*/
double minus() {
return num1 - num2;
}

/**
* @return 乘法运算
*/
double multiple() {
return num1 * num2;
}

/**
* @return 除法运算
*/
double divide() {
//若被除数为零,结果为零,返回零值
if (num2 == 0) {
return 0;
} else {
//被除数都不为零,做除法运算
return num1 / num2;
}
}

public static void main(String[] args) {
//第十二章简答题3(第243页)
Scanner sc = new Scanner(System.in);
Calculator js = new Calculator();
//创建对象
System.out.print("请输入第一个数字:");
js.num1 = sc.nextDouble();
System.out.print("请输入第二个数字:");
js.num2 = sc.nextDouble();
System.out.print("请输入运算符号:");
String i = sc.next();
//switch结构根据选择的运算符进行运算
switch (i) {
case "+":
//调用加法运算方法
System.out.println("\n" + js.add());
break;
case "-":
System.out.println("\n" + js.minus());
break;
case "*":
System.out.println("\n" + js.multiple());
break;
case "/":
System.out.println("\n" + js.divide());
break;
default:
System.out.println("输入符号错误!");
break;
}
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.mjc;

import java.util.Scanner;

public class ATM {
double Money = 10000.0; //定义double类型变量Money

/**
* @return 返回变量Money的值
*/
double inquire() {
return Money;
}
}

class AtmTest {
public static void main(String[] args) {
//第十二章简答题4(第243页)
ATM atm = new ATM(); //创建对象atm
Scanner sc = new Scanner(System.in);
int pwd = 123456; //密码
//循环四次
for (int i = 1; i < 4; i++) {
System.out.print("请输入密码:");
int password = sc.nextInt();
//如果密码相同则返回账户余额
if (password == pwd) {
System.out.println("您的账户余额为:" + atm.inquire());
//一旦返回余额,退出循环
break;
} else if (3 - i == 0) {
//三次输入错误提示用户
System.out.println("抱歉,您三次输入错误,请取卡!");
} else {
//输入错误,提示用户继续输入
System.out.println("密码错误,请重新输入,当前第" + i + "次");
}
}
}
}


简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.mjc;

import java.util.Scanner;

public class QuessMachine {
//第十二章简答题5(第243页)
int score = (int) (Math.random() * 10000) % (9999 - 1000 + 1) + 1000;

public void initial() {
String[] m = {"公主电动车", "芒果小酪", "大萝卜", "蔓越莓饮品"};

System.out.println("请猜测:" + m[(int) (Math.random() * m.length)]);

}

public void guess() {
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 4; i++) {
System.out.print("请输入商品价格:");
int num = sc.nextInt();
if (score > num && 4 - i != 0) {
System.out.println("再大些!\n");
} else if (score < num && 4 - i != 0) {
System.out.println("再小点!\n");
} else if (4 - i == 0) {
System.out.println("4次内没有猜对,下次努力吧!");
} else {
System.out.println("恭喜你,猜对了");
break;
}
}
}

public static void main(String[] args) {
QuessMachine gj = new QuessMachine();
gj.initial();
gj.guess();
}
}

第十三章-人机猜拳

用户类–Person

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.mjc;

import java.util.Scanner;

public class Person {
String name; //用户名
int score; //积分

public int showFist() {
Scanner sc = new Scanner(System.in);
System.out.print("请出拳:1剪刀,2.石头,3.布(请输入相应数字):");
int num = sc.nextInt();
switch (num) {
case 1:
System.out.println("你出拳:剪刀 ");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳,布");
break;
default:
System.out.println("输入有误!");
break;
}
return num;
}
}

计算机类–Computer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.mjc;

public class Computer {
String name;
int score;
public int showFist() {
int num = (int) (Math.random() * 3) + 1;
switch (num) {
case 1:
System.out.println(name + "出拳:剪刀 ");
break;
case 2:
System.out.println(name + "出拳:石头");
break;
case 3:
System.out.println(name + "出拳,布");
break;
default:
System.out.println("输入有误!");
break;
}
return num;
}
}

游戏类–Game

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.mjc;

import java.util.Scanner;

public class Game {
Person person = new Person();
Computer computer = new Computer();
int count; //对战次数

void startGame() {
Scanner in = new Scanner(System.in);

System.out.println("-------------------欢迎进入游戏世界------------------------");
System.out.println("\n\t\t*****************");
System.out.println("\t\t** 猜拳,开始 **");
System.out.println("\t\t*****************\n");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");
int num = in.nextInt();
while (true) {
switch (num) {
case 1:
computer.name = "刘备";
break;
case 2:
computer.name = "孙权";
break;
case 3:
computer.name = "曹操";
break;
}
if (num <= 3 && num > 0) {
break;
}
System.out.print("输入错误,请重新输入(1:刘备 2:孙权 3:曹操):");
num = in.nextInt();
}
System.out.print("请输入姓名:");
person.name = in.next();
System.out.println(person.name + "\tVs\t" + computer.name + "对战");
System.out.print("\n要开始吗?(y/n):");
String con = in.next();
int perFist;
int compFist;
while (con.equals("y")) {
perFist = person.showFist();
if (perFist > 0 && perFist < 4) {
compFist = computer.showFist();
count++;
if ((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)) {
System.out.println("结果:和局,真衰!\n");
} else if ((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)) {
System.out.println("结果:恭喜,你赢了!\n");
person.score++;
} else {
System.out.println("结果:^_^,你输了,真笨!\n");
computer.score++;
}
}
System.out.print("\n继续吗(y/n):");
con = in.next();
}
}

void showResult() {
System.out.println("--------------------------------------------");
System.out.println(computer.name + "\tVS\t" + person.name);
System.out.println("对战次数:" + count);
System.out.println("姓名\t\t得分");
System.out.println(person.name + "\t\t" + person.score);
System.out.println(computer.name + "\t\t" + computer.score);
if (person.score > computer.score) {
System.out.println("恭喜,你赢了");
} else if (person.score == computer.score) {
System.out.println("平局");
} else {
System.out.println("抱歉,你输了");
}
System.out.println("--------------------------------------------");
}

}

测试游戏(调用)

1
2
3
4
5
6
7
8
9
10
package com.mjc;

public class Test {
public static void main(String[] args) {
Game sv = new Game();
sv.startGame();
sv.showResult();
}
}

上述代码仅仅满足题目要求,如需优化请自行解决~

第十四章-带参数的方法

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.mjc;

import java.util.Scanner;

public class CustomerBiz {
String[] names = new String[10];
public void addName(String name) {
for (int i = 0; i < names.length; i++) {
if (names[i] == null) {
names[i] = name;
break;
}
}
}

public void showName() {
for (String name : names) {
System.out.print(name + "\t");
}
}
}

class TestCustomer {
public static void main(String[] args) {
//第十四章上机练习1(第262页)
CustomerBiz ct = new CustomerBiz();
Scanner sc = new Scanner(System.in);
boolean con = true;
while (con) {
System.out.print("请输入客户姓名:");
String newName = sc.next();
//调用addName方法
ct.addName(newName);
System.out.print("继续输入吗?(y/n):");
String choice = sc.next();
if ("n".equals(choice)) {
con = false;
}
}
System.out.println("------------------------");
System.out.println("\t客户信息列表");
System.out.println("------------------------");
ct.showName();
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.mjc;

import java.util.Scanner;

public class User {
String[] names = new String[5];
public boolean editName(String oldName, String newName){
boolean find = false;
for (int i = 0; i < names.length; i++) {
if (oldName.equals(names[i])) {
names[i] = newName;
find = true;
break;
}
}
return find;
}
public static void main(String[] args) {
//第十四章上机练习2(第263页)
Scanner sc = new Scanner(System.in);
User d = new User();

for (int i = 0; i < d.names.length; i++) {
System.out.print("请输入客户姓名:");
d.names[i] = sc.next();
}
System.out.println("**********************************");
System.out.println("\t客户姓名列表");
System.out.println("**********************************");
for (int i = 0; i < d.names.length; i++) {
System.out.print(d.names[i] + "\t");
}
System.out.println();
System.out.print("请输入要修改的客户名字:");
String oldName = sc.next();
System.out.print("请输入新的的客户名字:");
String newName = sc.next();
System.out.println();

boolean flag = d.editName(oldName, newName);
if (flag) {
System.out.println("*******修改结果********");
System.out.println("\t找到并修改成功!");
System.out.println("**********************************");
} else {
System.out.println("*******修改结果********");
System.out.println("\t没有此元素,无法修改!");
System.out.println("**********************************");
}

System.out.println("**********************************");
System.out.println("\t客户列表");
System.out.println("**********************************");
for (int i = 0; i < d.names.length; i++) {
System.out.print(d.names[i] + "\t");
}
}
}


上机练习3

数组排序类

1
2
3
4
5
6
7
8
9
10
11
package com.mjc;

import java.util.Arrays;

public class StudentsBiz {
//数组排序
public void sortNames(String[] names) {
Arrays.sort(names); //升序
}
}

主类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.mjc;

public class TestSort {
public static void main(String[] args) {
//第十四章上机练习3(第268页)
StudentsBiz st = new StudentsBiz(); //创建对象
//定义数组并赋值
String[] namesbysort = new String[]{"Tom", "Jack", "Merry", "Smith", "Sunny"};
System.out.println("****排序前****");
//打印排序前
for (int i = 0; i < namesbysort.length; i++) {
if (namesbysort[i] != null) {
//判断数组中元素是否为null,如果不为null则打印数组每一位元素
System.out.print(namesbysort[i] + "\t");
}
}
//调用数组排序方法,传入一个参数
st.sortNames(namesbysort);
System.out.println("\n****排序后****");
for (int i = 0; i < namesbysort.length; i++) {
if (namesbysort[i] != null) {
System.out.print(namesbysort[i] + "\t");
}
}
}
}

上机练习4

答案略

上机练习5

模拟用户存取款类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.mjc;

import java.util.Scanner;

public class Account {
double money; //存款
double qmoney; //取钱
Scanner sc = new Scanner(System.in);

//模拟用户存款
public void cmoney() {

System.out.print("请输入存款金额:");
money += sc.nextDouble(); //存款加用户输入的金额
System.out.println("存款成功!");

System.out.println("\n***当前余额为:" + money + "元***\n");
}

//模拟用户取款
public void qmoney() {
System.out.print("请输入取款金额:");
qmoney = sc.nextDouble();

//如果取款金额小于等于存款
if (qmoney <= money) {
System.out.println("取款成功:");
money -= qmoney; //存款金额-取款金额
System.out.println("\n***当前余额为:" + money + "***\n");
} else { //否则没有存款,取款失败
System.out.println("余额不足\n");
}
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.mjc;

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
//第十四章上机练习5(第266页)
Account ac = new Account(); //创建对象
Scanner sc = new Scanner(System.in);
boolean flag = true; //布尔变量,判断循环何时终止
while (flag) { //初值为true,一直循环
System.out.println("1.存款 2.取款 0.退出");
System.out.print("请选择你要办理的业务:");
int num = sc.nextInt();
switch (num) { //等值判断,首选switch
case 1:
ac.cmoney(); //调用存款方法
break;
case 2:
ac.qmoney(); //调用取款方法
break;
default:
System.out.println("\n谢谢使用");
flag = false; //flag为false,循环终止
break;
}
}
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.mjc;

import java.util.Scanner;

public class Jd2 {
double sum;
public double ope(int op, double num1, double num2) {
switch (op) {
case 1:
sum = num1 + num2;
break;
case 2:
sum = num1 - num2;
break;
case 3:
sum = num1 * num2;
break;
case 4:
if (num2 == 0) {
sum = 0;
} else {
sum = num1 / num2;
}
break;
default:
System.out.println("请输入正确选择!");
break;
}
return sum;
}
public static void main(String[] args){
//第十四章简答题2(第279页)
Scanner sc = new Scanner(System.in);
Jd2 jd = new Jd2();
System.out.print("请选择运算方式 1加法 2减法 3乘法 4除法:");
int op = sc.nextInt();
System.out.print("请输入第一位数字:");
double num1 = sc.nextInt();
System.out.print("请输入第二位数字:");
double num2 = sc.nextDouble();
System.out.println("\n计算结果:" + jd.ope(op, num1, num2));
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.mjc;

import java.util.Scanner;

public class Jd3 {
double money; //月供
//实现返回运算结果
//loan:贷款金额 yearchoice:贷款年限
double loan(double loan, int yearchoice) {
switch (yearchoice) {
case 1:
//总利息=贷款金额*利率
//月供=(贷款金额+总利息)/月份
money = (loan + loan * 0.0603) / 36;
break;
case 2:
money = (loan + loan * 0.0612) / 60;
break;
case 3:
money = (loan + loan * 0.0639) / 240;
break;
default:
System.out.println("输入有误!");
break;
}
System.out.println("月供为:" + money);
return money; //计算后返回月供
}

public static void main(String[] args) {
//第十四章简答题3(第279页)
Jd3 jd = new Jd3(); //创建对象
Scanner sc = new Scanner(System.in);
System.out.print("请输入贷款金额:");
double money = sc.nextDouble(); //用户输入贷款金额
System.out.print("请选择贷款年限:1.3年(36个月)2.5年(60个月)3.20年(240个月):");
int year = sc.nextInt(); //用户输入贷款年限
jd.loan(money, year); //调用loan方法返回远算结果
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.mjc;

import java.util.Scanner;

public class Jd4 {
public void printTriangle(int row, String ch) {
for(int i = 0; i < row; i++){
for (int j = 1; j <= i+1; j++){
System.out.print(ch);
}
System.out.println();
}
}

public static void main(String[] args) {
//第十四章简答题4(第280页)
Jd4 jd = new Jd4();
Scanner input = new Scanner(System.in);
System.out.print("请输入行高:");
int row = input.nextInt();
System.out.print("请输入打印的字符:");
String ch = input.next();
jd.printTriangle(row,ch);
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.mjc;

import java.util.Scanner;

public class Jd5 {
//判断是否构成三角形
public boolean isTriangle(int a, int b, int c) {
boolean flag = true;
if (a + b > c && a + c > b && b + c > a) {
} else {
System.out.println("这不能构成三角形!");
flag = false;
}
return flag;
}

//判断构成何种三角形
public String shepe(int a, int b, int c) {
String shape = "";
if (a * a == b * b + c * c || b * b == a * a + c * c || c * c == a * a + b * b) {
shape = "这是一个直角三角形!";
} else if (a * a > b * b + c * c || b * b > a * a + c * c || c * c > a * a + b * b) {
shape = "这是一个钝角三角形!";
} else if (a == c && b == c) {
shape = "这是一个等边三角形!";
} else {
shape = "这是一个锐角三角形!";
}
System.out.println(shape);
return shape;
}
}

class TestJd5 {
public static void main(String[] args) {
//第十四章简答题5(第280页)
Scanner scanner = new Scanner(System.in);
String con;
Jd5 jd = new Jd5();
while (true) {
System.out.print("请输入第一条边:");
int a = scanner.nextInt();
System.out.print("请输入第二条边:");
int b = scanner.nextInt();
System.out.print("请输入第三条边:");
int c = scanner.nextInt();
if (jd.isTriangle(a, b, c)) {
jd.shepe(a, b, c);
}
System.out.print("继续吗?(y/n):");
con = scanner.next();
if ("n".equals(con)) {
break;
}
}
System.out.println("谢谢使用,系统瑞出!");
}
}

简答题6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.mjc;

import java.util.Scanner;

public class Jd6 {
public void insertArray(int[] arr, int index, int vul) {
for (int i = arr.length - 1; i >=index; i--) {
arr[i] = arr[i - 1];
}
arr[index-1] = vul;
}
}


class TestJd6 {
public static void main(String[] args) {
//第十四章简答题6(第281页)
Jd6 jd = new Jd6();
int[] nums = new int[]{2, 5, 3, 5, 9};
Scanner in = new Scanner (System.in);
System.out.println("插入前:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + "\t");
}
System.out.print("\n请输入要插入的位置:");
int index = in.nextInt();
System.out.print("请输入要插入的整数:");
int number = in.nextInt();
jd.insertArray(nums, index, number);
System.out.println("\n插入后");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + "\t");
}
}
}

简答题7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.mjc;

import java.util.Scanner;

public class Jd7 {
int java; //java成绩
int c; //C#成绩
int sql; //SQL成绩

//计算平均成绩
public double getAvg(int java, int c, int sql) {
return (java + c + sql) / 3.0; //返回平均成绩
}

public static void main(String[] args) {
//第十四章简答题7(第281页)
Scanner input = new Scanner(System.in);
Jd7 jd = new Jd7(); //创建对象

System.out.print("请输入java成绩:");
int java = input.nextInt();
System.out.print("请输入C#成绩:");
int c = input.nextInt();
System.out.print("请输入第SQL的成绩:");
int sql = input.nextInt();
//调用方法
System.out.println("平均成绩是:" + jd.getAvg(java, c, sql));
}
}

第十五章-字符串

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.mjc;

import java.util.Scanner;

public class Register {
public boolean verify(String name, String pwd1, String pwd2) {
boolean flag = false;
if (name.length() >= 3 && pwd1.length() >= 6) {
if (pwd1.equals(pwd2)) {
flag = true;
System.out.println("注册成功,请牢记您的用户名和密码");
} else {
System.out.println("两次输入的密码不相同");
}
} else {
System.out.println("用户名长度不能小于3,密码长度不能小于6!");
}
return flag;
}

public static void main(String[] args) {
//第十五章上机练习1(第290页)
Scanner sc = new Scanner(System.in);
Register r = new Register();
do {
System.out.println("---欢迎进入注册系统---\n");
System.out.print("请输入用户名:");
String name = sc.next();
System.out.print("请输入密码:");
String password = sc.next();
System.out.print("请再次输入密码:");
String password2 = sc.next();
boolean flag = r.verify(name, password, password2);
if (flag) {
break;
}
} while (true);
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.mjc;

import java.util.Scanner;

public class Lx2 {
boolean idRight(String card, String number, String phone) {
boolean flag = false;
String[] ph = phone.split("-");

if (card.length() == 16 || card.length() == 18) {
if (number.length() == 11){
if (ph[0].length() == 4 && ph[1].length() == 7) {
flag = true;
System.out.println("注册成功!");
} else {
System.out.println("座机号码区号必须为4位,电话号码必须是7位!");
}
} else {
System.out.println("手机号码必须是11位!");
}
} else {
System.out.println("身份证号码必须是16位或18位!");
}
return flag;
}

public static void main(String[] args) {
//第十五章上机练习2(第297页)
Scanner sc = new Scanner(System.in);
Lx2 r = new Lx2();
System.out.println("---欢迎进入注册系统---\n");
do {
System.out.print("请输入身份证:");
String card = sc.next();

System.out.print("请输入手机号:");
String number = sc.next();

System.out.print("请输入座机号:");
String phone = sc.next();

boolean flag = r.idRight(card, number, phone);

if (flag) {
break;
}
} while (true);
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.mjc;

import java.util.Scanner;

public class Lx3 {
public void counter(String input, String word) {
int count = 0;

for (int i = 0; i <= input.length() - 1; i++) {
String res = input.substring(i, i + 1);
if (res.equals(word)) {
count++;
}
}
System.out.print("\n”" + input + "“" + "中包含" + count + "个" + word + "。");

}

public static void main(String[] args) {
//第十五章上机练习3(第298页)
Scanner sc = new Scanner(System.in);
Lx3 c = new Lx3();
System.out.print("请输入一个字符串:");
String input = sc.next();
System.out.print("请输入要查找的字符:");
String word = sc.next();
c.counter(input, word);
}
}

上机练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.mjc;

import java.util.Scanner;

public class Goods {
boolean con = true;
double money;
String[] goods = new String[] { "电风扇", "洗衣机", "电视机", "冰箱", "空调" };
double[] price = new double[] { 124.23, 4500, 8800.90, 5000.88, 4456, 12000.46};

public void login() {
Scanner sc = new Scanner(System.in);
while(con) {
System.out.print("请输入用户名:");
String userName = sc.next();
System.out.print("请输入密码:");
String password = sc.next();
if (userName.equals("xc") && password.equals("123")) {
System.out.println("登陆成功!");
con = false;
} else {
System.out.println("登陆失败");
}
}
}
public void show() {
System.out.println("**********************欢迎进入商品批发城**********************");
System.out.println("\t编号\t\t商品\t\t价格\t");
for (int i = 0; i < goods.length; i++) {
System.out.print("\t" + (i + 1) + "\t\t");
System.out.print(goods[i] + "\t\t");
System.out.print(change(price[i]) + "\t");
System.out.println();
}
System.out.println("*********************************************************");
}
public StringBuffer change(double a) {
StringBuffer str = new StringBuffer(String.valueOf(a));
for (int i = str.indexOf(".") - 3; i > 0; i = i - 3) {
str.insert(i, ",");
}
return str;
}

public static void main(String[] args) {
//第十五章上机练习4(第301页)
Goods fx = new Goods();
fx.login();
fx.show();
System.out.print("请输入您批发的商品编号:");
Scanner sc = new Scanner(System.in);
int price = sc.nextInt();
System.out.print("请输入批发数量:");
int number = sc.nextInt();
switch (price) {
case 1:
fx.money = 124.23 * number;
break;
case 2:
fx.money = 4500 * number;
break;
case 3:
fx.money = 8800.9 * number;
break;
case 4:
fx.money = 5000.88 * number;
break;
case 5:
fx.money = 4456 * number;
break;
default:
System.out.println("输入有误!");
break;
}
System.out.println("您需要付款为:" + fx.change(fx.money));
}
}

简答题2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.mjc;

import java.util.Arrays;
import java.util.Scanner;

public class Jd2 {
String[] names = new String[5];
public void addName(String name) {
for (int i = 0; i < names.length; i++) {
if (names[i] == null) {
names[i] = name;
break;
}
}
}

public static void main(String[] args) {
//第十五章简答题2(第305页)
Jd2 jd = new Jd2();
Scanner sc = new Scanner(System.in);
int i = 0;
do {
System.out.print("请输入第" + (i + 1) + "种水果:");
String newName = sc.next();
jd.addName(newName);
i++;
} while (i != 5);

Arrays.sort(jd.names);

System.out.println("这些水果在词典中出现的顺序是:");
for (int j = 0; j < jd.names.length; j++) {
System.out.print(jd.names[j] + "\n");
}

}
}


简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.mjc;

import java.util.Scanner;

public class Jd3 {
public static void user(String name) {
System.out.println("姓氏:" + name.charAt(0));
System.out.println("名字:" + name.substring(1));
}

public static void main(String[] args) {
//第十五章简答题3(第305页)
Scanner sc = new Scanner(System.in);
System.out.print("请输入任意一个姓名:");
String con = sc.next();
user(con);
}
}

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.mjc;

import java.util.Scanner;

public class Jd4 {
boolean idUser(String id) {
boolean flag = false;
if (id.length()==18) {
System.out.println("生日为:" + id.substring(6, 10) + "年" + id.substring(10, 12) + "月" + id.substring(12, 14) + "日");
flag = true;
} else {
System.out.println("输入的格式有误,请重新输入!\n");
}
return flag;
}
public static void main(String[] args) {
//第十五章简答题4(第305页)
Scanner sc = new Scanner(System.in);
Jd4 jd = new Jd4();
while (true) {
System.out.print("请输入用户的身份证号码:");
String id = sc.next();

boolean flag = jd.idUser(id);

if (flag) {
break;
}
}
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.mjc;

import java.util.Scanner;

public class Jd5 {

public void grep(String in, String of) {

for (int i = 0; i <= in.length() - 1; i++) {
if (in.substring(i, i + 1).equals(of)) {
System.out.println(i + "\t");
}
}
}

public static void main(String[] args) {
//第十五章简答题5(第305页)
Scanner sc = new Scanner(System.in);
Jd5 jd = new Jd5();
System.out.println("请输入一段字符:");
String in = sc.next();
System.out.println("请输入要查询的字符串:");
String of = sc.next();
System.out.print(of + "出现的位置是:");
jd.grep(in, of);

}
}

简答题6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.mjc;

import java.util.Scanner;

public class Jd6 {
public boolean isDateOfRight(String date) {
boolean flag = false;
if (date.indexOf('/') != 2) {
System.out.println("生日形式输入错误!\n");
} else {
flag = true;
System.out.println("该会员生日是:" + date + "\n");
}
return flag;
}

public boolean isPwdOfRight(String password) {
boolean flag = false;
if (password.length() < 6 || password.length() > 10) {
System.out.println("密码形式输入错误!\n");
} else {
flag = true;
System.out.println("该会员密码是:" + password + "\n");
}
return flag;
}

public static void main(String[] args) {
//第十五章简答题6(第306页)
Scanner sc = new Scanner(System.in);
Jd6 jd = new Jd6();
while (true) {
System.out.print("请输入会员生日<月/日:00/00>:");
String date = sc.next();
if (jd.isDateOfRight(date)) {
while (true) {
System.out.print("请输入会员密码<6~10位>:");
String pwd = sc.next();
if (jd.isPwdOfRight(pwd)) {
break;
}
}
break;
}
}
}
}

第十六章-课程总复习

练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package mjc;

import java.util.Scanner;

public class Lx1 {
boolean login(String name, String pwd) {
if (!"xc".equals(name) && !"123465".equals(pwd)) {
System.out.println("用户名或密码错误,请重新输入\n");
return false;
}

return true;
}

public static void main(String[] args) {
//第十六章练习1(第312页)
Lx1 c = new Lx1();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("请输入用户名:");
String name = sc.next();
System.out.print("请输入密码:");
String pwd1 = sc.next();
boolean flag = c.login(name, pwd1);
if (flag) {
System.out.println("登陆成功!");
break;
}
}
}
}

练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package mjc;

import java.util.Scanner;

public class Student {
String name;
int age;
String school;
char gender;
Scanner sc = new Scanner(System.in);

public void insertStudent(Student stu) {
//第十六章练习2(第312页)
System.out.print("请输入学生姓名:");
stu.name = sc.next();

System.out.print("请输入学生年龄:");
stu.age = sc.nextInt();

System.out.print("请输入学生性别:");
stu.gender = sc.next().charAt(0);

System.out.print("请输入学生学校:");
stu.school = sc.next();

System.out.println("将该学生信息成功导入数据库");
System.out.println(stu.name + "\t" + stu.age + "\t" + stu.gender + "\t" + stu.school);
}
}

class Lx2 {
public static void main(String[] args) {
Student student = new Student();
student.insertStudent(student);
}
}

练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package mjc;

import java.util.Scanner;

public class Lx3 {

public void ff(String year, int type) {
int sj = (int) (Math.random() * (999 - 100) + 100);
if (year.length() == 2) {
if (type > 0 && type < 4) {
System.out.println("该固定的资产编号是:" + year.substring(0, 2) + "0" + type + sj);
} else {
System.out.println("产品类型输入有误!");
}
} else {
System.out.println("年份输入有误!");
}
}
public static void main(String[] args) {
//第十六章练习3(第312页)
Scanner sc = new Scanner(System.in);
Lx3 c = new Lx3();
System.out.print("请输入年份:(两位数字):");
String year = sc.next();

System.out.print("请输入产品类型:(1.台式机 2.笔记本 3.其他):");
int type = sc.nextInt();

c.ff(year, type);
}
}

练习4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package mjc;

import java.util.Scanner;

public class Lx4 {
public void day(String input) {
String[] year = input.split("/");

if (input.indexOf('/') != 2) {
System.out.println("日期形式输入有误!\n");
} else {
System.out.println(year[2] + "年" + year[0] + "月" + year[1] + "日");
}
}

public static void main(String[] args) {
//第十六章练习4(第313页)
Lx4 c = new Lx4();
Scanner sc = new Scanner(System.in);

System.out.print("请输入一个日期(月/日/年):");
String day = sc.next();
System.out.println();
c.day(day);
}
}

第十七章-吃货联盟订单系统

用例1:数据初始化

用例2:实现菜单切换

《图书管理系统——参考代码》

题目

一、语言和环境

  1. 语言:Java

  2. 环境:使用Eclipse/MyEclipse

  3. 要求:按要求编写Java控制台应用程序。

二、功能需求

1)需求描述:使用java编写一个图书信息管理系统,保存3本图书信息;然后使用自定义方法完成显示图书列表和显示最高单价图书信息两个功能。

2)推荐步骤:

步骤1:定义图书Book类:

图书Book类属性

  • 书名:字符串类型

  • 单价:双精度浮点型

  • 作者:字符串类型

  • 借阅状态:整形

步骤2:在程序入口main方法中,定义一个用于保存图书信息的对象数组。

​ 对象[] 数组名 = new 对象[长度];

步骤3:在程序入口main方法中,利用循环逐个为图书数组的每个元素赋值。

步骤4:编写方法实现输出所有图书信息列表,自定义有参无返回值的方法,参数为需要输出的图书对象数组,在方法中遍历数组循环输出数组中图书的信息

步骤5:编写方法实现输出单价最高图书的详细信息,自定义有参无返回值的方法,参数为图书对象数组,在方法中遍历数组循环找到单价最高的图书并输出其详细信息

步骤6:在程序入口Main方法中,调用步骤4和步骤5的自定义方法实现输出所有图书信息列表和输出单价最高图书的详细信息。

显示效果参考:

样例

三、注意事项

  1. 必须使用方法返回对象数组

  2. 注意程序逻辑分明、命名规范以及书写有缩进。

  3. 添加适当的注释。

四、评分标准

题目:
该程序的评分标准如下:
15正确定义类及其成员
10正确定义对象数组
20正确对对象数组的元素的成员进行赋值
15定义输出所有图书信息列表方法
15正确定义输出单价最高图书的详细信息方法
10正确调用方法并输出结果
5整体功能全部实现
10总体编程技术
3 命名符合规范
3 关键步骤有注释
2 编码书写有缩进
2 能够编译运行
总分100分

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Scanner;

public class Book {
//1)需求描述:使用java编写一个图书信息管理系统,保存3本图书信息;然后使用自定义方法完成显示图书列表和显示最高单价图书信息两个功能。
//2)推荐步骤:
//步骤1:定义图书Book类:
String bookName; //书名:字符串类型

double price; //单价:双精度浮点型

String author; //作者:字符串类型

int state; //借阅状态:整形


public static void main(String[] args) {
//步骤2:在程序入口main方法中,定义一个用于保存图书信息的对象数组。
//对象[] 数组名 = new 对象[长度];
Book[] bk = new Book[3];
//步骤3:在程序入口main方法中,利用循环逐个为图书数组的每个元素赋值。
Scanner sc = new Scanner(System.in);

Book books = new Book();

for (int i = 0; i < bk.length; i++) {
bk[i] = new Book();
System.out.println("请输入第" + (i + 1) + "本图书信息:");

System.out.print("书名:");
bk[i].bookName = sc.next();

System.out.print("单价:");
bk[i].price = sc.nextDouble();

System.out.print("作者:");
bk[i].author = sc.next();

System.out.print("借阅状态(0-已借阅,1-已归还):");
bk[i].state = sc.nextInt();
System.out.println();
}
//步骤6:在程序入口Main方法中,调用步骤4和步骤5的自定义方法实现输出所有图书信息列表和输出单价最高图书的详细信息。
books.show(bk);
books.showMaxPrice(bk);
}

//步骤4:编写方法实现输出所有图书信息列表,自定义有参无返回值的方法,参数为需要输出的图书对象数组,在方法中遍历数组循环输出数组中图书的信息
void show(Book[] bk) {
System.out.println("序号\t名称\t单价\t作者\t借阅状态");
for (int i = 0; i < bk.length; i++) {
String jy = bk[i].state > 0 ?"已归还":"已借阅";
System.out.println((i+1) + "\t" + bk[i].bookName + "\t" + bk[i].price + "\t" + bk[i].author + "\t" + jy);
}
}
//步骤5:编写方法实现输出单价最高图书的详细信息,自定义有参无返回值的方法,参数为图书对象数组,在方法中遍历数组循环找到单价最高的图书并输出其详细信息
void showMaxPrice(Book[] bk) {
//假设数组中单价最高的是第一位
double max = bk[0].price;
int index = 0;
System.out.println("\n最高单价图书信息如下:");
for (int i = 0; i < bk.length; i++) {
//找到了
if (bk[i].price > max) {
index = i;
}
}
String jy = bk[index].state > 0 ?"已归还":"已借阅";
System.out.println("名称:" + bk[index].bookName);
System.out.println("单价:" + bk[index].price);
System.out.println("作者:" + bk[index].author);
System.out.println("状态:" + jy);
}

}

财务报销管理系统

题目

一、语言和环境

  1. 语言:Java

  2. 环境:使用Eclipse/MyEclipse

  3. 要求:按要求编写Java控制台应用程序。

二、功能需求

1)需求描述:使用java编写一个财务报销管理系统,保存3个人的报销信息;要求:

​ A、使用自定义方法完成显示报销信息列表

​ B、使用自定义方法显示报销金额最低的人报销单据信息。

​ C、使用自定义方法将报销数字的状态翻译成文字的报销状态。

2)推荐步骤:

步骤1:定义报销单据实体类,添加以下属性:报销人、报销金额、报销说明、报销状态。

其中报销状态如下:

  • 主管审批 = 1,

  • 总监审批 = 2,

  • 财务审批 = 3,

  • 已报销 = 4

步骤2:在程序入口main方法中,定义一个用于保存报销信息的对象数组。

​ 类[] 数组名 = new 类[长度];

步骤3:在程序入口main中,利用循环逐个为报销数组的每个元素赋值。

步骤4:编写方法实现输出所有报销信息列表,自定义有参无返回值的方法,参数为需要输出的报销单据对象数组,在方法中遍历数组循环输出数组中的报销单据的信息

步骤5:编写方法实现输出报销金额最低的人的报销信息,自定义有参无返回值的方法,参数为需要输出的报销单据对象数组,在方法中遍历数组循环输出数组中的报销单据的信息

步骤6:编写方法实现根据状态码返回对应的文字版状态说明,自定义有参有返回值的方法,参数为整型的报销状态码,在方法中根据情况返回对应的文字版状态说明

步骤7:在程序入口main方法中,调用步骤4和步骤5的自定义方法实现输出所有报销信息列表和输出报销金额最低的人的报销详细信息。

显示效果参考:

图片1

四、评分标准

题目:
该程序的评分标准如下:
10正确定义类及其成员
10正确定义对象型数组
20正确对结构体数组的元素的成员进行赋值
15定义输出所有报销信息列表方法
15正确定义输出报销金额最低的人报销详细信息方法
10正确定义返回文字版报销状态的方法
5正确调用各方法并输出对应结果
5整体功能全部实现
10总体编程技术
3命名符合规范
3关键步骤有注释
2编码书写有缩进
2能够编译运行
总分100分

代码

财务报销管理类

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.mjc;

public class Cw {
//步骤1:定义报销单据实体类,添加以下属性:报销人、报销金额、报销说明、报销状态。
String name; //报销人

double money; //报销金额

String text; //报销说明

int state; //报销状态
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.mjc;

import java.util.Scanner;

public class TestCw {

public static void main(String[] args) {
//步骤2:在程序入口main方法中,定义一个用于保存报销信息的对象数组。
//类[] 数组名 = new 类[长度];
Cw[] c = new Cw[3];
TestCw tc = new TestCw();

Scanner sc = new Scanner(System.in);

//步骤3:在程序入口main中,利用循环逐个为报销数组的每个元素赋值。
System.out.println("================财务报销管理系统================");
for (int i = 0; i < c.length; i++) {
c[i] = new Cw();
System.out.println("请输入第" + (i + 1) + "个员工的报销信息:");

System.out.print("报销人:");
c[i].name = sc.next();

System.out.print("报销金额:");
c[i].money = sc.nextDouble();

System.out.print("报销说明:");
c[i].text = sc.next();

System.out.print("报销状态(1-主管审批,2-总监审批,3.财务审批,4已报销):");
c[i].state = sc.nextInt();
System.out.println();
// 主管审批 = 1,
// 总监审批 = 2,
// 财务审批 = 3,
// 已报销 = 4
}
//步骤7:在程序入口main方法中,调用步骤4和步骤5的自定义方法
//实现输出所有报销信息列表和输出报销金额最低的人的报销详细信息。
tc.show(c);
tc.showMinPrice(c);
}

//步骤4:编写方法实现输出所有报销信息列表,自定义有参无返回值的方法,
//参数为需要输出的报销单据对象数组,在方法中遍历数组循环输出数组中的报销单据的信息
void show(Cw[] c) {
System.out.println("===========================================");
System.out.println("报销人\t金额\t报销状态\t说明");
for (int i = 0; i < c.length; i++) {
System.out.println(c[i].name + "\t" + c[i].money + "\t" + nameId(c[i].state) + "\t" + c[i].text);
}
System.out.print("===========================================");
}

//步骤5:编写方法实现输出报销金额最低的人的报销信息,自定义有参无返回值的方法,
//参数为需要输出的报销单据对象数组,在方法中遍历数组循环输出数组中的报销单据的信息
void showMinPrice(Cw[] c) {
//假设数组中单价最低的是第一位
double min = c[0].money;
int index = 0;
System.out.println("\n报销金额最低的人的报销信息如下:");
for (int i = 0; i < c.length; i++) {
if (min > c[i].money) {
index = i;
}
}
System.out.println("报销人:" + c[index].name);
System.out.println("报销金额:" + c[index].money);
System.out.println("报销说明:" + c[index].text);
System.out.println("报销状态:" + nameId(c[index].state));
System.out.println("========================================");
}

//步骤6:编写方法实现根据状态码返回对应的文字版状态说明,自定义有参有返回值的方法,
//参数为整型的报销状态码,在方法中根据情况返回对应的文字版状态说明
String nameId(int ztm) {
String zt = "";
switch (ztm) {
case 1:
zt = "主管审批";
break;
case 2:
zt = "总监审批";
break;
case 3:
zt = "财务审批";
break;
case 4:
zt = "已报销";
break;
}
return zt;
}
}