Java练习题

Java练习题,第1张

Java练习

目录
  • Java练习
  • 前言
  • 第一题、模拟彩票双色球
    • 1.1 版本一(覆盖加扩容的方式解决重复问题)
    • 2.2 版本二(使用贴标签的方式解决重复问题)
  • 第二题 数组排序
    • 2.1 冒泡排序
    • 2.2 选择排序
  • 续作
  • 总结

前言

温故而知新


第一题、模拟彩票双色球 1.1 版本一(覆盖加扩容的方式解决重复问题)
package day06;

import java.util.Arrays;
import java.util.Random;

public class BoubleColorBalls {
	
	public static void main(String[] args) {
		String[] redPoolStrings = {"01","02","03","04","05","06"
				,"07","08","09","10","11","12","13","14","15","16","17","18"
				,"19","20","21","22","23","24","25","26","27","28","29","30"
				,"31","32","33"};
		String[] balls = new String[6];
		//创建Random对象,随机生成33个红球下表
		Random rdRandom = new Random();
		for (int i = 0; i < balls.length; i++) {
			//随机生成红球下标
			int index = rdRandom.nextInt(redPoolStrings.length);
			
			//通过下标取出一个红球放置到球罐里
			 balls[i] = redPoolStrings[index];
			 redPoolStrings[index] = redPoolStrings[redPoolStrings.length - 1 - i];
			 redPoolStrings = Arrays.copyOf(redPoolStrings, redPoolStrings.length - 1);
			 System.out.println(Arrays.toString(redPoolStrings));
			 
		}
		Arrays.sort(balls);
		System.out.println(Arrays.toString(balls));
		String[] bluePoolStrings = {"01","02","03","04","05","06"
				,"07","08","09","10","11","12","13","14","15","16"};
		String blueBall = bluePoolStrings[rdRandom.nextInt(bluePoolStrings.length)];
		balls = Arrays.copyOf(balls,balls.length + 1);
		balls[balls.length - 1] = blueBall;
		System.out.println(Arrays.toString(balls));
 	}

}
2.2 版本二(使用贴标签的方式解决重复问题)
package day06;

import java.util.Arrays;
import java.util.Random;

public class BoubleColorBalls2 {
	
	public static void main(String[] args) {
		String[] redPoolStrings = {"01","02","03","04","05","06"
				,"07","08","09","10","11","12","13","14","15","16","17","18"
				,"19","20","21","22","23","24","25","26","27","28","29","30"
				,"31","32","33"};
		
		String[] balls = new String[6];
		
		boolean[] bq = new boolean[redPoolStrings.length];
		//创建Random对象,随机生成33个红球下表
		Random rdRandom = new Random();
		int i = 0;
		do {
			
			int index = rdRandom.nextInt(redPoolStrings.length);
			if (bq[index]) {
				
				continue;
			}
			balls[i] = redPoolStrings[index];
			bq[index] = true;
			
			i++;
			
		} while (i != 6);
		Arrays.sort(balls);
		System.out.println(Arrays.toString(balls));
		
		String blueBall = redPoolStrings[rdRandom.nextInt(16)];
		balls = Arrays.copyOf(balls,balls.length + 1);
		balls[balls.length - 1] = blueBall;
		System.out.println(Arrays.toString(balls));
 	}

}
第二题 数组排序 2.1 冒泡排序
package day07;

import java.util.Arrays;

public class SortDemo {
	
	public static void main(String[] args) {
		
		int[] ary = new int[] {5,6,4,7,8,3,};
		Arrays.sort(ary);
		System.out.println(Arrays.toString(ary));
		
		bubbleSort(ary);
		System.out.println(Arrays.toString(ary));
		
	}
	
	//冒泡排序
	public static void bubbleSort(int[] ary) {
		
		for (int i = 0; i < ary.length; i++) {
			for (int j = 0; j < ary.length - i -1; j++) {
				
				if (ary[j] < ary[j + 1]) {
					
					int temp = ary[j];
					ary[j] = ary[j + 1];
					ary[j + 1] = temp;
					
				}
				
			}
		}
		
	}

}
2.2 选择排序
package day07;

import java.util.Arrays;

public class SortDemo {
	
	public static void main(String[] args) {
		
		int[] ary = new int[] {5,6,4,7,8,3,};
		Arrays.sort(ary);
		System.out.println(Arrays.toString(ary));
		selectSort(ary);
		System.out.println(Arrays.toString(ary));
	}
	
	
	//选择排序
	public static void selectSort(int[] ary) {
		
		for (int i = 0; i < ary.length - 1; i++) {
			for (int j = i + 1; j < ary.length; j++) {
				
				if(ary[i] > ary[j]) {
					int temp = ary[i];
					ary[i] = ary[j];
					ary[j] = temp;
				}
				
			}
			
		}
		
	}

}

