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

本文涵盖了Java编程基础的第7章关键知识点,包括7.17至7.21节的内容。通过深入学习这些章节,读者将能够掌握Java编程中的核心概念和技巧。
摘要由CSDN通过智能技术生成

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"));
			}
			
		}
	
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值