package leif.tests;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
import org.junit.Test;
public class ComprehensiveExercises {
/*
* 打印九九乘法表
*/
@Test
public void test1() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
}
/*
* 输入一个数字判断是奇数还是偶数
*/
@Test
public void test2() {
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextInt() % 2 == 0 ? "偶数" : "奇数");
scanner.close();
}
/*
* 输出1到100之间的偶数
*/
@Test
public void test3() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
/*
* 一次酒店宴席安排宾客就座吃饭,5人一桌剩4人,7人一桌剩6人,9人一桌剩8人,11人一桌正好。问宴席共最少有多少人
*/
@Test
public void test4() {
int x = 0;
while (true) {
x++;
if (x % 5 == 4 && x % 7 == 6 && x % 9 == 8 && x % 11 == 0) {
break;
}
}
System.out.println(x);
}
/*
* 求1到100之间的素数
*/
@Test
public void test5() {
loop: for (int i = 2; i <= 100; i++) {
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
continue loop;
}
}
System.out.println(i);
}
}
/*
* 求1到10之间的数的乘积之和
*/
@Test
public void test6() {
int result = 0;
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
result += i * j;
}
}
System.out.println(result);
}
/*
* 笼子里一共有鸡和兔子35只,已知一共有94条腿,求笼子里各有鸡和兔子多少只
*/
@Test
public void test7() {
for (int chicken = 1; chicken < 47; chicken++) {
int rabbit = 35 - chicken;
if (2 * chicken + 4 * rabbit == 94) {
System.out.println("鸡:" + chicken);
System.out.println("兔子:" + rabbit);
}
}
}
/*
* 键盘输入3个值并将这三个值从小到大打印出来
*/
@Test
public void test8() {
Scanner scanner = new Scanner(System.in);
int[] is = { scanner.nextInt(), scanner.nextInt(), scanner.nextInt() };
for (int x = 1; x <= is.length - 1; x++) {
for (int y = 0; y < is.length - x; y++) {
if (is[y] > is[y + 1]) {
is[y] = is[y] ^ is[y + 1];
is[y + 1] = is[y] ^ is[y + 1];
is[y] = is[y] ^ is[y + 1];
}
}
}
System.out.println(Arrays.toString(is));
scanner.close();
}
/*
* 有一分数序列:2/1、3/2、5/3、8/5、13/8、21/13……求出这个数列的前20项之和
*/
@Test
public void test9() {
double result = 0;
for (int i = 1; i <= 20; i++) {
result += function9(i, 2) / function9(i, 1);
}
System.out.println(result);
}
public static double function9(int i, int flag) {
if (flag == 1) {
if (i == 1) {
return 1;
}
if (i == 2) {
return 2;
}
}
if (flag == 2) {
if (i == 1) {
return 2;
}
if (i == 2) {
return 3;
}
}
return function9(i - 1, flag) + function9(i - 2, flag);
}
/*
* 输入一个大于6且可以拆分为两个素数的偶数
*/
@Test
public void test10() {
for (int i = 6;; i += 2) {
loop: for (int x = 2; x < i; x++) {
for (int y = 2; y <= Math.sqrt(x); y++) {
if (x % y == 0) {
continue loop;
}
}
int z = i - x;
for (int y = 2; y <= Math.sqrt(z); y++) {
if (z % y == 0) {
continue loop;
}
}
System.out.println(i + "=" + x + "+" + z);
}
}
}
/*
* 打印一个三角型
*/
@Test
public void test11() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
/*
* 从键盘输入一个年份,判断该年份是不是闰年(四年一闰,百年不闰,四百年再闰)
*/
@Test
public void test12() {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println("闰年");
} else {
System.out.println("平年");
}
scanner.close();
}
/*
* 有100个房间,每个房间里都有一盏亮着的灯,拉一下灭,再拉一下又亮,现在有一百个人排成一队,第一个人拉一下所有能把1整除的房间里的灯,
* 第二个人拉一下所有能把2整除的房间里的灯……问最后哪些灯是亮的
*/
@Test
public void test13() {
Map<Integer, Boolean> roomMap = new HashMap<Integer, Boolean>();
for (int i = 1; i <= 100; i++) {
roomMap.put(i, true);
}
for (int person = 1; person <= 100; person++) {
for (int room = 1; room <= 100; room++) {
if (person % room == 0) {
roomMap.put(room, !roomMap.get(room));
}
}
}
for (Entry<Integer, Boolean> roomEntry : roomMap.entrySet()) {
if (roomEntry.getValue()) {
System.out.println(roomEntry.toString());
}
}
}
/*
* 公司一共有100名员工和100块钱,已知门票:中国馆5元一张,美国馆3元一张,日本馆一元2张,问如何分配才能保证每名员工有且仅有一张票
*/
@Test
public void test14() {
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 34; y++) {
int z = 100 - x - y;
if (5 * x + 3 * y + 0.5 * z == 100) {
System.out.println("中国馆:" + x + ",美国馆:" + y + ",日本馆:" + z);
}
}
}
}
/*
* 团团圆圆每3个月都会生一对小熊猫,小熊猫生下来3个月之后也会生小猫熊,问24个月后有多少只熊猫
*/
@Test
public void test15() {
System.out.println(function15(24));
}
public static int function15(int i) {
if (i == 1 || i == 2) {
return 1;
}
return function15(i - 1) + function15(i - 2);
}
/*
* 求100-999之间所有的水仙花数
*/
@Test
public void test16() {
for (int i = 100; i <= 999; i++) {
if (i == Math.pow(i / 100, 3) + Math.pow(i / 10 % 10, 3) + Math.pow(i % 10, 3)) {
System.out.println(i);
}
}
}
/*
* 打印如下图形
* *
* ***
* *****
* ***
* *
*/
@Test
public void test17() {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
for (int row = 1; row <= i; row++) {
for (int col = 1; col <= Math.abs((i + 1) / 2 - row); col++) {
System.out.print(" ");
}
for (int col = 1; col <= i - 2 * (Math.abs((i + 1) / 2 - row)); col++) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
/*
* 打印如下图形
* *
* * *
* * *
* * *
* *
*/
@Test
public void test18() {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
for (int row = 1; row <= i; row++) {
for (int col = 1; col <= Math.abs((i + 1) / 2 - row); col++) {
System.out.print(" ");
}
System.out.print("*");
for (int col = 1; col <= i - 2 - 2 * Math.abs((i + 1) / 2 - row); col++) {
System.out.print(" ");
}
if (Math.abs((i + 1) / 2 - row) <= (i + 1) / 2 - 2) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
/*
* 输出1!+2!+3!+……+10!的和
*/
@Test
public void test19() {
int result = 0;
for (int i = 1; i <= 10; i++) {
result += function19(i);
}
System.out.println(result);
}
public int function19(int i) {
if (i == 1) {
return 1;
}
return i * function19(i - 1);
}
/*
* 依次输入10个学生成绩,判断学生(优秀、良好、中等、及格、不及格)并计算人数
*/
@Test
public void test20() {
Random random = new Random();
int a = 0, b = 0, c = 0, d = 0, e = 0;
int i = 1;
while (i <= 10) {
int score = random.nextInt(101);
System.out.print(score + ":");
switch (score / 10) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("不及格");
e += 1;
break;
case 6:
System.out.println("及格");
d += 1;
break;
case 7:
System.out.println("中等");
c += 1;
break;
case 8:
System.out.println("良好");
b += 1;
break;
case 9:
case 10:
System.out.println("优秀");
a += 1;
break;
default:
System.out.println("error!");
i -= 1;
break;
}
i++;
}
System.out.println("\n优秀共" + a + "人" + "\n良好共" + b + "人" + "\n中等共" + c + "人" + "\n及格共" + d + "人" + "\n不及格共" + e + "人");
}
/*
* 使用冒泡排序
*/
@Test
public void test21() {
Random random = new Random();
int[] is = new int[10];
for (int i = 0; i < is.length; i++) {
is[i] = random.nextInt(101);
}
System.out.println(Arrays.toString(is));
for (int x = 1; x <= is.length - 1; x++) {
for (int y = 0; y < is.length - x; y++) {
if (is[y] > is[y + 1]) {
is[y] = is[y] ^ is[y + 1];
is[y + 1] = is[y] ^ is[y + 1];
is[y] = is[y] ^ is[y + 1];
}
}
}
System.out.println(Arrays.toString(is));
}
/*
* 实现会员注册,要求用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相同
*/
@Test
public void test22() {
Scanner scanner = new Scanner(System.in);
String name = new String();
do {
System.out.print("请输入用户名:");
name = scanner.next();
if (name.length() < 3) {
System.out.println("要求用户名长度不小于3!");
}
} while (name.length() < 3);
String password1 = new String();
do {
System.out.print("请输入密码:");
password1 = scanner.next();
if (password1.length() < 6) {
System.out.println("要求密码长度不小于6!");
}
} while (password1.length() < 6);
String password2 = new String();
do {
System.out.print("请重新输入密码:");
password2 = scanner.next();
if (!password2.equals(password1)) {
System.out.println("两次输入的密码不一致!");
}
} while (!password2.equals(password1));
System.out.println("注册成功!");
scanner.close();
}
/*
* 一个景区根据游人的年龄收取不同价格的门票,请编写游人类,根据年龄段决定能够购买的门票价格并输出,然后写出测试类测试该类
*/
@Test
public void test23() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年龄:");
Tourist tourist = new Tourist(scanner.nextInt());
System.out.print(tourist.ticket());
scanner.close();
}
/*
* 1、编写一个圆类Circle,该类拥有:
* 1)一个成员变量
* ① r(私有,浮点型):存放圆的半径
* 2)两个构造方法
* ① Circle():将半径设为0
* ② Circle(double r):创建Circle对象时将半径初始化为r
* 3)三个成员方法
* ① double getPerimeter():获取圆的周长
* ② double getArea():获取圆的面积
* ③ void showCircle():将圆的半径、周长、面积输出到屏幕
* 2、编写一个圆柱体类Cylinder,它继承于上面的Circle类,还拥有:
* 1)一个成员变量
* ① h(私有,浮点型):存放圆柱体的高
* 2)一个构造方法
* ① Cylinder(double r, double h):创建Cylinder对象时将半径初始化为r、高初始化为h
* 3)两个成员方法
* double getVolume():获取圆柱体的体积
* void showCylinder():将圆柱体的高、体积输出到屏幕
* 3、编写应用程序,计算并分别显示圆的半径、周长、面积以及圆柱体的高、体积
*/
@Test
public void test24() {
Circle circle = new Circle(Math.random());
circle.showCircle();
Cylinder cylinder = new Cylinder(Math.random(), Math.random());
cylinder.showCylinder();
}
/*
* 编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃
*/
@Test
public void test25() {
Scanner scanner = new Scanner(System.in);
String string1 = scanner.nextLine();
String string2 = scanner.nextLine();
try {
System.out.println(function25(string1, string2));
System.out.println(function25(Integer.valueOf(string1), Integer.valueOf(string2)));
System.out.println(function25(Double.valueOf(string1), Double.valueOf(string2)));
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
scanner.close();
}
}
public static String function25(String string1, String string2) {
return string1 + string2;
}
public static int function25(int i1, int i2) {
return i1 + i2;
}
public static double function25(double f1, double f2) {
return f1 + f2;
}
/*
* 应用FileInputStream类,编写应用程序,从磁盘上读取一个Java程序,并将源程序代码显示在屏幕上
*/
@Test
public void test26() throws IOException {
FileInputStream fileInputStream = new FileInputStream(new File("D:\\Workspace\\STS\\lbc-tests\\src\\leif\\tests\\ComprehensiveExercises.java"));
byte[] bytes = new byte[20];
fileInputStream.read(bytes);
String string = new String(bytes);
System.out.println(string);
fileInputStream.close();
}
/*
* 编写一个Java程序将任意六个数以数组的形式写入到Dest.txt文件中,并以相反的顺序读出显示在屏幕上
*/
@Test
public void test27() {
int[] is = new int[6];
for (int i = 0; i < is.length; i++) {
is[i] = (int)(Math.random() * 100 + 1);
}
System.out.println(Arrays.toString(is));
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
dataOutputStream = new DataOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\Dest.txt"));
for (int i : is) {
dataOutputStream.writeInt(i);
}
dataInputStream = new DataInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\Dest.txt"));
int[] data = new int[is.length];
for (int i = 0; i < data.length; i++) {
data[i] = dataInputStream.readInt();
}
for (int i = data.length - 1; i >= 0; i--) {
System.out.print(data[i] + " ");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dataInputStream != null) {
dataInputStream.close();
}
if (dataOutputStream != null) {
dataOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次
*/
@Test
public void test28() {
Test28 t1 = new Test28("T1");
Test28 t2 = new Test28("T2");
Test28 t3 = new Test28("T3");
Thread thread1 = new Thread(t1);
Thread thread2 = new Thread(t2);
Thread thread3 = new Thread(t3);
thread1.start();
thread2.start();
thread3.start();
}
/*
* 定义一个表示学生信息的类Student,有以下成员变量:
* no:表示学号
* name:表示姓名
* gender:表示性别
* age:表示年龄
* java:表示Java课程成绩
* 根据类Student的定义,创建五个该类的对象,输入每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及他们Java语言成绩的最大值和最小值
*/
@Test
public void test29() {
Student student1 = new Student(1, "s1", "男", 21, 91);
Student student2 = new Student(2, "s2", "男", 22, 92);
Student student3 = new Student(3, "s3", "男", 23, 93);
Student student4 = new Student(4, "s4", "男", 24, 94);
Student student5 = new Student(5, "s5", "男", 25, 95);
System.out.println("平均值:" + function29_1(student1, student2, student3, student4, student5) + ",最大值:" + function29_2(student1, student2, student3, student4, student5)[0] + ",最小值:" + function29_2(student1, student2, student3, student4, student5)[1]);
}
public double function29_1(Student ... students) {
double sum = 0;
for (Student student : students) {
sum += student.getJava();
}
return sum / students.length;
}
public double[] function29_2(Student ... students) {
Arrays.sort(students, new MyComparator());
return new double[]{students[students.length - 1].getJava(), students[0].getJava()};
}
}
class Tourist {
private int age;
private double price;
public Tourist(int age) {
this.age = age;
}
public String ticket() {
if (age > 0 && age < 12) {
price = 20;
} else if (age < 20) {
price = 40;
} else if (age < 50) {
price = 80;
} else {
price = 35;
}
return "门票价格:" + price;
}
}
class Circle {
private double r;
public Circle() {}
public Circle(double r) {
this.r = r;
}
public double getPerimeter() {
return 2 * Math.PI * r;
}
public double getArea() {
return Math.PI * Math.pow(r, 2);
}
public void showCircle() {
System.out.println("半径:" + r + ",周长:" + getPerimeter() + ",面积:" + getArea());
}
}
class Cylinder extends Circle {
private double h;
public Cylinder (double r, double h) {
super(r);
this.h = h;
}
public double getVolume() {
return getArea() * h;
}
public void showCylinder() {
System.out.println("高:" + h + ",体积:" + getVolume());
}
}
class Test28 implements Runnable {
private String name;
public Test28(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(name + " start");
for (int i = 0; i < 20; i++) {
System.out.println(name);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + " end");
}
}
class Student {
private int no;
private String name;
private String gender;
private int age;
private double java;
public Student(int no, String name, String gender, int age, double java) {
this.no = no;
this.name = name;
this.gender = gender;
this.age = age;
this.java = java;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getJava() {
return java;
}
public void setJava(double java) {
this.java = java;
}
@Override
public String toString() {
return "Student [no=" + no + ", name=" + name + ", gender=" + gender + ", age=" + age + ", java=" + java + "]";
}
}
class MyComparator implements Comparator<Student> {
@Override
public int compare(Student student1, Student student2) {
return (int)(student1.getJava() - student2.getJava());
}
}
综合练习题
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 点“考研竞赛数学”↑可每天“涨姿势”哦! 竞赛题 【注1】试题与参考解析整理自网络,相关题目是否对应相应年份的题目...
- 题目: 请输出name变量中的值"Q的索引的位置 name = " gouguoQ" #定义name的内容v = ...