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
{
public Methods()
{
}
public static int[] RandomNum()
{
int guessNum[] = new int[4];
ArrayList Temp = new ArrayList();
for (int i = 0; i < 4; i++)
guessNum[i] = -1;
for (int i = 0; i < 4; 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; i < 4; i++)
{
if (Num[3 - i] == Integer.valueOf(enterString.substring(i, i + 1)).intValue())
rightInAll++;
for (int j = 0; j < 4; 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; i < guessedStrings.length; i++)
if (guessString.equals(guessedStrings[i]))
isGuessed = true;
return isGuessed;
}
public static boolean HasSameNum(String guessString)
{
boolean hasSameNum = false;
for (int i = 0; i < guessString.length(); i++)
{
for (int j = 0; j < guessString.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 (!isOver && guessTimeCount < 10)
{
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);
}
}
}
int gameNum = num();// 系统生成的四位不重复数字
while(true){
Scanner sc = new Scanner(System.in);// 键盘输入
int userNum = sc.nextInt();
if (gameNum == userNum) {// 如果用户输入等于系统生成,执行下面的代码
System.out.println("恭喜你猜对了O(∩_∩)O哈哈~");
num();// 继续猜下一个游戏
} else if (userNum > gameNum) {
System.out.println("数字有点大噢/(ㄒoㄒ)/~~");
} else if (userNum < gameNum) {
System.out.println("数字小了点噢(*^__^*) 嘻嘻……");
}
}
}
public static int num() {
List<Integer> list = new ArrayList<Integer>();// 首先建立个list集合
String str = "";
list.add(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.add(9);
Random rand = new Random();
int a = rand.nextInt(list.size());// 随机获取list下标
for (int i = 0; i < list.size(); i++) {// for循环
if (list.get(a) == list.get(i)) {
str = str + list.get(i);// 拼接字符串
list.remove(i);
}
}
int b = rand.nextInt(list.size() - 1);
for (int i = 0; i < list.size(); i++) {
if (list.get(b) == list.get(i)) {
str = str + list.get(i);
list.remove(i);
}
}
int c = rand.nextInt(list.size() - 1);
for (int i = 0; i < list.size(); i++) {
if (list.get(c) == list.get(i)) {
str = str + list.get(i);
list.remove(i);
}
}
int d = rand.nextInt(list.size() - 1);
for (int i = 0; i < list.size(); i++) {
if (list.get(d) == list.get(i)) {
str = str + list.get(i);
list.remove(i);
}
}
int num = Integer.valueOf(str);// String转化成int
return num;
}
免责声明:本平台仅供信息发布交流之途,请谨慎判断信息真伪。如遇虚假诈骗信息,请立即举报
举报