591
社区成员
发帖
与我相关
我的任务
分享
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class GUESSGAME {
/**
* 开始猜数字游戏
*
* @param maxCount
* 猜测上线
* @param maxRandom
* 随机数最大值
*/
public static void startTheGame(int maxCount, int maxRandom) {
// 初始化
Random random = new Random();
int sourceInt = 1 + random.nextInt(maxRandom);
int count = 0;
// TODO 测试用,上线需删除
System.out.println("源数字:" + sourceInt);
int targetInt = 0;
// 循环输入====关键点,思路是否清晰,是否先设计框架及步骤在开始开发
while (true) {
// 看门狗,若超过循环上限跳出循环====关键点,防止死循环,业务上即便没有要求也要有看门狗,避免调用黑盒或其他系统接口导致本服务崩溃
if (count >= maxCount) {
System.out.println("超过猜数字上限,上限为:" + maxCount + ",game over!");
break;
}
// 输入目标值,每输入一次计数器加一
try {
targetInt = new Scanner(System.in).nextInt();
count++;
} catch (InputMismatchException e) {// ====关键点,防止用户使用时的异常,当然直接Exception也可以
System.out.println("请重新输入整形数值");
continue;
}
// 比较源与目标大小
Map<String, Object> resultMap = compareTheInt(sourceInt, targetInt);
// 打印比较信息
if (null != resultMap && null != resultMap.get("message")) {// ====关键点,若使用不确定返回值一定要判空,避免异常
System.out.println(compareTheInt(sourceInt, targetInt).get(
"message")
+ ",一共猜了:" + count + "次");
}
// 若源与目标相等跳出循环
if (null != resultMap
&& null != resultMap.get("flag")
&& ((boolean) compareTheInt(sourceInt, targetInt).get(
"flag"))) {
break;
}
}
}
/**
* 比较两个int大小,并打印结果
*
* @param sourceInt
* @param targetInt
* @return key:value =
* {"flag":boolean,"message":String}--最好用个类似Message的对象,一个项目的所有返回都使用统一值
* ,提高扩展性降运维成本,例如:以后要加一个属性,表示这次两个数之间的差等等类似需求,不需要该方法修改结构即可实现。
* 若面试时这个是对外的api或接口,不考虑这一点的在我们项目会大打折扣
*/
public static Map<String, Object> compareTheInt(int sourceInt, int targetInt) {
Map<String, Object> resultMap = new HashMap<String, Object>();
// 目标大于源
if (sourceInt < targetInt) {
resultMap.put("flag", false);
resultMap.put("message", "你填写的数字大了");
} else
// 源大于目标
if (sourceInt > targetInt) {
resultMap.put("flag", false);
resultMap.put("message", "你填写的数字小了");
} else
// 源等于目标
{
resultMap.put("flag", true);
resultMap.put("message", "相等了,game over!");
}
return resultMap;
}
public static void main(String[] args) {
// 开始猜数字
startTheGame(5, 100);
}
}
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
srand(time(NULL));
int num = rand() % 100 + 1;
int guess;
while (cin >> guess){
if (guess == num){
cout << "correct!";
break;
}
else if (guess < num) cout << "too small";
else cout << "too big";
cout << "\n";
}
}
水一贴
public class test {
public static void main(String args[]) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int aim = new Random().nextInt(100), in, count = 0; aim >= 0 && (in = Integer.parseInt(reader.readLine())) == in; count++)
System.out.println(in < aim ? "<" : in > aim ? ">" : "you win : " + (count + (aim = -1) + 2));
}
}
import java.util.Scanner;
public class MyTest {
public static void main(String[] args) {
int key = (int) (Math.random() * 100);// 生成随机数
int count = 0;// 统计次数
int answer;
Scanner sc = new Scanner(System.in);
while(true) {
count++;
System.out.println("输入第"+count+"个数字:");
answer = sc.nextInt();
if (key == answer) {
sc.close();
System.out.println("恭喜你答对了!一共猜了"+count+"次。");
break;
} else if (key > answer) {
System.out.println("对不起,你猜小了!");
} else {
System.out.println("对不起,你猜大了!");
}
}
}
}
import java.util.*;
class Try {
public static void main(String[] args) {
Random random1 = new Random();
int randomNumber=random1.nextInt(101);//获得随机数
System.out.println(randomNumber);
int count=0;//定义计数器
Scanner in=new Scanner(System.in); //使用Scanner类定义对象
System.out.println("请输入整形数字");
int a;
while (true){
a=in.nextInt(); //接收整形数据
count++;
if (a==randomNumber){
System.out.println("恭喜您猜中了");
break;
}
else if (a>randomNumber)
System.out.println("大了");
else
System.out.println("小了");
}
System.out.println("您一共猜了"+count+"次");
}
}
随便写写import java.util.Random;
import java.util.Scanner;
public class Guess {
public static void main(String[] args) {
int num = new Random().nextInt(100) + 1;
int userNum = input();
int counter = 0;
while (!check(num, userNum)) {
userNum = input();
counter++;
}
System.out.println("您猜对了,你一共猜了" + (counter + 1) + "次");
}
private static boolean check(int num, int userNum) {
if (userNum > num) {
System.out.println("您输入的数字太大了");
return false;
} else if (userNum < num) {
System.out.println("您输入的数字太小了");
return false;
}
return true;
}
private static int input() {
System.out.print("请输入一个数字(1-100):");
return new Scanner(System.in).nextInt();
}
}