java猜数字游戏的代码 java猜数字游戏代码script

用java编写一个猜数字游戏,package day06;
import java.util.Scanner;
//猜字符游戏
public class GuessingGame {
//主方法
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 0; //猜错java猜数字游戏的代码的次数
char[] chs = generate(); //随机生成的字符数组
System.out.println(chs); //作弊
while(true){ //自造死循环
System.out.println("猜吧!");
String str = scan.next().toUpperCase(); //获取用户输入的字符串
if(str.equals("EXIT")){ //判断str是否是EXIT
System.out.println("下次再来吧!");
break;
}
char[] input = str.toCharArray(); //将字符串转换为字符数组
int[] result = check(chs,input);//对比
if(result[0]==chs.length){ //位置对为5
int score = chs.length*100 - count*10; //一个字符100分java猜数字游戏的代码,错一次减10分
System.out.println("恭喜java猜数字游戏的代码你猜对java猜数字游戏的代码了java猜数字游戏的代码,得分:"score);
break; //猜对时跳出循环
}else{ //没猜对
count; //猜错次数增1
System.out.println("字符对:" result[1] "个 , 位置对:" result[0] "个");
}
}
}
//随机生成5个字符数组
public static char[] generate(){
char[] chs = new char[5];
char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'};
boolean[] flags = new boolean[letters.length]; //1.
for(int i=0;ichs.length;i){
int index;
do{
index = (int)(Math.random()*letters.length); //0到25
}while(flags[index]==true); //2.
chs[i] = letters[index];
flags[index] = true; //3.
}
return chs;
}
//对比随机数组与用户输入的数组
public static int[] check(char[] chs,char[] input){
int[] result = new int[2];
for(int i=0;ichs.length;i){
for(int j=0;jinput.length;j){
if(chs[i]==input[j]){ //字符对
result[1]; //字符对个数增1
if(i==j){ //位置对
result[0]; //位置对个数增1
}
break;
}
}
}
return result;
}
}
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; i4; i)
guessNum[i] = -1;
for (int i = 0; i4; 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; i4; i)
{
if (Num[3 - i] == Integer.valueOf(enterString.substring(i, i1)).intValue())
rightInAll;
for (int j = 0; j4; j)
if (Num[i] == Integer.valueOf(enterString.substring(j, j1)).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, i1).equals(guessString.substring(j, j1)))
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("以下是java猜数字游戏的代码你猜数字的结果:\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());
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, "你应经猜java猜数字游戏的代码了十次java猜数字游戏的代码,输java猜数字游戏的代码了!");
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猜数字小游戏 。用eclipse写的import java.util.Scanner;
/**
* Java命令行版 猜数字游戏
* @author kaifang
*/
public class GuessNum {
public static void main(String[] args)
{
System.out.println("======猜数字游戏======\n");
int answer = (int)(Math.random() * 2001);
Scanner sr = new Scanner(System.in);
while(true) {
System.out.print("请输入你猜的数字(1-200)java猜数字游戏的代码:");
int in = sr.nextInt();
if (inanswer) {
System.out.println("猜大java猜数字游戏的代码了!\n");
} else if(inanswer){
System.out.println("猜小了!\n");
} else {
System.out.println("恭喜你,才猜对了?。。n");
break;
}
}
sr.close();
}
}
java如何实现的简单猜数字游戏代码主要通过随机数与逻辑判断来实现游戏功能
实现代码如下:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 产生一个随机数
int number = (int) (Math.random() * 100)1;
// 加入count
int count = 0;
// 在这里加入最大值,和最小值
int max = 100;
int min = 1;
while (true) {
// 键盘录入数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要猜的数据:("min"~"max")");
try {
count;
int guessNumber = sc.nextInt();
// 判断
if (guessNumbernumber) {
max = guessNumber;
System.out.println("你猜大了");
} else if (guessNumbernumber) {
min = guessNumber;
System.out.println("你猜小了");
} else {
System.out.println("恭喜你,花了"count"次就猜中了");
// 问是否继续
System.out.println("请问还要继续吗?(yes)");
sc = new Scanner(System.in);
String str = sc.nextLine();
if ("yes".equals(str)) {
// 重写赋值随机数
number = (int) (Math.random() * 100)1;
count = 0;
max = 100;
min = 1;
} else {
break;
}
}
} catch (InputMismatchException e) {
System.out.println("你输入的数据有误");
}
}
}
【java猜数字游戏的代码 java猜数字游戏代码script】java猜数字游戏的代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于java猜数字游戏代码script、java猜数字游戏的代码的信息别忘了在本站进行查找喔 。

    推荐阅读