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();
}
}

解决方案 »

  1.   


    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(); 

      

  2.   

    楼主,我有个封装好的exe文件。
      

  3.   

    路过填格式…………
    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();
    }
    }
      

  4.   

    感谢楼上的,我也重排了一下格式。先玩一把:
    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
      

  5.   

    找到一小BUG,
     i = r.nextInt(9);
    这个只会产生0~8的随机数,不会产生9
      

  6.   

    1357
    1357 1A1B
    2468
    2468 1A1B
    1458
    1458 2A1B
    1059
    1059 1A0B
    4587
    4587 1A3B
    8457
    8457 1A3B
    4758
    4758 4A0B
    恭喜你猜对了!数字是4758
      

  7.   

          while (!repeat && i < SIZE - 1) {
                        if (s.lastIndexOf(s.charAt(i)) != i) {
                            System.out.println("请输入4不同的数字:");
                            repeat = true;
                        }
                        i++;
                    }判断四个不同的数字的方法值得学习. 
      

  8.   

    while (!repeat && i < SIZE - 1) {
                        if (s.lastIndexOf(s.charAt(i)) != i) {
                            System.out.println("请输入4不同的数字:");
                            repeat = true;
                        }
                        i++;
                    }