java写的猜数字游戏

xtuxucj 2009-08-19 09:37:14
java写的猜数字游戏。没有实现GUI界面,只能在Eclipse控制台输入玩玩。请各位高人根据源码,指出该程序的不足之处或者代码规范问题

package guessGame;

import java.util.*;
import java.io.*;

public class GuessDigit {
private ArrayList<Integer> digitArray ;
private boolean flag = true;
private static final int SIZE = 4;
private static int count = 7;


public GuessDigit(){
this(GuessDigit.count);
}

public GuessDigit(int count){

GuessDigit.count = count;
digitArray = new ArrayList<Integer>(SIZE);
Random r = new Random();
int n = 0;
while(n < SIZE){

int i = r.nextInt(9);
if(!digitArray.contains(i)){
digitArray.add(n, i);
n ++;
}
}

System.out.println("请输入4个不同的数字,你一共有"+count+"次机会:");
}

public String toString(){
String s = "";
for(int i = 0; i < digitArray.size(); i ++){
s += digitArray.get(i);
}
return s;
}

private void campare(String s){
String array = toString();
int A = 0, B = 0;
char c1, c2;
for(int i = 0 ;i < SIZE; i ++){
c1 = array.charAt(i);
c2 = s.charAt(i);
if(c1 == c2){
A ++;
}else if(array.indexOf(c2) != -1)
B ++;
}

System.out.println(s + " " + A + "A" + B + "B");

if(A == SIZE){
System.out.println("恭喜你猜对了!数字是" + this);
flag = false;
}

if(--count == 0 && A < SIZE ){
System.out.println("已达到规定次数,数字是:"+this);
flag = false;
}
}

public void getConsole() throws IOException{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s ;
boolean repeat;
int i;

while(flag){
s = stdin.readLine();
repeat = false;
i = 0;
if(s.matches("\\d{4}")){
while(!repeat && i < SIZE-1){
if(s.lastIndexOf(s.charAt(i)) != i){
System.out.println("请输入4不同的数字:");
repeat = true;
}
i ++;
}
if(!repeat)
campare(s);
}else
System.out.println("请检查输入值的格式:");



}
}

public static void main(String[] args) throws IOException {
GuessDigit g = new GuessDigit(); ;
//System.out.println(g);
g.getConsole();
}
}
...全文
444 15 打赏 收藏 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
smallsmallworld 2009-08-21
  • 打赏
  • 举报
回复
不错,顶一个
my_firstlove 2009-08-20
  • 打赏
  • 举报
回复
while (!repeat && i < SIZE - 1) {
if (s.lastIndexOf(s.charAt(i)) != i) {
System.out.println("请输入4不同的数字:");
repeat = true;
}
i++;
}
bigbug9002 2009-08-19
  • 打赏
  • 举报
回复
      while (!repeat && i < SIZE - 1) {
if (s.lastIndexOf(s.charAt(i)) != i) {
System.out.println("请输入4不同的数字:");
repeat = true;
}
i++;
}


判断四个不同的数字的方法值得学习.
feishare 2009-08-19
  • 打赏
  • 举报
回复
1357
1357 1A1B
2468
2468 1A1B
1458
1458 2A1B
1059
1059 1A0B
4587
4587 1A3B
8457
8457 1A3B
4758
4758 4A0B
恭喜你猜对了!数字是4758
bigbug9002 2009-08-19
  • 打赏
  • 举报
回复
找到一小BUG,
i = r.nextInt(9);
这个只会产生0~8的随机数,不会产生9
feishare 2009-08-19
  • 打赏
  • 举报
回复
不错,先玩两局~~
bigbug9002 2009-08-19
  • 打赏
  • 举报
回复
感谢楼上的,我也重排了一下格式。先玩一把:
import java.util.*; 
import java.io.*;

public class GuessDigit {
private ArrayList <Integer> digitArray ;
private boolean flag = true;
private static final int SIZE = 4;
private static int count = 7;


public GuessDigit(){
this(GuessDigit.count);
}

public GuessDigit(int count){
GuessDigit.count = count;
digitArray = new ArrayList <Integer>(SIZE);
Random r = new Random();
int n = 0;
while(n < SIZE){
int i = r.nextInt(9);
if(!digitArray.contains(i)){
digitArray.add(n, i);
n ++;
}
}

System.out.println("请输入4个不同的数字,你一共有"+count+"次机会:");
}

public String toString(){
String s = "";
for(int i = 0; i < digitArray.size(); i ++){
s += digitArray.get(i);
}
return s;
}
private void campare(String s){
String array = toString();
int A = 0, B = 0;
char c1, c2;
for(int i = 0 ;i < SIZE; i ++){
c1 = array.charAt(i);
c2 = s.charAt(i);
if(c1 == c2){
A ++;
}else if(array.indexOf(c2) != -1)
B ++;
}
System.out.println(s + " " + A + "A" + B + "B");
if(A == SIZE){
System.out.println("恭喜你猜对了!数字是" + this);
flag = false;
}

if(--count == 0 && A < SIZE ){
System.out.println("已达到规定次数,数字是:"+this);
flag = false;
}
}
public void getConsole() throws IOException{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s ;
boolean repeat;
int i;
while(flag){
s = stdin.readLine();
repeat = false;
i = 0;
if(s.matches("\\d{4}")){
while(!repeat && i < SIZE-1){
if(s.lastIndexOf(s.charAt(i)) != i){
System.out.println("请输入4不同的数字:");
repeat = true;
}
i ++;
}
if(!repeat)
campare(s);
}else
System.out.println("请检查输入值的格式:");
}
}

public static void main(String[] args) throws IOException {
GuessDigit g = new GuessDigit(); ;
//System.out.println(g);
g.getConsole();
}
}


