Java方法可以理解为C#中的函数,都是把复杂的问题简单化,按模块,按功能区分,分别完成各个部分在调用这些方法完成整个功能。

方法的综合练习,猜数字的实现:

代码要求:

生成不重复的4位数字(只有1--9)
然后用户竞猜,总共最多只能猜7次
每次都是输入4位数字,按照标准,
分为数字存在(记录为B);
数字存在且位置正确(记录为A)
例如: 生成数字1234
竞猜数字: 1367(提示竞猜结论: 1A1B)
如果1次就中了,提示: 我的神啊!
如果2次就中了,提示: 您是天才啊!
如果3次就中了,提示: 您也是个人才!
如果4次就中了,提示: 不错不错啊
如果5次就中了,提示: 哟,厉害了哟!
如果6次就中了,提示: 呵呵,很好
如果7次就中了,提示: 终于对了,好难啊!

思路:猜数字分为: 1.随机提取不同的四位数;

          2.输入猜测的四位数;

          3.判断猜测是否正确

          4.根据猜测次数做出输出语句

 

 package Hw0414;

 import java.util.Scanner;

 public class Hw01 {

     public static void main(String[] args) {
System.out.println("\t\t\t游戏 猜数字");
System.out.println(
"游戏规则:\n1.生成不重复的4位数字(只有1--9)然后用户竞猜;\n2.总共最多只能猜7次每次都是输入4位数字;\n3.按照标准,分为数字存在(记录为B);数字存在且位置正确(记录为A);");
System.out.println("\t\t\t1.开始游戏\n2.退出游戏");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int[] nums = new int[4];
int[] num = new int[4];
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (a == 2)// 判断是退出游戏
{
System.out.println("game over");
System.exit(a); }
nums = shuzi(a);// 调用方法shuzi将产生的四位随机数存入nums
for (int i = 0; i < nums.length; i++)
System.out.print(nums[i]);
shuchu(panduan(nums, a));// 调用输出语句,在输出语句的实参为判断方法返回的判断次数
} /**
* 输出长度为四但是彼此不想的的数组
*
* @param a开始游戏信号
* @return 需要猜的数字
*/
public static int[] shuzi(int a) {
int[] nums = new int[4];
int count = 0;
while (count < 4) {
if (count == 0) {
nums[count] = (int) (Math.random() * 9 + 1);
} else {
int temp = (int) (Math.random() * 9 + 1);// 随机产生的数字不能相同
nums[count] = temp;
for (int i = 0; i < count; i++) {
if (nums[count] == nums[i]) {
count--;
break;
}
}
}
count++;
}
return nums;
} /**
* 输入您需要猜测的数字
*
* @param 确认输入信号
* @return猜测数字存入数组返回
*/
public static int[] caice(int a) {
while (true) {
System.out.println("\n\n\t请输入您要猜测的数字(四位数)");
int[] num = new int[4];
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
int count = 0;
if (input >= 1000 && input <= 9999) {
num[0] = input / 1000 % 10;
num[1] = input / 100 % 10;
num[2] = input / 10 % 10;
num[3] = input % 10;
for (int i = 0; i < num.length - 1; i++) {
for (int j = i; j < num.length; j++) {
if (num[i] != num[j]) {
count++;
}
}
}
if (count == 6) {
return num;
} else {
System.out.println("输入错误,数字不能相等");
}
} else {
System.out.println("您的输入错误请重新输入");
}
}
} /**
* 判断输入并且输出相应的提示
*
* @param 随机数组
* @param 游戏开始信号
* @return猜了多少次猜对了
*/
public static int panduan(int[] nums, int a) {
int[] num = new int[4];
int count = 0;// 猜测次数计数器
while (true) {
num = caice(a);// 判断语句内部调用猜测方法
System.out.println();
int b = 0;// 数字正确且在位置计数器
int c = 0;// 数字存在且不在位置计数器 for (int i = 0; i < num.length; i++) {
for (int j = 0; j < nums.length; j++)
if (num[i] == nums[j])// 判断输入数字和答案数字有无相同,位置数字相同b++,数字存在位置不对
{
if (i == j) {
b++;
} else {
c++;
}
}
}
count++;// 猜测次数计数器
System.out.println("您还剩" + (7 - count) + "次");
if (b == 4)// 猜测都对
{
return count;
} else
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("\t输入的数字为:");
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]);
}
System.out.println("\t提示:" + b + "A" + c + "B\n\t加油,再接再厉");
if (count == 7) {
System.out.println("抱歉您的机会以及用完 ");
return 0;
}
}
} /**
* 输出语句
*
* @param a输入猜测的次数
*/
public static void shuchu(int a) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
switch (a) {
case 1:
System.out.println("我的神啊!");
break;
case 2:
System.out.println("您是天才啊!");
break;
case 3:
System.out.println("您也是个人才!");
break;
case 4:
System.out.println("不错不错啊");
break;
case 5:
System.out.println(" 哟,厉害了哟!");
break;
case 6:
System.out.println("呵呵,很好");
break;
case 7:
System.out.println("终于对了,好难啊!");
default:
System.out.println("GAME OVER");
}
}
}

总结:

  方法能够很好的将复杂问题简单化,分而治之,首先对问题的分解,当能够很好地将问题分解为多部分,只需要将每部分完成,后调用每个部分完成整个问题及可,在方法调用时注意参数列表和返回值这个两个部分,正确的配置参数列表合理的利用方法。

最新文章

  1. 关于 bind 你可能需要了解的知识点以及使用场景
  2. JAVA常用的XML解析方法
  3. 使用php脚本查看已开启的扩展
  4. 打开FileGeoDatabase中要素类
  5. PyOpenGL利用文泉驿正黑字体显示中文字体
  6. 161018、springMVC中普通类获取注解service方法
  7. 简单的C语言小学四则运算设计
  8. MySQL(25):事务的隔离级别出现问题之 不可重复读
  9. GridView自带分页 1总页数 首页 下一页 上一页 尾页 X 页 go 实现方法 .
  10. C​+​+​默​认​构​造​函​数
  11. 软件授权协议有什么作用,例如GPL、Apache License、CDDL、EPL这些协议有什么区别?
  12. &lt;php&gt;对文件的目录、属性、路径的操作
  13. 如何分割(split)string字符串
  14. 面向GC的Java编程(转)
  15. Servlet详解
  16. java 基础知识五 数组
  17. android studio gradle 两种更新方法更新
  18. tomcat调优的几个方面(转)
  19. 用python实现的一个自动聊天的机器人
  20. ubuntu16.04运行ros的时候编译工作空间catkin_make出现的一个问题Could not find a package configuration file provided by

热门文章

  1. 被遗忘的 Logrotate
  2. PHP和zookeeper结合实践
  3. H5-video1 iOS苹果和微信中音频和视频实现自动播放的方法
  4. JAVA关键技术
  5. 多层下firebird自增长字段的处理
  6. QT中tableview不能更新数据,why?
  7. 一个表空间使用率查询sql的优化
  8. 状态压缩dp poj 3254 hdu5045
  9. 【JEECG技术博文】Local storage &amp;amp; easyui extensions
  10. 能够在子线程绘画的View SurfaceView