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, i + 1)).intValue())
rightInAll++;
for (int j = 0; j4; j++)
if (Num[i] == Integer.valueOf(enterString.substring(j, j + 1)).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, i + 1).equals(guessString.substring(j, j + 1)))
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());
推荐阅读
- 各种卡片制作器下载安卓,卡片制作方法
- 笔电怎么看显卡瓦数,笔记本显卡怎么看多少瓦
- flutter穿戴应用,flutter platform
- 华硕路由器怎么查看账号,华硕路由器怎么查看账号密码
- mysql怎么插入表记录 mysql怎么添加记录
- sqlserver2列和成一列,sqlserver将列拼接
- 好看的动漫游戏桌面,超好玩的动漫游戏
- 头条怎样可以带货直播卖货,怎么在头条直播卖货
- go语言实习文案朋友圈 go语言培训