java猜数字项目代码 java猜数字项目代码是多少-成都快上网建站

java猜数字项目代码 java猜数字项目代码是多少

java猜数字游戏代码

import javax.swing.UIManager;

创新互联成立与2013年,先为双柏等服务建站,双柏等地企业,进行企业商务咨询服务。为双柏企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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如何实现的简单猜数字游戏代码

主要通过随机数与逻辑判断来实现游戏功能

实现代码如下:

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 (guessNumber number) {

max = guessNumber;

System.out.println("你猜大了");

} else if (guessNumber number) {

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实现的简单猜数字游戏代码,通过随机数与逻辑判断来实现游戏功能 代码如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 产生一个随机数 int n

用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猜数字小游戏代码怎么写?

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猜数字代码

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猜数字项目代码是多少
分享URL:http://kswjz.com/article/hjescp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流