java语言程序设计 第十版(基础篇)7.17-21

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;iscore[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

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;ilist[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;ilist[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;i1?" ball":" balls"));
			}
			
		}
	
	}

你可能感兴趣的:(java)