MathGame

Java猜数小游戏

玩法:输入1-100以内的随机数,系统会判断偏大或者偏小,然后重新输入直至输入与系统给出的随机数一致时,程序结束运行,程序会统计你一共用了多少次猜对结果。

Tips:猜数方法正确的话7次就能猜对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner;

public class MathGame {

```
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number = (int)(Math.random()*100+1);
int a,count = 0;
do
{
a =in.nextInt();
count = count + 1;
if(a>number)
{
System.out.println("偏大");
}
else if(a<number)
{
System.out.println("偏小");
}
}while( a !=number );
System.out.println("恭喜你猜对了,一共猜了"+count+"次");
}
```

}
-------------本文结束感谢您的阅读-------------