java猜数字游戏代码import javax.swing.UIManager;
public class MainClass
{
public MainClass()
{
}
public static void main(String args[])
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception exception) { }
new NumGuessing();
}
}
import java.util.ArrayList;
public class Methods
【java猜字游戏代码 javabean猜数字的游戏】{
public Methods()
{
}
public static int[] RandomNum()
{
int guessNum[] = new int[4];
ArrayList Temp = new ArrayList();
for (int i = 0; i4; i)
guessNum[i] = -1;
for (int i = 0; i4; i)
{
do
{
float tempNum = (float)Math.random() * 10F;
if ((double)tempNum = 9.5D)
guessNum[i] = 0;
else
guessNum[i] = Math.round(tempNum);
} while (Temp.contains(Integer.valueOf(guessNum[i])));
Temp.add(Integer.valueOf(guessNum[i]));
}
return guessNum;
}
public static boolean CheckIsFourNum(String enterString)
{
boolean IsFourNum = true;
try
{
Integer.valueOf(enterString);
}
catch (NumberFormatException e)
{
IsFourNum = false;
}
if (enterString.length() != 4)
IsFourNum = false;
return IsFourNum;
}
public static String GetResult(int Num[], String enterString)
{
String resultString = "";
int rightInAll = 0;
int rightOnlyInNum = 0;
for (int i = 0; i4; i)
{
if (Num[3 - i] == Integer.valueOf(enterString.substring(i, i1)).intValue())
rightInAll;
for (int j = 0; j4; j)
if (Num[i] == Integer.valueOf(enterString.substring(j, j1)).intValue())
rightOnlyInNum;
}
resultString = (new StringBuilder(String.valueOf(rightInAll))).append("A").append(rightOnlyInNum).append("B").toString();
return resultString;
}
public static boolean IsGuessed(String guessString, String guessedStrings[])
{
boolean isGuessed = false;
for (int i = 0; iguessedStrings.length; i)
if (guessString.equals(guessedStrings[i]))
isGuessed = true;
return isGuessed;
}
public static boolean HasSameNum(String guessString)
{
boolean hasSameNum = false;
for (int i = 0; iguessString.length(); i)
{
for (int j = 0; jguessString.length(); j)
{
if (i == j || !guessString.substring(i, i1).equals(guessString.substring(j, j1)))
continue;
hasSameNum = true;
break;
}
}
return hasSameNum;
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class NumGuessing
implements ActionListener
{
private JFrame frame;
private JPanel centerJPanel;
private JPanel southJPanel;
private JTextArea showArea;
private JTextField guessField;
private JButton commitJButton;
private Font font;
private boolean isOver;
private int guessTimeCount;
private int guessingNum[];
private String guessedNum[];
public NumGuessing()
{
isOver = false;
guessTimeCount = 0;
guessedNum = new String[10];
frame = new JFrame("NumGuessing");
frame.setSize(200, 300);
frame.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - frame.getSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - frame.getSize().height) / 2);
font = new Font(null, 2, 15);
centerJPanel = new JPanel();
southJPanel = new JPanel();
showArea = new JTextArea("以下是你猜数字的结果:\n");
showArea.setFont(font);
showArea.setEnabled(false);
guessField = new JTextField(6);
guessField.setFont(font);
commitJButton = new JButton("Enter");
commitJButton.setFont(font);
frame.setLayout(new BorderLayout());
centerJPanel.setLayout(new GridLayout(1, 2));
centerJPanel.add(showArea);
southJPanel.add(guessField);
southJPanel.add(commitJButton);
frame.add(centerJPanel, "Center");
frame.add(southJPanel, "South");
frame.validate();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
commitJButton.addActionListener(this);
guessingNum = Methods.RandomNum();
}
public void actionPerformed(ActionEvent e)
{
String guessString = guessField.getText();
if (!isOverguessTimeCount10)
{
if (Methods.CheckIsFourNum(guessString))
{
if (!Methods.IsGuessed(guessString, guessedNum))
{
if (!Methods.HasSameNum(guessString))
{
showArea.append((new StringBuilder(String.valueOf(guessString))).append("").toString());
showArea.append((new StringBuilder(String.valueOf(Methods.GetResult(guessingNum, guessString)))).append("\n").toString());
guessField.setText(null);
guessedNum[guessTimeCount] = guessString;
guessTimeCount;
if (Methods.GetResult(guessingNum, guessString).equals("4A4B"))
{
isOver = true;
JOptionPane.showMessageDialog(null, "You Are The Winner");
}
if (guessTimeCount = 10)
{
isOver = true;
JOptionPane.showMessageDialog(null, "你应经猜了十次 , 输了!");
guessField.setText(null);
}
} else
{
guessField.setText(null);
JOptionPane.showMessageDialog(null, "你输入的数中有相同的数字,请重新填写 。");
}
} else
{
guessField.setText(null);
JOptionPane.showMessageDialog(null, "这个数字你已经猜过了!");
}
} else
{
guessField.setText(null);
JOptionPane.showMessageDialog(null, "请输入一个四位数字");
}
} else
{
guessField.setEnabled(false);
}
}
}
猜数字游戏java,要用到GUI图形界面,且运行要有次数限制测试效果如图
参考java代码和注释如下
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class GuessNumFrame extends JFrame {
private JPanel contentPane;
private int num;
private final JTextField jtfInput;
private final JLabel label;
static final int MAX_TIMES = 5; // 最多可以尝试5次
private int times = 0;// 记录尝试次数
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuessNumFrame frame = new GuessNumFrame();
frame.setVisible(true);// 窗口可见
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GuessNumFrame() {
setTitle("猜数字");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(360, 160);// 窗口大小
setLocationRelativeTo(null);// 居中
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new GridLayout(3, 1, 0, 0));
JPanel panel = new JPanel();
panel_1.add(panel);
JButton jbNum = new JButton("得到一个随机数");
panel.add(jbNum);
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
label = new JLabel("");
label.setOpaque(true);
label.setBackground(Color.CYAN);
panel_2.add(label);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
jtfInput = new JTextField();
panel_3.add(jtfInput);
jtfInput.setColumns(10);
JButton jbOk = new JButton("确定");
jbOk.setEnabled(false);
panel_3.add(jbOk);
jbNum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num = (int) (Math.random() * 10);// 生成随即数
times = 0; // 猜测次数归零
label.setText("随机数已经生成范围0~9");
jtfInput.setText(null);// 输入框清空
jbOk.setEnabled(true);// 按钮恢复可用
System.out.println(num);
}
});
jbOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x;
try {
x = Integer.parseInt(jtfInput.getText());// 取得输入框的数字 进行比较
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "输入格式错误!请输入数字", "提示", JOptionPane.WARNING_MESSAGE);
return;//输入字母等非整数,直接返回
}
if (x == num) {
JOptionPane.showMessageDialog(null, "厉害,猜对了~!");
jbOk.setEnabled(false);//按钮设置为不可用
return;//猜对了也返回
} else if (xnum) {
JOptionPane.showMessageDialog(null, "你猜小了", "提示", JOptionPane.WARNING_MESSAGE);
} else if (xnum) {
JOptionPane.showMessageDialog(null, "你猜大了", "提示", JOptionPane.WARNING_MESSAGE);
}
times;
if (times = MAX_TIMES) {
JOptionPane.showMessageDialog(null, "尝试次数过多失败", "提示", JOptionPane.WARNING_MESSAGE);
jbOk.setEnabled(false);//按钮设置为不可用
return;// 猜错次数太多也返回
}
}
});
}
}
用java编写一个猜字游戏程序,答案不限 。急?。。?/h2>public static void main(String[] args){
Random r = new Random();
Scanner sc = new Scanner(System.in);
//100以内
int num = r.nextInt(100);
System.out.println(num);
int gsNum=-1;
do{
System.out.println("请输入要猜的数字:");
gsNum = sc.nextInt();
if(numgsNum){
System.out.println("太小啦");
}else if(numgsNum){
System.out.println("太大啦");
}
}while(num!=gsNum);
System.out.println("正确");
}
用JAVA语言编写一个“猜数字游戏”的程序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 (guessNumnum) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}
扩展资料:
编写思路
1、成1-100之间随机数
(int)(Math.random()*100) 1;
提示用户输入数字 ,
Scannersc=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 (guessNumnum) {
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 (guessNumnum) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}
}
三、最后用while() 括号中的条件表达式,当用户猜测的数和系统生成的数字不相等时,就需要继续循环 。
java猜数字游戏怎么怎么做到在游戏结束后输入y重新开始输入n退出游戏?下面是源代码while(true){
//你的程序
System.out.println("再玩一次?");
String st= scan.next();
if( st=="y"){continue;}
if( st=="n"){break;}
System.out.println("既不是y也不是n , 出错");
}
关于java猜字游戏代码和javabean猜数字的游戏的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 宽带怎么连接旧老路由器,宽带怎么连接旧老路由器设置
- 视频号没有视频怎么办苹果,视频号没有视频怎么办苹果12
- 阿里云服务器内网集群,阿里云服务器集群方案
- 血性竞技游戏,有血性的游戏名字
- 什么浏览器集成chatgpt,什么浏览器最全
- python爬虫b站去水印,python b站爬虫
- 皮裤新款直播带货,皮裤博主搭配
- c语言如何封装共同函数 封装一个函数,函数的功能是实现两个变量的交换代码
- mysql字段去重复数据,mysql删除某个字段重复数据