【JAVA】猜数字
2024-08-26 18:56:24
import java.util.*; public class GN {
public static void main(String arg[]) {
int a = ;// 数字标记
int b = ;// 位置标记
int time = ;// 次数
int rdn[] = new int[];
Random rd = new Random(); do {
rdn[] = rd.nextInt();
rdn[] = rd.nextInt();
rdn[] = rd.nextInt();
rdn[] = rd.nextInt();// 生成四个随机数字。
if (rdn[] == || rdn[] == rdn[] || rdn[] == rdn[]
|| rdn[] == rdn[] || rdn[] == rdn[] || rdn[] == rdn[]
|| rdn[] == rdn[] || rdn[] == rdn[] || rdn[] == rdn[]
|| rdn[] == rdn[])
continue;
else
break;
} while (true); do {
System.out.println("请输入您所猜的数字:");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String x = String.valueOf(n);
if (n == )
System.out.println(rdn[] + "" + rdn[] + "" + rdn[] + ""
+ rdn[]);
int num[] = new int[];
num[] = n / ;
num[] = n / % ;
num[] = n % / ;
num[] = n % ;// 处理键入的数字
if (num[] == ) {
System.out.println("输入的数字不能以零开头,请重新输入。");
} else if (x.length() > || x.length() <= )
System.out.println("输入的数字不符合要求,请重新输入。");
else if (x.length() == ) {
time++;
a = ;
b = ;
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (num[i] == rdn[j])
if (i == j)
b++;
else
a++; }
}
if (b != )
System.out.println("数字相同而位置不同有 " + a + " 个,位置和数字都相同有 " + b
+ " 个");
if (b != && time > )
System.out.println("你个笨蛋,都猜" + time + "次了,还猜不对!");
}
if (b == ) {
System.out.println("恭喜你,猜对了!一共猜了" + time + "次。");
break;
}
} while (true); } }
最新文章
- 2.快速部署MySQL主从复制
- 【webGL】threejs常用的api
- ORA-32004: obsolete and/or deprecated parameter(s) specified
- 解决li在ie,firefox中行高不一致问题
- Layout Resource官方教程(1)简介
- 多次读取请求request里数据
- opengl 函数
- Exception testing
- 常用的CSS属性
- Python购物车的实现课程
- hadoop启动守护进程报JAVA_HOME is not set and could not be found
- 项目中Spring注入报错小结
- 存读Blob Oracle
- 兔子与樱花[HEOI2015]
- 使用PHP画统计图的方法
- python中type dtype astype 的用法
- mongodb数据导入导出
- 浏览器与Tomcat交互
- python爬虫-基础入门-爬取整个网站《2》
- js的原型prototype究竟是什么?
热门文章
- libaio.so.1: undefined reference to `__stack_chk_fail@GLIBC_2.4&#39;
- C# DataTable转实体 通用方法
- Apache+php5
- ios的两种界面跳转方式
- Linux+Redis实战教程_day03_Redis-set【重点】_有序set(了解)
- ios开发之--把秒转换为天时分秒
- 安装eclipse,配置tomcat
- Java调用MQ队列
- Fragment获取Activity,Activity获取Fragment
- 偶值得纪念的一天-初学习C#