java猜数字游戏实验报告(用JAVA编猜数字游戏)

本文目录
用JAVA编猜数字游戏
1)
程序随机分配给客户一个1—100之间的整数
Random
gen
=
new
Random();
int
a
=
gen.nextInt(100)+1;
//不加1是0到99
2)
用户在输入对话框中输入自己的猜测
Scanner
in
=
new
Scanner(System.in);
System.out.println("give
me
a
number")
int
b
=
in.nextInt();
3)
程序返回提示信息,提示信息分别是:“猜大了”、“猜小了”和“猜对了”。
if(b》a)
System.out.println("big");
else
if(b《a)
System.out.println("small");
else
System.out.println("right");
4)
用户可根据提示信息再次输入猜测,直到提示信息是“猜对了”。
while(b!=a){
if(b》a)
System.out.println("big");
else
if(b《a)
System.out.println("small");
System.out.println("give
me
a
new
number")
int
b
=
in.nextInt();
}
System.out.println("right");
3看懂以后,3,4可以合在一起,用4的回答.
java语言实现猜数问题
import java.util.Random;
import java.util.Scanner;
public class TestMain {
public static void main(String args) {
// TODO Auto-generated method stub
String str = getRandom();
System.out.println("获取的随机数为:"+str);
Scanner sca = new Scanner(System.in);
String res = "";
int guessTime = 0;
while(!res.equals("AAAA")){
guessTime ++;
System.out.println("请输入的4个0到9之间的自然数");
String numStr = sca.next();
if(numStr.length()》4){
System.out.println("数据长度不符合,请输入的4个0到9之间的自然数");
numStr = sca.next();
}
res = guess(str,numStr);
String appStr = "";
if(res.equals("AAAA")){
appStr = "。恭喜您,猜对了。一共猜了"+guessTime+"次。地球已经无法阻止你。";
}else{
appStr = "。亲爱的小伙伴请继续加油。";
}
System.out.println("您本次猜数字的结果为:"+res+appStr);
}
}
public static String guess(String random,String numStr){
String res = "";
char ranChar = random.toCharArray();
char guessChar = numStr.toCharArray();
for(int i=0;i《ranChar.length;i++){
for(int j=0;j《guessChar.length;j++){
if(ranChar){
if(i==j){
res+="A";
}else{
res+="B";
}
continue;
}
}
}
if(res.equals("")){
res = "无成绩";
}
return res;
}
//获取随机数
public static String getRandom(){
Random ran = new Random();
String res = "";
int s;
int num = 0;
while(res.length()《4){
num = ran.nextInt(10);
if(!res.contains(String.valueOf(num))){
res += String.valueOf(num);
}
}
return res;
}
}
代码已经写好,望采纳。
用java来写一个猜数字游戏,要用到界面
package p1;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class MyFrame extends JFrame
{
private static final long serialVersionUID = 1L;
int random = 0;
public static void main ( String args )
{
new MyFrame ();
}
public MyFrame ()
{
Container container = getContentPane ();
container.setLayout (new BoxLayout (container, BoxLayout.Y_AXIS | BoxLayout.LINE_AXIS));
container.setBackground (Color.pink);
setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
setSize (300, 200);
setLocationRelativeTo (null);
setResizable (false);
setTitle ("输入1到10的数字猜");
JPanel p = new JPanel ();
final JTextField jtf = new JTextField (20);
jtf.setToolTipText ("输入1到10的数字猜");
p.add (jtf);
container.add (p);
JPanel panel = new JPanel ();
final JButton bl = new JButton ("开始");
bl.addActionListener (new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
random = (int) ( Math.random () * 10 + 1 );
bl.setEnabled (false);
}
});
panel.add (bl);
JButton bl1 = new JButton ("确定");
bl1.addActionListener (new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
String num = jtf.getText ().trim ();
if (!num.matches ("^|10$"))
{
JOptionPane.showConfirmDialog (null, "请填入一个 1~10 的整数 !", "友情提示", JOptionPane.CLOSED_OPTION,
JOptionPane.ERROR_MESSAGE);
}
else
{
if (random == Integer.parseInt (num))
{
JOptionPane.showConfirmDialog (null, "恭喜你,猜对了 !", "友情提示", JOptionPane.CLOSED_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showConfirmDialog (null, "很遗憾,你猜错了 ! 随机数是:" + random, "友情提示",
JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
}
}
jtf.requestFocus ();
bl.setEnabled (true);
}
});
panel.add (bl1);
JButton bl2 = new JButton ("退出");
bl2.addActionListener (new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
System.exit (0);
}
});
panel.add (bl2);
container.add (panel);
JLabel jl = new JLabel ();
container.add (jl);
setVisible (true);
}
}

更多文章:
java mq消息队列详解(java如何获取rabbitmq队列中消息数量)
2026年5月11日 19:30
号码随机抽取器(如何用vb设计摇奖器(抽手机号的,而且要在已知的号码内抽取))
2025年7月19日 19:00
journalism形容词(J字母开头的英语单词有哪些最少十个!把意思带上!)
2026年7月10日 05:30
jsp简介内容对应的的论文参考文献(有关计算机的论文参考文献)
2026年9月1日 21:15
dm数据库连接工具(如何使用dmprovider链接数据库)
2026年1月20日 17:15
onmouseover在js中用法(javascript给一个html标签添加onmouseover事件)
2026年5月29日 14:30
stackoverflow 中文(overflow是什么意思)
2025年9月4日 07:00
docker和虚拟机的区别 一句话总结(docker容器和虚拟机的区别)
2025年6月17日 10:45
java课程设计及源码(急求java 计算器课程设计报告,有源码,)
2026年2月10日 07:00
滑块轴承属于什么类目(滑块轴承字母及数字的含义是什么,如SCS13UU)
2025年6月11日 14:00
git clone branch(git里面怎么看local branch和remote branch的关系)
2025年6月4日 06:00
格行显示assert(计算机开机时显示assert at file: proc.c 是什么问题)
2026年4月7日 14:45
泛型有什么用(一般情况下,集合中为什么要使用泛型不使用泛型的情况下,集合中的元素是什么类型)
2025年9月11日 00:45
用css做页面特效的软件(什么软件编辑div+css网页能够像frontpage哪样即时看效果)
2026年9月6日 20:45
erp管理系统开发(在企业中该怎么实施ERP系统(企业如何实施erp管理))
2026年5月17日 07:45
最新nba排名赛程(nba赛程安排(2021-2022赛季))
2026年5月21日 15:15
ssl连接失败怎么解决(电脑中出现浏览器上不了网提示SSL安全连接失败如何解决)
2026年3月27日 18:30







