[关闭]
@Hubertoo 2017-07-17T05:05:14.000000Z 字数 6123 阅读 653

Fundamental.Unit06

达内


知识体系

方法

经典案例

猜字母游戏

  1. 设计数据结构
  1. package day01;
  2. public class GuessLettersGame {
  3. public static void main(String[] args) {
  4. //表示玩家猜测的次数
  5. int count =0;
  6. //表示用户猜测的数据
  7. char[] input = null;
  8. //表示猜测的字符串
  9. char[] chs = null;
  10. //用于保存判断的结果,result[0]表示猜对的字母个数(字符对位置也对),result[1]表示字符对位置不对的个数
  11. //如果用两个int型,则方法在返回值上面不好处理,好的数据结构可以带来好的算法
  12. int[] result = new int[2];
  13. }
  14. }
  1. 设计程序结构
  1. package day01;
  2. public class GuessLettersGame {
  3. public static void main(String[] args) {
  4. //表示玩家猜测的次数
  5. int count =0;
  6. //表示用户猜测的数据
  7. char[] input = null;
  8. //表示猜测的字符串
  9. char[] chs = null;
  10. //用于保存判断的结果,result[0]表示猜对的字母个数(字符对位置也对),result[1]表示字符对位置不对的个数
  11. //如果用两个int型,则方法在返回值上面不好处理,好的数据结构可以带来好的算法
  12. int[] result = new int[2];
  13. }
  14. /**
  15. * 随机生成需要猜测的字母序列
  16. * @return 随机生成的字符数组
  17. */
  18. public static char[] generate(){
  19. char[] chs = new char[5];
  20. return chs;
  21. }
  22. /**
  23. * 比较用户输入的字符数组和随机生成的字符数组
  24. * @param chs 随机生成的字符数组
  25. * @param input 用户输入的字符数组
  26. * @return 比较结果,对的个数
  27. */
  28. public static int[] check(char[] chs, char[] input){
  29. int[] result = new int[2];
  30. return result;
  31. }
  32. }
  1. 实现字母生成方法
  1. ` /**
  2. * 随机生成需要猜测的字母序列
  3. * @return 随机生成的字符数组
  4. */
  5. public static char[] generate(){
  6. char[] chs = new char[5];
  7. char[] letters = {'A','B','C','D','E','F','G',
  8. 'H','I','J','K','L','M','N',
  9. 'O','P','Q','R','S','T',
  10. 'U','V','W','X','Y','Z'};
  11. boolean[] flags = new boolean[letters.length];
  12. for(int i=0; i<chs.length; i++){
  13. int index;
  14. do{
  15. index = (int)(Math.random() * (letters.length));
  16. }while(flags[index]); //用来判断生成的字符是否重复
  17. flags[index] = true;
  18. }
  19. return chs;
  20. }
  1. 实现字母检测方法
  1. /**
  2. * 比较用户输入的字符数组和随机生成的字符数组
  3. * @param chs 随机生成的字符数组
  4. * @param input 用户输入的字符数组
  5. * @return 比较结果,对的个数
  6. */
  7. public static int[] check(char[] chs, char[] input){
  8. int[] result = new int[2];
  9. for(int i=0; i<input.length; i++){
  10. for(int j=0; j< chs.length; j++){
  11. if(input[i] == chs[j]){
  12. result[1]++;
  13. if(i == j){
  14. result[0]++;
  15. }
  16. break;
  17. }
  18. }
  19. }
  20. return result;
  21. }
  1. 实现主方法
  1. package day01;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. public class GuessLettersGame {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(System.in);
  7. //表示玩家猜测的次数
  8. int count =0;
  9. //表示用户猜测的数据
  10. char[] input = null;
  11. //表示猜测的字符串
  12. char[] chs = null;
  13. //用于保存判断的结果,result[0]表示猜对的字母个数(字符对位置也对),result[1]表示字符对位置不对的个数
  14. //如果用两个int型,则方法在返回值上面不好处理,好的数据结构可以带来好的算法
  15. int[] result = new int[2];
  16. //先看到5个字符,好测试
  17. chs = generate();
  18. System.out.println(Arrays.toString(chs));
  19. System.out.println("游戏开始,请输入你猜的五个字符序列:exit退出");
  20. while(true){
  21. String inputStr = scan.next().trim().toUpperCase();
  22. if("EXIT".equals(inputStr)){
  23. System.out.println("谢谢你的尝试,下次再来");
  24. break;
  25. }
  26. input = inputStr.toCharArray();
  27. result = check(chs,input);
  28. if(result[0] == chs.length){ //全对的情况下
  29. int score = 100 * chs.length -count *10;
  30. System.out.println("你全猜对了,得分是:" + score);
  31. }else{
  32. count++;
  33. System.out.println("你猜对了:" + result[1] + "个字符,其中有" + result[0] + "个字符的位置正确(你的猜测字数是:" + count +")。" );
  34. }
  35. }
  36. scan.close();
  37. }
  38. /**
  39. * 随机生成需要猜测的字母序列
  40. * @return 随机生成的字符数组
  41. */
  42. public static char[] generate(){
  43. char[] chs = new char[5];
  44. char[] letters = {'A','B','C','D','E','F','G',
  45. 'H','I','J','K','L','M','N',
  46. 'O','P','Q','R','S','T',
  47. 'U','V','W','X','Y','Z'};
  48. boolean[] flags = new boolean[letters.length];
  49. for(int i=0; i<chs.length; i++){
  50. int index;
  51. do{
  52. index = (int)(Math.random() * (letters.length));
  53. }while(flags[index]); //用来判断生成的字符是否重复
  54. chs[i] = letters[index];
  55. flags[index] = true;
  56. }
  57. return chs;
  58. }
  59. /**
  60. * 比较用户输入的字符数组和随机生成的字符数组
  61. * @param chs 随机生成的字符数组
  62. * @param input 用户输入的字符数组
  63. * @return 比较结果,对的个数
  64. */
  65. public static int[] check(char[] chs, char[] input){
  66. int[] result = new int[2];
  67. for(int i=0; i<input.length; i++){
  68. for(int j=0; j< chs.length; j++){
  69. if(input[i] == chs[j]){
  70. result[1]++;
  71. if(i == j){
  72. result[0]++;
  73. }
  74. break;
  75. }
  76. }
  77. }
  78. return result;
  79. }
  80. }

课后作业

随机生成数组

封装一个方法generateArray(),实现生成指定长度的int数组,元素都是随机值
开始没有调用方法:

  1. package day01;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. public class GenerateArray {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(System.in);
  7. System.out.println("输入生成数组长度");
  8. int index = scan.nextInt();
  9. System.out.println("输入数组元素最大值:");
  10. int max = scan.nextInt();
  11. int[] arr = new int[index];
  12. for(int i=0; i<arr.length; i++){
  13. arr[i] = (int)(Math.random() * max + 1);
  14. }
  15. System.out.println(Arrays.toString(arr));
  16. }
  17. }

调用方法之后:

  1. package day01;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. public class GenerateArray {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(System.in);
  7. System.out.println("输入生成数组长度");
  8. int index = scan.nextInt();
  9. System.out.println("输入数组元素最大值:");
  10. int max = scan.nextInt();
  11. int[] arr = generateArray(index,max);
  12. System.out.println(Arrays.toString(arr));
  13. }
  14. /**
  15. * 生成指定长度的数组,里面元素范围也是指定
  16. * @param index 指定长度
  17. * @param max 指定范围
  18. * @return 产生的数组
  19. */
  20. public static int[] generateArray(int index, int max){
  21. int[] arr = new int[index];
  22. for(int i=0; i<arr.length; i++){
  23. arr[i] = (int)(Math.random() * max + 1);
  24. }
  25. return arr;
  26. }
  27. }

在猜字母游戏中实现游戏等级(3,5,7,9)

这个时候在写程序时候,考虑程序的扩展性

  1. package day01;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. public class GuessLettersGamePlus {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(System.in);
  7. //表示玩家猜测的次数
  8. int count =0;
  9. //表示用户猜测的数据
  10. char[] input = null;
  11. //表示猜测的字符串
  12. char[] chs = null;
  13. //用于保存判断的结果,result[0]表示猜对的字母个数(字符对位置也对),result[1]表示字符对位置不对的个数
  14. //如果用两个int型,则方法在返回值上面不好处理,好的数据结构可以带来好的算法
  15. int[] result = new int[2];
  16. //接受等级g
  17. System.out.println("请输入游戏等级(3,5,7,9):");
  18. int g = scan.nextInt();
  19. //先看到5个字符,好测试
  20. chs = generate(g);
  21. System.out.println(Arrays.toString(chs));
  22. System.out.println("游戏开始,请输入你猜的五个字符序列:exit退出");
  23. while(true){
  24. String inputStr = scan.next().trim().toUpperCase();
  25. if("EXIT".equals(inputStr)){
  26. System.out.println("谢谢你的尝试,下次再来");
  27. break;
  28. }
  29. input = inputStr.toCharArray();
  30. result = check(chs,input);
  31. if(result[0] == chs.length){ //全对的情况下
  32. int score = 100 * chs.length -count *10;
  33. System.out.println("你全猜对了,得分是:" + score);
  34. }else{
  35. count++;
  36. System.out.println("你猜对了:" + result[1] + "个字符,其中有" + result[0] + "个字符的位置正确(你的猜测字数是:" + count +")。" );
  37. }
  38. }
  39. scan.close();
  40. }
  41. /**
  42. * 随机生成需要猜测的字母序列
  43. * @return 随机生成的字符数组
  44. */
  45. public static char[] generate(int g){
  46. char[] chs = new char[g];
  47. char[] letters = {'A','B','C','D','E','F','G',
  48. 'H','I','J','K','L','M','N',
  49. 'O','P','Q','R','S','T',
  50. 'U','V','W','X','Y','Z'};
  51. boolean[] flags = new boolean[letters.length];
  52. for(int i=0; i<chs.length; i++){
  53. int index;
  54. do{
  55. index = (int)(Math.random() * (letters.length));
  56. }while(flags[index]); //用来判断生成的字符是否重复
  57. chs[i] = letters[index];
  58. flags[index] = true;
  59. }
  60. return chs;
  61. }
  62. /**
  63. * 比较用户输入的字符数组和随机生成的字符数组
  64. * @param chs 随机生成的字符数组
  65. * @param input 用户输入的字符数组
  66. * @return 比较结果,对的个数
  67. */
  68. public static int[] check(char[] chs, char[] input){
  69. int[] result = new int[2];
  70. for(int i=0; i<input.length; i++){
  71. for(int j=0; j< chs.length; j++){
  72. if(input[i] == chs[j]){
  73. result[1]++;
  74. if(i == j){
  75. result[0]++;
  76. }
  77. break;
  78. }
  79. }
  80. }
  81. return result;
  82. }
  83. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注