java猜数字问题

java猜数字,如果猜对了,继续猜,一共猜5次,求代码。~

import java.util.Random;import java.util.Scanner;public class caishu {static int i = 0;static int p;public static void main(String[] args) {// TODO Auto-generated method stubp = (int) (Math.random() * 100);//System.out.println(p);while (i s) {System.out.println("你猜得数小了");} else {System.out.println("你猜得数大了");}} else {System.out.println("恭喜你猜对了!");return;}i++;} catch (Exception ex) {System.out.println("输入的数据有误!");}}System.out.println("你的猜数次数已经用完了!");}}运行结果

int num = (int)(Math.random()*100)+1;
Scanner sc = new Scanner(System.in);
int guessNum = -1;
while (guessNum != num) {
System.out.println("请输入1-100之间整数");
guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} elseif (guessNum < num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}

扩展资料:编写思路
1、成1-100之间随机数
(int)(Math.random()*100)+1;
提示用户输入数字,
Scanner sc=new Scanner(System.in);
int guessNum = sc.nextInt();
需要将随机数和用户输入的数字进行比较。
猜一次:
Scanner sc = new Scanner(System.in);
int num = (int)(Math.random()*100)+1;
System.out.println("请输入0-100之间整数");
int guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} elseif (guessNum < num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
二、使用while循环
publicstaticvoid main(String[] args) {
int num = (int)(Math.random()*100)+1;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入1-100之间整数");
int guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} elseif (guessNum < num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}
}
三、最后用while() 括号中的条件表达式,当用户猜测的数和系统生成的数字不相等时,就需要继续循环。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class gameFrame extends JFrame {
static int i = 1; // 记录猜数字次数
static int j = 20; // 记录剩余的猜数字次数
static int n = new Random().nextInt(100);
JPanel in = new JPanel(new GridLayout(1, 4));
// 输入文本框所在行面板,hp,tfin,buttonArea将加入此面板
JPanel hp = new JPanel(new BorderLayout());
// 标签help 将加入此面板;此面板将加入 in 面板
JLabel help = new JLabel("请输入0~100之间的数字:");
// 输入框前面的提示文本
JPanel tfin = new JPanel(new BorderLayout());
// 输入框将加入此面板,“确定”按钮将加入此面板;此面板将加入in 面板
JTextField input = new JTextField();
// 输入框
JButton ensure = new JButton("确定");
// 确定输入按钮
JPanel buttonsArea = new JPanel(new GridLayout(1, 2));
// 按键区(答案,重玩),此面板将加入in 面板
JPanel re = new JPanel(new BorderLayout());
// 重玩按钮区,repeat 按钮将加入此面板
JButton repeat = new JButton("重玩");
// 重玩按钮
JPanel an = new JPanel(new BorderLayout());
// answer 按钮区
JButton answer = new JButton("答案");
// answer 按钮
JTextArea a = new JTextArea();
// 提示文本显示区域
JPanel p = new JPanel(new BorderLayout());
// 文本域面板
static long startTime=System.currentTimeMillis();
gameFrame() {
// 构造方法
hp.add(help, BorderLayout.CENTER);
tfin.add(input, BorderLayout.CENTER);
tfin.add(ensure, BorderLayout.EAST);
re.add(repeat, BorderLayout.CENTER);
re.add(new JLabel(" "), BorderLayout.WEST);
an.add(answer, BorderLayout.CENTER);
an.add(new JLabel(" "), BorderLayout.WEST);
buttonsArea.add(re);
buttonsArea.add(an);
in.add(hp);
in.add(tfin);
in.add(buttonsArea);
this.add(in, BorderLayout.NORTH);
a.setEditable(false);
a.setLineWrap(true);
a.setWrapStyleWord(true);
a.setFont(new Font("font1", Font.BOLD, 15));
p.add(new JScrollPane(a));
this.add(p, BorderLayout.CENTER);
input.addActionListener(new MyMonitor());
ensure.addActionListener(new MyMonitor());
repeat.addActionListener(new MyMonitor2());
answer.addActionListener(new MyMonitor3());
this.addWindowListener(new MyWindowMonitor());
this.setBounds(400, 200, 550, 450);
this.setResizable(false);
this.setTitle("猜数字示例");
this.setVisible(true);
}
class MyWindowMonitor extends WindowAdapter { // X 监听器
public void windowClosing(WindowEvent e) {
setVisible(true);
System.exit(0);
}
}
class MyMonitor2 implements ActionListener { // repeat 重玩按钮监听器
public void actionPerformed(ActionEvent ex1) {
i = 1;
j=20;
a.setText("");
n = new Random().nextInt(100);
}
}
class MyMonitor3 implements ActionListener { // answer 按钮监听器
public void actionPerformed(ActionEvent ex2) {
String str1 = new String("正确答案是:" + n);
a.append(str1+"
" );
}
}
class MyMonitor implements ActionListener { // 确定按钮监听器
public void actionPerformed(ActionEvent e) {
int n1;
long endTime=System.currentTimeMillis();
long t=endTime-startTime;
String strshow=("您总共猜了"+i+"次,您猜数字总共花了"+((endTime-startTime)/1000)+"秒"+"
");
j--;
if (j == 0) {
a.append("还要继续,请点击“重玩”按钮!
");
j=20;
input.setText("");
return ;
}
else{
String str = input.getText();
input.setText("");
a.append(str +":");
try {
n1 = Integer.parseInt(str);
}catch (NumberFormatException e1) {
n1 = -1;
}
}
if (n1<=1 || n1>=100) {
a.append("无效输入!请重新输入吧!
");
return ;
}
else if (n1 == n) {
a.append("猜对了!
"+strshow);
if(t<10){
a.append("您真棒,智商真高!
"+strshow);
}
else{
a.append("这么久才猜出来,有点笨!
"+strshow);
}
j = 20;
a.append("还要继续,请点击“重玩”按钮!
");
}
else if (n1 > n) {
if (j == 0) {
String s = new String("猜大了!
还要继续,请点击“重玩”按钮!
"+strshow);
a.append(s);
i++;
}
else {
String s = new String("猜大了!
你还有" + j + "次机会!
"+strshow);
a.append(s);
i++;
}
}
else if (n1 < n) {
if (j== 0) {
String s = new String("猜小了!
还要继续,请点击“重玩”按钮!
"+strshow);
a.append(s);
i++;
}
else {
String s = new String("猜小了!
你还有" + j + "次机会!
"+strshow);
a.append(s);
i++;
}
}
}
}
}

大概给你改了下,bug还比较多啊。。主要是一个值的变化的问题,你自己想想吧。

main函数你就用你写的那个。

我主要给你稍微改了下变量修饰词,和代码位置。

还有System.currentTimeMillis()计算的是毫秒,不是秒



酱油,无能为力。

你想问啥?

这是小弟自己写的java猜数字小游戏,可是最後怎麼样都不会再猜错5次后...
答:do{ //1.获取键盘输入 //2. 比较数据,如果猜对了flag=true //3. 次数计数器+1 }while(循环执行的条件:次数计数器<=5 &&数字没匹配对 即flag=false )当跳出循环时候有两种情况,第一种情况次数>5,第二种,匹配对了(flag=true),可以在循环的时候设置一个布尔变量(flag),初始值为false...

java初学者题目,求详细解答
答:import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Random;public class TestNumber { private A a; public void setA(A a) { this.a = a; } public A getA() { return a; } public boolean judge(int num) { if (a.getV()...

java编程题,猜数字游戏本人初学者才学到第八章,使用循环和随机数就好了...
答:回答:汗,怎么有点像朗沃的题啊,不是朗沃的学生吧= =

编写猜数游戏的Java程序。 猜数游戏的规则如下: 1)系统产生一个[0,1...
答:import java.util.Random;import java.util.Scanner;public class GuessNumber { public static void main(String[] args) { Random rand = new Random();int number = rand.nextInt(100);//产生一个0-100间的随机数 Scanner in = new Scanner(System.in);//获取用户的输入 System.out.println...

java猜数字游戏?
答:import java.util.Scanner;/ Author: Cool_Wu Date: 2020-12-01 23:39 / public class GuessNumberGame { static int count = 0;static int answer = new Random().nextInt(100);public static void main(String[] args) throws Exception { System.out.println("猜数字游戏开始,该数字是一...

用Java编程实现一个猜数字的游戏:系统随机产生一个1~100的数字,然后让...
答:2.用一个变量去接收玩家输入的数,例如int i;3.用if语句,当玩家输入的数与随机数相等时(i==num),则输出语句(恭喜你猜对了).4.用else写出猜错是的语句.大致思路就是这样.建议你自己去写,只有自己写过才会懂.如有不懂的M我,本人也在学习中,大家可以相互讨论讨论.另外你可以把数字设置成1~10;...

java 编写生成一个10 20 (包括10和20)的随机整数,然后在输入文本框输...
答:1、猜数字游戏 import java.util.InputMismatchException;import java.util.Scanner;public class Main { public static void main(String[] args) { // 产生一个随机数 int number = (int) (Math.random() * 100) + 1;// 加入count int count = 0;// 在这里加入最大值,和最小值 int ...

猜数字游戏0-1000之间,用JAVA编程
答:这简单,代码如下,这是我用记事本写的,没运行过可能有点问题,你修改下就可以了,创建个类以Number 命名,然后直接复制粘贴就可以了。希望能帮到你!import java.util.Scanner;public class number { public static void main(String[] args) {do{ int i = 0; int random = (int) (Math....

JAVA 设计猜数字小游戏
答:1、猜10次的问题可以设一个计数器,如‘b',while(b<10){...}这样猜错一次计数器加一,当计数器等于10的时候不再while循环,退出猜数,然后System输出’游戏结束‘字样。2、加入重新再来和退出游戏可以将程序分为两步份,面版代码while死循环,不断打印规则并scanner获取用户输入的选择(如Y重新再来...

java中四个不重复的数字猜数字游戏
答:num();// 继续猜下一个游戏 } else if (userNum > gameNum) { System.out.println("数字有点大噢/(ㄒoㄒ)/~~");} else if (userNum < gameNum) { System.out.println("数字小了点噢(*^__^*) 嘻嘻……");} } } public static int num() { List<Integer> list = new ...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网