banner
NEWS LETTER

Java语言实例

Scroll down

猜数字小游戏

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.demo;

import java.util.Scanner;

public class Test {
public static void main(String[] args){
//获得1至100的随机数
int number = (int)(Math.random()*100+1);
//输出要求
System.out.println("给你一个1至100之间的整数,请猜测这个数");
System.out.print("输入您的猜测:");
Scanner input = new Scanner(System.in);
//循环判断
boolean x = false;
do {
//获得用户数据
int guess = input.nextInt();
//判断数据大小
if (guess > number) {
System.out.print("猜大了,再输入你的猜测:");
} else if (guess < number) {
System.out.print("猜小了,再输入你的猜测:");
} else {
System.out.print("猜对了!");
x = true;
}
}while(!x);
}
}

评定学生成绩等级

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.demo;

import java.util.Scanner;

public class Test3 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个分数:");
double score = input.nextDouble();
if (score >= 90 && score <= 100) {
System.out.println("优秀");
} else if (score >= 80 && score < 90) {
System.out.println("良好");
} else if (score >= 70 && score < 80) {
System.out.println("中等");
} else if (score >= 60 && score < 70) {
System.out.println("及格");
} else if (score >= 0 && score < 60) {
System.out.println("不及格");
}else {
System.out.println("超出范围");
}
}
}

读取用户输入的数,并判断结果

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

import java.util.Scanner;