package day07;

import java.net.CacheRequest;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;

import javax.naming.ldap.Rdn;

public class GuessNumber {
	
	public static void main(String[] args) {
			
		System.out.println("欢迎进入猜字游戏,请输入难度级别:");
		
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		char[] ans = gen(num);
		System.out.println(Arrays.toString(ans));
		Scanner s = new Scanner(System.in);
		
		
		for (;/* true */ ; ) {
			
			System.out.println("请输入你猜的答案:");
			String str = s.nextLine();
			if ("exit".equals(str)) {
				System.out.println("猜不出来,不玩了!");
				break;
			}
			
			char[] gs = str.toCharArray();
			int[] cs = compare(gs, ans);
			System.out.println("你猜对了"+cs[0]+"个字母,猜对"+cs[1]+"个位置。");
			if (num == cs[1]) {
				
				System.out.println("棒棒哒!");
				break;
				
			}
		}			
		
	}	
	
	public static char[] gen(int m) {
		
		//
		char[] str = {'A','B','C','D','E','F','G','H','I'
				,'J','K','L','M','N','O','P','Q','R','S'
				,'T','U','V','W','X','Y','Z'};
		//
		char[] cs = new char[m];
		Random random = new Random();
		int i = 0;
		while (true) {
			int index = random.nextInt(str.length);
			cs[i] = str[index];
			//
			str[index] = str[str.length - 1];
			str = Arrays.copyOf(str, str.length - 1);
			i++;		
			if (i == m) {			
				break;
			}		
		}		
		return cs;		
	}
	
	public static int[] compare(char[] gues,char[] right) {
		
		int[] ins = new int[2];
		int nums = 0;
		int indexs = 0;
		for (int i = 0; i < gues.length; i++) {
			for (int j = 0; j < right.length; j++) {
				
				if(gues[i] == right[j]) {
					nums++;
					
					if (i == j) {
						indexs++;
					}
				}
				
			}
		}
		ins[0] = nums;
		ins[1] = indexs;
		return ins;
		
	}
	
	
//	private static void bijiao(int num) {
//		
//		System.out.println("请输入"+ num +"个字母(大写):");
//		Scanner scanner = new Scanner(System.in);
//		String str = scanner.next();
//		char[] yonghu = str.toCharArray();
//		
//		while (true) {
//			char[] sj = suiji(num);
//			if (yonghu.length != num) {
//			
//			System.out.println("您输入的字母个数不对!请重新输入:");
//			bijiao(num);
//			
//		}else if (yonghu.equals(sj)){
//			
//			System.out.println("您猜的全对了");
//			
//		}else  {
//			
//			int count = 0;
//			for (int i = 0; i < yonghu.length; i++) {
//				for (int j = 0; j < sj.length; j++) {
//					if (yonghu[i] == sj[j]) {
//					
//					count++;			
//				}
//				}			
//				
//			}
//			if (count == 0) {
//				
//				System.out.println("错完了");
//				break;
//
//			}else {
//				System.out.println("" +count);
//				
//				break;
//			}
//			
//		}
//		
//		}
//		
//			
//		}
		
		
		
	
	
	
	
//	public static char[] suiji(int n) {
//		
//		char[] cha = new char[n];
//		for (int i = 0; i < n; i++) {
//			
//			Random random = new Random();
//			int num = random.nextInt(26) + 65;
//			cha[i] = (char)num;
//			System.out.println(Arrays.toString(cha));		
//			
//		}
//		return cha;	
//		
//	}

}

续作
  1. 控制台随机打印一个大写字母

  2. 控制台输入一个字符,判断是否为英文字母

  3. 控制台输入一个年费份,判断该年份是闰年还是平年

  4. 控制台输入一个百分制成绩,判断该成绩的等级

    90~100 ->‘A’ 60~89 ->‘B’ 60以下->‘C’

  5. 控制台输入你的薪资,判断你回家的方式

    sal>15000 “坐飞机”

  6. 控制台打印所有的水仙花数(三种循环结构)

    利用循环完成累加案例:

  7. 1+2+3+4…+100

  8. 计算PI的值

  9. 9+99+999+…+99999999(8个9)

  10. 折纸超过珠穆朗玛峰的高度利用死循环

  11. 定义方法,打印M行列的(*)图案

  12. 定义方法,打印乘法口诀表

  13. 百钱百鸡 公:5 母:3 小:1/3

  14. 控制台输入一个不限位数的整数,判断是否为欸回文数

  15. 重构双色球案例(解决红球重复的问题两种方式)

  16. 利用递归求出处第N个斐波那契数列

  17. 猜字母案例

总结

提示:这里对文章进行总结:

例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/943725.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存