
正文
java+猜字代码 java猜字母
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java窗体猜数字代码
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class caishuzi extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private JPanel textpanel, buttonpanel, fopanel, downpanel;
private JButton btn;
private JTextField jtinput, jttishi;
private JLabel lbnum0, lbnum, lbtishi, lbinput;
private JTextArea tafo;
private Boolean start = false, jisuan = false, jinru = false;
MenuBar mb = new MenuBar();
Menu filemenu = new Menu("文件"), systemmenu = new Menu("系统"),
helpmenu = new Menu("帮助");
MenuItem newfile = new MenuItem("文件"), openfile = new MenuItem("打开"),
closefile = new MenuItem("关闭"), quit = new MenuItem("退出"),
inputsecret = new MenuItem("输入密码"), help = new MenuItem("帮助"),
inputcishu = new MenuItem("输入限制次数");
char[] c = new char[4];
String strsecret = "123";
int xianzhi=8;
caishuzi()
{
super("猜数字");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setLocationRelativeTo(null);
// 设置菜单
filemenu.add(newfile);
filemenu.add(openfile);
filemenu.addSeparator();
filemenu.add(closefile);
newfile.setEnabled(false);
openfile.setEnabled(false);
closefile.setEnabled(false);
inputsecret.addActionListener(this);
inputcishu.addActionListener(this);
inputcishu.setEnabled(false);
systemmenu.add(inputsecret);
systemmenu.add(inputcishu);
systemmenu.add(quit);
quit.addActionListener(this);
help.addActionListener(this);
helpmenu.add(help);
mb.add(filemenu);
mb.add(systemmenu);
mb.add(helpmenu);
this.setMenuBar(mb);
Container c = this.getContentPane();
BorderLayout b = new BorderLayout();
b.setVgap(10);
c.setLayout(b);
settextpanel();
c.add(textpanel, BorderLayout.NORTH);
setbuttonpanel();
setfopanel();
setdownpanel();
c.add(downpanel, BorderLayout.CENTER);
this.setSize(680,350);
//this.setResizable(false);
//this.pack();
this.setVisible(true);
}
void settextpanel()
{
textpanel = new JPanel();
textpanel.setLayout(new FlowLayout());
lbinput = new JLabel("输入:");
jtinput = new JTextField("", 10);
jtinput.setEditable(false);
jttishi = new JTextField(30);
lbnum = new JLabel("8");
lbnum0 = new JLabel("剩余次数:");
lbtishi = new JLabel("提示:");
jttishi.setEditable(false);
textpanel.add(lbinput);
textpanel.add(jtinput);
textpanel.add(lbtishi);
textpanel.add(jttishi);
textpanel.add(lbnum0);
textpanel.add(lbnum);
}
void setbuttonpanel()
{
String[] str =
{ "7", "8", "9", "4", "5", "6", "1", "2", "3", "确定", "0", "退格" };
buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(4, 3, 4, 8));
for (int i = 0; i str.length; i++)
{
btn = new JButton(str[i]);
btn.addActionListener(this);
buttonpanel.add(btn);
}
}
void setfopanel()
{
JButton btnright = new JButton("正确答案");
JPanel p = new JPanel();
btnright.addActionListener(this);
fopanel = new JPanel();
fopanel.setLayout(new BorderLayout());
btn = new JButton("开始");
btn.addActionListener(this);
tafo = new JTextArea(15,12);
p.setLayout(new GridLayout(1, 2));
p.add(btn);
p.add(btnright);
fopanel.add(p, BorderLayout.NORTH);
tafo.setEditable(false);
fopanel.add(tafo, BorderLayout.CENTER);
}
void setdownpanel()
{
downpanel = new JPanel();
downpanel.setLayout(new GridLayout(1, 2, 30, 0));
downpanel.add(buttonpanel);
downpanel.add(fopanel);
}
public static void main(String[] args)
{
new caishuzi();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("退出"))
{
int i = JOptionPane.showConfirmDialog(this, "确认退出?", "",
JOptionPane.YES_NO_OPTION);
//System.out.println(i);
if (i == 0)
{
System.exit(0);
}
}
if (s.equals("帮助"))
{
JOptionPane.showMessageDialog(this,
"猜数字游戏,可以输入限制次数,密码为123,\n在菜单:系统——输入密码 中输入!");
}
if (s.equals("输入密码"))
{
while (true)
{
String inputValue = "";
inputValue = JOptionPane.showInputDialog(this, "输入密码");
// System.out.println(inputValue);
if (inputValue == null)
return;
if (inputValue.equals(strsecret))
{
jinru = true;
jttishi.setText("密码正确,点击开始");
inputsecret.setEnabled(false);
break;
} else
{
int i = JOptionPane.showConfirmDialog(this, "密码错误,重新输入?",
"", JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
}
}
if (!jinru)
{
jttishi.setText("请输入密码:");
return;
}
if (s.equals("开始") || s.equals("重玩"))
{
StringBuffer sb = new StringBuffer("0123456789");
for (int i = 0; i c.length; i++)
{
int n = (int) (Math.random() * sb.length());
c[i] = sb.charAt(n);
sb.deleteCharAt(n);
}
btn.setText("重玩");
start = true;
lbnum.setText(String.valueOf(xianzhi));
jisuan = false;
//System.out.println(c);
jttishi.setText("请输入四不重复的数:");
jtinput.setText("");
tafo.setText("");
}
if (s.compareTo("0") = 0 s.compareTo("9") = 0)
{
jisuan = false;
if (!start)
jttishi.setText("点击开始!");
else
{
if (jtinput.getText().length() 4)
{
if (jtinput.getText().indexOf(s) 0)
{
jtinput.setText(jtinput.getText() + s);
if (jtinput.getText().length() 4)
jttishi.setText("还差"
+ (4 - jtinput.getText().length()) + "个数");
else
{
jttishi.setText("输入正确,点击确定");
jisuan = true;
}
} else
jttishi.setText("不能输入重复的数");
} else
{
jttishi.setText("不能超过四个数,点击确定");
jisuan = true;
}
}
}
if (s.equals("退格") start)
{
if (jtinput.getText().length() 0)
{
jtinput.setText(jtinput.getText().substring(0,
jtinput.getText().length() - 1));
jttishi.setText("还差" + (4 - jtinput.getText().length()) + "个数");
}
jisuan = false;
}
if (s.equals("确定"))
{
if (!start)
{
jttishi.setText("还没开始呢!");
return;
}
if (!jisuan)
{
jttishi.setText("输入不正确!");
return;
}
int numa = 0, numb = 0;
char[] c1 = jtinput.getText().toCharArray();
// System.out.println(c1);
for (int i = 0; i c1.length; i++)
{
for (int j = 0; j c.length; j++)
{
if (c1[i] == c[j] i != j)
{
numb++;
}
if (c1[i] == c[j] i == j)
{
numa++;
}
}
}
tafo.append(jtinput.getText() + "\t" + numa + "A" + numb + "B"
+ "\n");
jtinput.setText("");
jisuan = false;
if (numa == 4)
{
jttishi.setText("答对了,再来一局吧!");
start = false;
btn.setText("开始");
tafo.append("恭喜,答对了! ");
} else
{
lbnum.setText(String
.valueOf(Integer.valueOf(lbnum.getText()) - 1));
if (Integer.valueOf(lbnum.getText()) == 0)
{
String str = "";
jttishi.setText("机会没有了!很遗憾");
for (int i = 0; i c.length; i++)
str += c[i];
tafo.append("超过限制次数,失败!" + "正确答案是:" + str);
start = false;
btn.setText("开始");
} else
jttishi.setText("输入四个数:");
}
}
if (s.equals("正确答案"))
{
if (!start)
{
jttishi.setText("还没开始呢!");
return;
}
String str = "";
for (int i = 0; i c.length; i++)
str += c[i];
tafo.append("正确答案是:" + str);
jttishi.setText("重新开始吧!");
start = false;
jisuan = false;
btn.setText("开始");
}
if (!start)
{
inputcishu.setEnabled(true);
} else
inputcishu.setEnabled(false);
if (s.equals("输入限制次数"))
{
while (true)
{
try
{
String str = JOptionPane.showInputDialog(this,
"输入小于15的整数数字:");
if (str == null)
return;
if (Integer.valueOf(str) = 15 Integer.valueOf(str) 0)
{
xianzhi=Integer.valueOf(str);
lbnum.setText(String.valueOf(xianzhi));
break;
} else
{
int i = JOptionPane.showConfirmDialog(this, "输入错误,重试?",
"", JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
} catch (Exception ex)
{
int i = JOptionPane.showConfirmDialog(this, "输入错误,重试?", "",
JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
}
}
}
}
我做课程设计的最初代码 参考下吧 运行时先看帮助~
相关问答
Q1: 兄弟帮忙看看我的java代码 老提示找不到符号
我只是在你的程序上加了打印,是可以看到程序本身并没有错误的。。。
import java.util.*;
public class Czyx
{
public static void main(String[] args)
{
System.out.println("请输入一个1-100之间的整数,谢谢!");
int a,k,j;
a=sr();
System.out.println("a="+a);
j=(int)(Math.random()*100);
System.out.println("j="+j);
for(k=1;k=10;k++)
{
if(aj)
{
System.out.println("good.aj 了!~~");
a=sr();
continue;
}
if(aj)
{
System.out.println("太大了!这是第"+k+"次输入!");
a=sr();
continue;
}
else
System.out.println("Good!你获得了"+(100-(k-1)*10)+"分!");
break;
}
}
public static int sr()
{
int b;
Scanner a=new Scanner(System.in);
b=a.nextInt();
while(b0||b100)
{
System.out.println("输入的数字有误,请重新输入.");
b=a.nextInt();
}
System.out.println("b="+b);
return b;
}
}
Q2: 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
{
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);
}
}
}
Q3: 用java编写一个猜字游戏程序,答案不限。急!!!
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("请输入要猜java+猜字代码的数字java+猜字代码:");
gsNum = sc.nextInt();
if(numgsNum){
System.out.println("太小啦");
}else if(numgsNum){
System.out.println("太大啦");
}
}while(num!=gsNum);
System.out.println("正确");
}
java+猜字代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java猜字母、java+猜字代码的信息别忘了在本站进行查找喔。








