撰寫一個程式,獲取10個1至20的亂數,要求亂數不能重復,
*
* 分析:
* A:創建亂數物件
* B:創建一個HashSet集合
* C:判斷集合的長度是不是小于10
* 是:就創建一個亂數添加
* 否:不搭理它
* D:遍歷HashSet集合
*/
package Day17; import java.util.HashSet; import java.util.Random; //撰寫一個程式,獲取10個1-20的亂數 public class Lx3 { public static void main(String[] args) { //創建一個亂數物件 Random AA =new Random(); //創建一個set集合 HashSet<Integer> MM = new HashSet<Integer>(); //判斷集合長度是否小于10 while(MM.size()<10){ //public int nextInt(int n)回傳一個偽亂數, // 它是取自此亂數生成器序列的、在 0(包括)和指定值(不包括)之間均勻分布的 int 值 //獲取1-20之間的亂數--其中亂數的產生包左不包右--所以默認是0-19--所以多加1使其從1-20 int num = AA.nextInt(20) +1; MM.add(num); } //遍歷HashSet集合 for(Integer e: MM){ System.out.println(e); } } }
/*
* 鍵盤錄入5個學生資訊(姓名,語文成績,數學成績,英語成績),按照總分從高到低輸出到控制臺
*
* 分析:
* A:定義學生類
* B:創建一個TreeSet集合
* C:總分從高到底如何實作呢?
* D:鍵盤錄入5個學生資訊
* E:遍歷TreeSet集合
學生類
package Day17; //鍵盤錄入5個學生資訊(姓名,語文成績,數學成績,英語成績),按照總分從高到低輸出到控制臺 public class Student3 { private String name; private int Chinese; private int number; private int English; //構造方法 public Student3(){} public Student3(String name, int chinese, int number, int english) { this.name = name; Chinese = chinese; this.number = number; English = english; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getChinese() { return Chinese; } public void setChinese(int chinese) { Chinese = chinese; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getEnglish() { return English; } public void setEnglish(int english) { English = english; } //給出學生總分---并回傳值 public int show(){ return this.Chinese+this.number+this.English; } }
測驗類
package Day17; import java.util.Comparator; import java.util.Scanner; import java.util.TreeSet; /*鍵盤錄入5個學生資訊(姓名,語文成績,數學成績,英語成績),按照總分從高到低輸出到控制臺 * 分析: * A:創建一個學生類---用來存盤學生的基本資訊--實作介面為Comparable * B:創建測驗類 * A: 創建集合TreeSet保證排序和唯一性 * B: 創建學生類物件--將其添加到集合中 * C: 使用自然排序---重寫Comparable介面下的CompareTo方法 * D: 遍歷輸出即可 */ public class Lx4 { public static void main(String[] args) { //創建TreeSet集合 //TreeSet<Student3> AA = new TreeSet<Student3>();---自然排序 //new comperator比較器排序 TreeSet<Student3> AA = new TreeSet<Student3>(new Comparator<Student3>() { @Override//重寫此方法---傳入兩個物件 public int compare(Student3 a , Student3 b ) { //將傳入的倆個物件資料進行比較--首先比較其總分 //如果是a-b,則排序是從低到高 //如果是b-a,則排序是從高到低 int num = b.show()-a.show(); //兩個物件的資料總分相同比較其語文分值 int num1 = num==0? b.getChinese()-a.getChinese():num; //兩個物件的語文分值也相同則比較數學分值 int num2 = num1==0?b.getNumber()-a.getNumber(): num1; //兩個物件數學分值也相同則比較英語的分值 int num3 = num2==0?b.getEnglish()-a.getEnglish():num2; //如何如上都相同--則比較其姓名 int num4 = num3==0?b.getName().compareTo(a.getName()):num3; return num4; } }); //鍵盤錄入五個學生的資訊 for(int x=1;x<=5;x++){ //創建鍵盤輸入物件 Scanner sc = new Scanner(System.in); System.out.println("請你輸入第"+x+"個學生的名字"); String name = sc.nextLine(); System.out.println("請你輸入第"+x+"個學生的語文成績"); String Chinese = sc.nextLine(); System.out.println("請你輸入第"+x+"個學生的數學成績"); String number = sc.nextLine(); System.out.println("請你輸入第"+x+"個學生的英語成績"); String english= sc.nextLine(); //創建學生物件并將鍵盤錄入的資訊添加到學生類物件中 Student3 SM = new Student3(); SM.setName(name); //使用Integer類下的parseInt方法 //public static int parseInt(String s)將字串引數作為有符號的十進制整數進行決議,轉化為int型別的整數 SM.setChinese(Integer.parseInt(Chinese)); SM.setNumber(Integer.parseInt(number)); SM.setEnglish(Integer.parseInt(english)); //將學生物件的資訊添加到集合中 AA.add(SM); } //遍歷輸出此集合 for(Student3 e: AA){ System.out.println(e.getName()+"\t"+e.getChinese()+"\t"+e.getNumber()+"\t"+e.getEnglish()); } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/449750.html
標籤:Java
