java語言程式設計 第十版(基礎篇)7.17-21
阿新 • • 發佈:2018-12-17
7.17
public static void main(String[] args) { // TODO Auto-generated method stub java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter number of students: "); int num = input.nextInt(); String [] name = new String [num]; double [] score = new double [num]; System.out.println("Enter "+num+" students' name ,score: "); for(int i =0;i<num;i++) { name[i]=input.next(); score[i]=input.nextDouble(); } Output(name,score); } public static void Output(String [] name,double [] score) { double temp=0; String tempName ; for(int i=0;i<score.length;i++) { for(int j =i+1;j<score.length;j++) { if(score[j]>score[i]) { temp = score[i]; score[i]=score[j]; score[j]=temp; tempName = name[i]; name[i]=name[j]; name[j]=tempName; } } } for(int i=0;i<score.length;i++) { System.out.println(name[i]+" "+score[i]); } }
7.18
public static void main(String[] args) { // TODO Auto-generated method stub java.util.Scanner input= new java.util.Scanner(System.in); double [] num = new double[10]; for(int i=0;i<10;i++) { num[i]= Math.random()*100; } bubbleSort(num); for(int i =0;i<10;i++) { System.out.printf("%4.3f\n",num[i]); } } public static void bubbleSort(double [] list) { double temp=0; for(int i=0;i<list.length;i++) { for(int j=i+1;j<list.length;j++) { if(list[i]>list[j]) { temp=list[j]; list[j]=list[i]; list[i]=temp; } } } }
7.19
public static void main(String[] args) { // TODO Auto-generated method stub java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter lits"); int num = input.nextInt(); int []list = new int [num]; for(int i =0;i<num;i++) { list[i]=input.nextInt(); } if(isSorted(list)) System.out.println("Tht list is alreaday sroted!"); else System.out.println("The list isn't sorted!"); } public static boolean isSorted(int [] list) { for(int i =0;i<list.length;i++) { for(int j =i+1;j<list.length;j++) { if(list[i]>list[j]) return false; } } return true; }
7.20
public static void main(String[] args) {
// TODO Auto-generated method stub
double [] list = new double [10];
for(int i =0;i<10;i++) {
list[i]=Math.random()*100;
}
selectionSort(list);
for(int i =0;i<10;i++) {
System.out.printf("%4.2f\n",list[i]);
}
}
public static void selectionSort(double[] list) {
for (int i = 0; i < list.length - 1; i++) {
// Find the minimum in the list[i..list.length-1]
double currentMax = list[i];
int currentMaxIndex = i;
for (int j = i + 1; j < list.length; j++) {
if (currentMax < list[j]) {
currentMax = list[j];
currentMaxIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMaxIndex != i) {
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
}
}
7.21 (水平有限,最後輸出沒有按照要求)
public static void main(String []args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of balls to drop :");
int ball = input.nextInt();
System.out.print("Enter the number of slots in the bean machine");
int slot = input.nextInt();
int [] slots = new int [slot];
ballPath(ball,slots);
}
public static void ballPath(int ball, int []slots) {
int path = slots.length-1;
int leftOrRight;
int countR;
for(int i=0;i<ball;i++) {
countR=0;
for(int j =0;j<path;j++) {//number of the path is slots.length-1
leftOrRight= (int)(Math.random()*101);//50% chance on left and 50% chance on right
if(leftOrRight<50)
System.out.print("L");
else {
System.out.print("R");
countR++;
}
}
switch(countR) {
case 0:slots[0]++;break;
case 1:slots[1]++;break;
case 2:slots[2]++;break;
case 3:slots[3]++;break;
case 4:slots[4]++;break;
case 5:slots[5]++;break;
case 6:slots[6]++;break;
case 7:slots[7]++;break;
case 8:slots[8]++;break;
}
System.out.println();
}
for(int i=0;i<slots.length;i++) {
if(slots[i]!=0) {
System.out.println("The "+i+" slot has "+slots[i]+(slots[i]>1?" ball":" balls"));
}
}
}