请输入4个不同的数字,你一共有7次机会:
1234
1234 0A2B
5678
5678 0A1B
0956
0956 0A2B
6078
6078 0A1B
7508
7508 2A0B
2503
2503 4A0B
恭喜你猜对了!数字是2503

dz007 2009-08-19
  • 打赏
  • 举报
回复
路过填格式…………
package guessGame;

import java.util.*;
import java.io.*;

public class GuessDigit {
private ArrayList<Integer> digitArray;
private boolean flag = true;
private static final int SIZE = 4;
private static int count = 7;

public GuessDigit() {
this(GuessDigit.count);
}

public GuessDigit(int count) {

GuessDigit.count = count;
digitArray = new ArrayList<Integer>(SIZE);
Random r = new Random();
int n = 0;
while (n < SIZE) {

int i = r.nextInt(9);
if (!digitArray.contains(i)) {
digitArray.add(n, i);
n++;
}
}

System.out.println("请输入4个不同的数字,你一共有" + count + "次机会:");
}

public String toString() {
String s = "";
for (int i = 0; i < digitArray.size(); i++) {
s += digitArray.get(i);
}
return s;
}

private void campare(String s) {
String array = toString();
int A = 0, B = 0;
char c1, c2;
for (int i = 0; i < SIZE; i++) {
c1 = array.charAt(i);
c2 = s.charAt(i);
if (c1 == c2) {
A++;
} else if (array.indexOf(c2) != -1)
B++;
}

System.out.println(s + " " + A + "A" + B + "B");

if (A == SIZE) {
System.out.println("恭喜你猜对了!数字是" + this);
flag = false;
}

if (--count == 0 && A < SIZE) {
System.out.println("已达到规定次数,数字是:" + this);
flag = false;
}
}

public void getConsole() throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
String s;
boolean repeat;
int i;

while (flag) {
s = stdin.readLine();
repeat = false;
i = 0;
if (s.matches("\\d{4}")) {
while (!repeat && i < SIZE - 1) {
if (s.lastIndexOf(s.charAt(i)) != i) {
System.out.println("请输入4不同的数字:");
repeat = true;
}
i++;
}
if (!repeat)
campare(s);
} else
System.out.println("请检查输入值的格式:");

}
}

public static void main(String[] args) throws IOException {
GuessDigit g = new GuessDigit();
;
// System.out.println(g);
g.getConsole();
}
}
sunzerui 2009-08-19
  • 打赏
  • 举报
回复
楼主,我有个封装好的exe文件。
yubangqi 2009-08-19
  • 打赏
  • 举报
回复
友情帮该
yubangqi 2009-08-19
  • 打赏
  • 举报
回复

package guessGame;

import java.util.*;
import java.io.*;

public class GuessDigit {
private ArrayList <Integer> digitArray ;
private boolean flag = true;
private static final int SIZE = 4;
private static int count = 7;


public GuessDigit(){
this(GuessDigit.count);
}

public GuessDigit(int count){

GuessDigit.count = count;
digitArray = new ArrayList <Integer>(SIZE);
Random r = new Random();
int n = 0;
while(n < SIZE){

int i = r.nextInt(9);
if(!digitArray.contains(i)){
digitArray.add(n, i);
n ++;
}
}

System.out.println("请输入4个不同的数字,你一共有"+count+"次机会:");
}

public String toString(){
String s = "";
for(int i = 0; i < digitArray.size(); i ++){
s += digitArray.get(i);
}
return s;
}

private void campare(String s){
String array = toString();
int A = 0, B = 0;
char c1, c2;
for(int i = 0 ;i < SIZE; i ++){
c1 = array.charAt(i);
c2 = s.charAt(i);
if(c1 == c2){
A ++;
}else if(array.indexOf(c2) != -1)
B ++;
}

System.out.println(s + " " + A + "A" + B + "B");

if(A == SIZE){
System.out.println("恭喜你猜对了!数字是" + this);
flag = false;
}

if(--count == 0 && A < SIZE ){
System.out.println("已达到规定次数,数字是:"+this);
flag = false;
}
}

public void getConsole() throws IOException{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s ;
boolean repeat;
int i;

while(flag){
s = stdin.readLine();
repeat = false;
i = 0;
if(s.matches("\\d{4}")){
while(!repeat && i < SIZE-1){
if(s.lastIndexOf(s.charAt(i)) != i){
System.out.println("请输入4不同的数字:");
repeat = true;
}
i ++;
}
if(!repeat)
campare(s);
}else
System.out.println("请检查输入值的格式:");



}
}

public static void main(String[] args) throws IOException {
GuessDigit g = new GuessDigit(); ;
//System.out.println(g);
g.getConsole();
}
}

xtuxucj 2009-08-19
  • 打赏
  • 举报
回复
已经改不了了,说是有回复,我没权限修改。
bigbug9002 2009-08-19
  • 打赏
  • 举报
回复
最多7次,应该能猜出来.一般情况下5-6次.再少要看运气了.
zWX19940 2009-08-19
  • 打赏
  • 举报
回复
复议
bigbug9002 2009-08-19
  • 打赏
  • 举报
回复
哈哈,我的小灵通上有这个小游戏,平时消磨时间时,就玩这个游戏。

楼主能不能把代码重贴一下?没有格式的代码,没有心情看啊。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