猜数字java源代码 猜数字java程序

java猜数字游戏代码

import javax.swing.UIManager;

成都创新互联网络公司拥有10多年的成都网站开发建设经验,近千家客户的共同信赖。提供成都做网站、成都网站制作、网站开发、网站定制、外链、建网站、网站搭建、成都响应式网站建设公司、网页设计师打造企业风格,提供周到的售前咨询和贴心的售后服务

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编写一个猜数字游戏,

package day06;

import java.util.Scanner;

//猜字符游戏

public class GuessingGame {

//主方法

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int count = 0; //猜错的次数

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分,错一次减10分

System.out.println("恭喜你猜对了,得分:" + 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猜数字游戏怎么怎么做到在游戏结束后输入y重新开始输入n退出游戏?下面是源代码

while(true){

//你的程序

System.out.println("再玩一次?");

String st= scan.next();

if( st=="y"){continue;}

if( st=="n"){break;}

System.out.println("既不是y也不是n,出错");

}

java猜数字小游戏代码怎么写?

import java.util.Scanner;

public class Guess {

private static final int F = 1;

private static final int T = 100;

public static void main(String[] args) {

int num = (int)(Math.random() * T) + F;

Scanner s = new Scanner(System. in );

// 初始化

int guest = -1;

while (true) {

System.out.print("请输入你猜测的数字(" + F + "-" + T + "):");

// 每次输入的值,采纳即可

guest = s.nextInt();

if (guest  num) {

System.out.println(F + "--" + guest);

} else if (guest  num) {

System.out.println(guest + "--" + T);

} else {

System.out.println("中奖了");

break;

}

}

s.close();

}

}


分享文章:猜数字java源代码 猜数字java程序
URL网址:http://cdiso.cn/article/hicchh.html

其他资讯