Java猜数字游戏代码

导入javax . swing . ui manager;

公共类MainClass

{

公共主类()

{

}

公共静态void main(String args[])

{

尝试

{

ui manager . setlookandfeel(" com . sun . Java . swing . plaf . windows . windows lookandfeel ");

}

catch(异常异常){ }

new NumGuessing();

}

}

导入Java . util . ArrayList;

公共类方法

{

公共方法()

{

}

public static int[] RandomNum()

{

int guess num[]= new int[4];

ArrayList Temp = new ArrayList();

for(int I = 0;我& lt4;i++)

guess num[I]=-1;

for(int I = 0;我& lt4;i++)

{

{

float tempNum =(float)math . random()* 10F;

if((double)tempNum & gt;= 9.5D)

guess num[I]= 0;

其他

guess num[I]= math . round(tempNum);

} while(temp . contains(integer . value of(guess num[I]));

temp . add(integer . value of(guess num[I]));

}

返回guessNum

}

公共静态布尔CheckIsFourNum(String enter String)

{

boolean IsFourNum = true

尝试

{

integer . value of(enter string);

}

catch (NumberFormatException e)

{

IsFourNum = false

}

if (enterString.length()!= 4)

IsFourNum = false

return IsFourNum

}

公共静态字符串GetResult(int Num[],String enterString)

{

字符串resultString =

int rightInAll = 0;

int rightOnlyInNum = 0;

for(int I = 0;我& lt4;i++)

{

if(Num[3-I]= = integer . value of(enter string . substring(I,i + 1))。intValue())

riginall++;

for(int j = 0;j & lt4;j++)

if(Num[I]= = integer . value of(enter string . substring(j,j + 1))。intValue())

rightonlyinnum++;

}

result string =(new StringBuilder(string . value of(rightInAll)))。追加(" A ")。append(rightOnlyInNum)。追加(" B ")。toString();

返回结果字符串;

}

public static boolean is guessed(String guess String,String guessedStrings)

{

boolean isGuessed = false

for(int I = 0;我& ltguessedStrings.lengthi++)

if(guess string . equals(guessed strings[I]))

isGuessed = true

退货被拒绝;

}

公共静态布尔HasSameNum(字符串guessString)

{

boolean hasSameNum = false

for(int I = 0;我& ltguess string . length();i++)

{

for(int j = 0;j & ltguess string . length();j++)

{

if (i == j ||!guessString.substring(i,i + 1)。equals(guessString.substring(j,j + 1)))

继续;

hasSameNum = true

打破;

}

}

返回hasSameNum

}

}

导入Java . awt . *;

导入Java . awt . event . action event;

导入Java . awt . event . action listener;

导入javax . swing . *;

公共类编号

实现ActionListener

{

私有JFrame框架;

私人JPanel中心JPanel

private JPanel southJPanel

私人JTextArea showArea

二等兵JTextField guessField

私有JButton commitJButton

私有字体字体;

私有布尔isOver

private int guessTimeCount

private int guessing num[];

私有字符串guessedNum[];

公共编号()

{

isOver = false

guest imecount = 0;

guessedNum = new String[10];

frame = new JFrame(" num guessing ");

frame.setSize(200,300);

frame . set location((toolkit . getdefaulttoolkit()。getScreenSize()。width - frame.getSize()。width) / 2,(Toolkit.getDefaultToolkit()。getScreenSize()。height - frame.getSize()。身高)/2);

font =新字体(null,2,15);

center JPanel = new JPanel();

south JPanel = new JPanel();

ShowArea = new JTextArea("以下是你猜测的结果:\ n ");

showArea.setFont(字体);

showarea . set enabled(false);

guess field = new JTextField(6);

guess field . set font(font);

commit JButton = new JButton(" Enter ");

commit button . set font(font);

frame . set layout(new BorderLayout());

centerJPanel.setLayout(新GridLayout(1,2));

center jpanel . add(showArea);

south jpanel . add(guess field);

south jpanel . add(commit jbutton);

frame.add(centerJPanel," Center ");

frame.add(southJPanel," South ");

frame.validate()。

frame . setresizable(false);

frame . set visible(true);

frame . setdefaultcloseoperation(3);

commit button . addactionlistener(this);

guessingNum =方法。random num();

}

公共无效操作已执行(操作事件e)

{

string guess string = guess field . gettext();

如果(!伊索弗& amp& ampguessTimeCount & lt10)

{

if(方法。CheckIsFourNum(guessString))

{

如果(!方法。IsGuessed(guessString,guessedNum))

{

如果(!方法。HasSameNum(guessString))

{

showarea . append((new StringBuilder(string . value of(guess string)))。追加(" ")。toString());

showarea . append((new StringBuilder(string . value of(Methods。GetResult(guessingNum,guessString))))。追加(" \n ")。toString());

guess field . settext(null);

guess ednum[guessTimeCount]= guess string;

guest imecount++;

if(方法。GetResult(guessingNum,guessString)。等于(" 4A4B "))

{

isOver = true

joptionpane . showmessagedialog(null,“你是赢家”);

}

if(guessTimeCount & gt;= 10)

{

isOver = true

Joption窗格。显示消息对话框(null,“你应该猜了十次都输了!”);

guess field . settext(null);

}

}否则

{

guess field . settext(null);

Joptionpane。显示消息对话框(空,“您输入的号码有相同的号码,请重新填写。”);

}

}否则

{

guess field . settext(null);

Joption窗格。显示消息对话框(空,“你已经猜到这个数字!”);

}

}否则

{

guess field . settext(null);

Joptionpane。显示消息对话框(空,“请输入一个四位数”);

}

}否则

{

guess field . set enabled(false);

}

}

}