public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入x:");
double x = input.nextDouble();
if (x<-2){
System.out.println("y=-2");
} else if (x>=-2&&x<2){
System.out.print("y=");
System.out.print(x+3);
} else if (x>=2) {
System.out.println("y=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
package com.demo;

import java.util.Scanner;

public class Test {
public static void main(String args[]){
boolean bool = true;
//循环判断结果
while(bool) {
//获取用户数据
Scanner input = new Scanner(System.in);
System.out.println("请输入第一个数:");
double input1 = input.nextDouble();
System.out.println("请输入第二个数:");
double input2 = input.nextDouble();
System.out.println("请输入运算符(+、-、*、/):");
char Calculate = input.next().charAt(0);
double output = 0;
//计算用户数据
switch(Calculate){
case '+':
output = input1 + input2;
break;
case '-':
output = input1 - input2;
break;
case '*':
output = input1 * input2;
break;
case '/':
output = input1 / input2;
break;
default:
System.out.println("请输入正确的符号");
}
//输出数据
System.out.println("结果是:" + input1 + Calculate + input2 + "=" + output);
//判断是否结束循环
System.out.println("请问要退出吗?y/n");
char result = input.next().charAt(0);
if(result == 'y') {
System.out.println("程序结束!");
bool = false;
}else if(result == 'n'){
bool = true;
}
}
}
}

银行管理

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.demo;

import java.util.*;
//银行账户类
class Cust {
//创建姓名,账号,密码,余额,流水号,开户总数,银行名称变量
private String name;
private int id;
private String pwd;
private int balance;
int selfNum;
static String bankName = "建设银行";
static int allNum = 0;
public Cust(){}//无参构造函数定义

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

public int getBalance() {
return balance;
}

public void setBalance(int balance) {
this.balance = balance;
}

public Cust(String name, int id, String pwd, int balance) {
this.name = name;
this.id = id;
this.pwd = pwd;
this.balance = balance;
allNum++;
selfNum = allNum;
}//构造成员

public void getMoney(int price) {
if (price > 0) {
if (price <= balance) {
this.balance -= price;
System.out.println("成功取款" + price + "元\n");
} else {
System.out.println("余额不足!");
}
} else {
System.out.println("金额非法");
}
}//取钱

public void saveMoney(int price) {
if (price > 0) {
this.balance += price;
System.out.println("成功存款" + price + "元\n");
} else {
System.out.println("金额非法");
}
}//存钱

public void search() {
System.out.println("所属银行:" + bankName);
System.out.println("用户姓名:" + name);
System.out.println("用户账号:" + id);
System.out.println("账户余额:" + balance);
System.out.println("您是本银行" + allNum + "个顾客中的第" + selfNum + "个顾客\n");
}//输出

public void changePWD() {
Scanner scanner = new Scanner(System.in);
System.out.println("请从键盘上键入新密码");
String password = scanner.nextLine();
if (password.equals(pwd)) {
System.out.println("输入的密码不能与原密码相同\n");
} else {
this.pwd = password;
System.out.println("修改成功\n");
}
}//修改密码

public static double sum(Cust... c) {
double sum = 0;
for (int i = 0; i < allNum; i++) {
sum += c[i].balance;
}
return sum;
}//求和

public static void sort(Cust... c) {
Cust temp;
for (int i = 0; i < allNum - 1; i++) {
for (int j = 0; j < allNum - i - 1; j++) {
if (c[j].balance < c[j + 1].balance) {
temp = c[j];
c[j] = c[j + 1];
c[j + 1] = temp;
}
}
}
for (int i = 0; i < allNum; i++) {
System.out.println(c[i].name + "," + c[i].id + "," + c[i].balance);
}
}//排序
}
//VIP账户类
class VIPCust extends Cust{
private static int overdraftLimit = 3000;//透支最大额度
public VIPCust(String name, int id, String pwd, int balance) {
super(name,id,pwd,balance);
}//重构成员
@Override
public void getMoney(int price) {
if(this.getBalance() +overdraftLimit>price){
super.getMoney(price);
} else {
System.out.println("余额不足,无法取款");
}
}//重构getMoney()函数
public void transfer(Cust recipient,int price)
{
if(this.getBalance()+overdraftLimit>=price){
recipient.setBalance(recipient.getBalance()+price);
this.setBalance(this.getBalance()-price);
System.out.println("成功转账"+price+"元\n");
}else {
System.out.println("余额不足,转账失败");
}
}
}
public class Test {
public static void main(String[] args) {
//预加载数据
Cust Customer1 = new Cust("李四",100,"123456",10000);
Cust Customer2 = new Cust("张三",105,"114514",9000);
Cust Customer3 = new Cust("优秀素质",101,"1919810",5000);
VIPCust VIP_Customer1 = new VIPCust("伍佰",1,"233333",50000);
VIPCust VIP_Customer2 = new VIPCust("阿陈",2,"122222",30000);
//显示数据
Customer1.search();
Customer2.search();
Customer3.search();
VIP_Customer1.search();
VIP_Customer2.search();
//测试操作
Customer1.changePWD();
Customer2.getMoney(2000);
Customer3.saveMoney(3000);
VIP_Customer1.transfer(VIP_Customer2,5000);
VIP_Customer2.transfer(Customer1,1000);
//按余额多少从上到下排序并输出
Cust.sort(Customer1,Customer2,Customer3,VIP_Customer1,VIP_Customer2);
//计算余额总量
double sum = Cust.sum(Customer1,Customer2,Customer3,VIP_Customer1,VIP_Customer2);
System.out.println("\n所有账户余额为"+sum);
}
}

学生信息

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
package com.student;

public class Student {
private int no;
private String name;
private int age;
private String address;
private static int count = 0;

public Student(int sno, String sname, int sage, String saddress) {
this.no = sno;
this.name = sname;
this.age = sage;
this.address = saddress;
count++;
}

public int getNo() {
return no;
}

public void setNo(int no) {
this.no = no;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String toString() {
return "学号:" + no + ",姓名:" + name + ",年龄:" + age + ",地址:" + address;
}

public static void main(String[] args) {
Student student1 = new Student(1, "张三", 20, "上海");
Student student2 = new Student(2, "李四", 22, "四川");
Student student3 = new Student(3, "赵六", 21, "重庆");

System.out.println("学生1的信息:" + student1.toString());
System.out.println("学生2的信息:" + student2.toString());
System.out.println("学生3的信息:" + student3.toString());
System.out.println("学生数:" + count);
}
}

分析大小写

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.analyze;
import java.util.*;
import java.lang.Character;

public class Analyze {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int upper = 0;
int lower = 0;
int digit = 0;
int space = 0;
System.out.print("请输入字符串:");
String input = scanner.nextLine();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isUpperCase(c)) {
upper++;
} else if (Character.isLowerCase(c)) {
lower++;
} else if (Character.isDigit(c)) {
digit++;
} else if (Character.isSpaceChar(c)) {
space++;
}
}
System.out.println("大写字母个数:"+upper);
System.out.println("小写字母个数:"+lower);
System.out.println("数字个数:"+digit);
System.out.println("空格个数:"+space);
}
}

其他文章