本文主要是介绍自测代码(方法与面向对象的应用【求区间质数、求100个随机数中的最小数、阶乘、图书管理类的应用】)-2021-7-21,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
MyClass类
package com.qianfeng.day07;public class MyClass {//输出三角形的行数public void printTriangle(int n) {for (int i = 1; i <= n; i++) {for (int j = 0; j < 2*i-1; j++) {System.out.print("*");}System.out.println();}}//返回区间[left,right]中质数的个数public int primeNumber(int left, int right) {int number = 0;for (int i = left; i <= right; i++) {int j;for (j = 2; j < i; j++) {if (i%j == 0) {break;}}if (i == j) {number++;}}return number;}//返回100个随机数中的最小值public int randomNumber(int n) {int min;int[] number = new int[100];for (int i = 0; i < 100; i++) {number[i] = (int)(Math.random()*n+1);}min = number[0];for (int i = 0; i < number.length; i++) {if (number[i] < min) {min = number[i];}}return min;}//求一个数的阶乘public int factorial(int n) {int number = 1;for (int i = 1; i <= n; i++) {number = number * i;}return number;}
}
LibraryManager类
package com.qianfeng.day07;public class LibraryManager {String[] bookNames = new String[10];double[] price = new double[10];public LibraryManager() {this.bookNames[0] = "老人与海";this.price[0] = 32;this.bookNames[1] = "活着";this.price[1] = 27;this.bookNames[2] = "红与黑";this.price[2] = 21;this.bookNames[3] = "西游记";this.price[3] = 26;this.bookNames[4] = "水浒传";this.price[4] = 28;this.bookNames[5] = "三国演义";this.price[5] = 30;this.bookNames[6] = "红楼梦";this.price[6] = 36;this.bookNames[7] = "小王子";this.price[7] = 38;this.bookNames[8] = "悲惨世界";this.price[8] = 40;this.bookNames[9] = "巴黎圣母院";this.price[9] = 42;}//1.根据传入的书名查找某本书的价格并返回给用户(如果图书不存在则显示没有这本书)public double inquire(String bookName) {double price = 0;for (int i = 0; i < 10; i++) {if (bookName == this.bookNames[i]) {price = this.price[i];}}if (price == 0) {System.out.println("查无此书");}return price;}//2.向客户展示目前所有的图书信息public void printBook() {for(int i = 0; i < 10; i++) {System.out.println("第" + (i+1) + "本书为:" + this.bookNames[i] + ",价格为:" + this.price[i]);}}//3.显示单价最高的前3本书的信息(书名和价格)public void maxPrice() {double temp;//备份价格表double[] xprice = new double[10];for (int i = 0; i < xprice.length; i++) {xprice[i] = this.price[i];}//冒泡排序选出最大的三个数在数组最后for (int i = 0; i < 3; i++) {for (int j = 0; j < 10-1; j++) {if (xprice[i] > xprice[i+1]) {temp = xprice[i];xprice[i] = xprice[i+1];xprice[i+1] = temp;}}}System.out.println("价格最高的三本书信息如下:");//将最大的三个价格与原来价格表进行比较,选出其对应书籍for (int i = 9; i > 6; i--) {for (int j = 0; j < 10; j++) {if (xprice[i] == this.price[j]) {System.out.print("本书为:" + this.bookNames[j]);}}System.out.println(",价格为:" + xprice[i]);}}//4.根据传入的书名删除这本书,删除完成后显示一下最新的图书情况public void deleteBook(String bookName) {int index = 0;for (int i = 0; i < 10; i++) {if (bookName == this.bookNames[i]) {index = i;}}for (int i = index; i < 9; i++) {this.bookNames[i] = this.bookNames[i+1];this.price[i] = this.price[i+1];}for (int i = 0; i < 9; i++) {System.out.println(this.bookNames[i] + "价格为:" + this.price[i]);}}}
work类(测试类)
package com.qianfeng.day07;public class Work {public static void main(String[] args) {MyClass myClass = new MyClass();//输出三角形行数System.out.println("输入的行数是:5");myClass.printTriangle(5);System.out.println();//返回区间质数的个数System.out.println("区间是[1,100]");System.out.println("有:"+ myClass.primeNumber(1, 10) + "个质数");System.out.println();//返回一百个随机数的最小值System.out.println("随机数设置为:1000");System.out.print("最小的那个随机数为:");System.out.println(myClass.randomNumber(1000));System.out.println();//返回一个数的阶乘System.out.print("5!=");System.out.println(myClass.factorial(5));System.out.println("\n\n");//测试图书管理类LibraryManager libraryManager = new LibraryManager();System.out.print("查询的书是红与黑,价格为:");System.out.println(libraryManager.inquire("红与黑"));System.out.println();System.out.println("所有书籍信息为");libraryManager.printBook();System.out.println();libraryManager.maxPrice();System.out.println();System.out.println("删除的书籍为活着");libraryManager.deleteBook("活着");}}
这篇关于自测代码(方法与面向对象的应用【求区间质数、求100个随机数中的最小数、阶乘、图书管理类的应用】)-2021-7-21的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!