求一段java代码猜数 java中猜数字怎么编程
简单的java猜数字代码
public static void main(String [] args){
建网站原本是网站策划师、网络程序员、网页设计师等,应用各种网络程序开发技术和网页设计技术配合操作的协同工作。创新互联公司专业提供成都网站建设、成都网站制作,网页设计,网站制作(企业站、成都响应式网站建设公司、电商门户网站)等服务,从网站深度策划、搜索引擎友好度优化到用户体验的提升,我们力求做到极致!
int[] num_list={1,3,5,7,9};
java.util.Scanner str=new java.util.Scanner(System.in);
int num = str.nextInt();
for(int i=0;inum_list.length;i++){
if(num!=nullnum.equals("")){
if(num_list[i]==num){
System.out.println("恭喜你,猜对了!");
}else{
System.out.println("抱歉,你猜错了!");
}
}
}
}
如何用Java语言实现猜数字游戏
java实现的简单猜数字游戏代码,通过随机数与逻辑判断来实现游戏功能 代码如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 产生一个随机数 int n
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);
}
}
}
用JAVA编猜数字游戏
1)
程序随机分配给客户一个1—100之间的整数
Random
gen
=
new
Random();
int
a
=
gen.nextInt(100)+1;
//不加1是0到99
2)
用户在输入对话框中输入自己的猜测
Scanner
in
=
new
Scanner(System.in);
System.out.println("give
me
a
number")
int
b
=
in.nextInt();
3)
程序返回提示信息,提示信息分别是:“猜大了”、“猜小了”和“猜对了”。
if(ba)
System.out.println("big");
else
if(ba)
System.out.println("small");
else
System.out.println("right");
4)
用户可根据提示信息再次输入猜测,直到提示信息是“猜对了”。
while(b!=a){
if(ba)
System.out.println("big");
else
if(ba)
System.out.println("small");
System.out.println("give
me
a
new
number")
int
b
=
in.nextInt();
}
System.out.println("right");
3看懂以后,3,4可以合在一起,用4的回答.
分享标题:求一段java代码猜数 java中猜数字怎么编程
文章源于:http://cdiso.cn/article/ddogosc.html