首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

刚刚写了一段代码,希望大家指点上

2013-01-17 
刚刚写了一段代码,希望大家指点下。使用的编译器是eclipse,语言为JAVA。问题是很多实现我都放到了main里,却

刚刚写了一段代码,希望大家指点下。
使用的编译器是eclipse,语言为JAVA。
问题是很多实现我都放到了main里,却不知道如何移植到类的方法里。


public class Game {
private int numOfHit;             //保存打中的个数
private int numOfGuesses;         //保存猜测的次数
private int[] locationCells;      //保存模板
private int[] location;           //保存需要击中的目标

//private int[] check;              //检测数组
//private int i;                    //检测的变量

public void set(int[] cell,int[] loc){
locationCells=cell;
location=loc;
}

public boolean judge(int x){
for(int cell:location){
if(cell==x)
return true;
}
return false;
}


}



import java.io.*;
public class GameDrive {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int[] locCell={0,1,2,3,4,5,6,7,8};
int[] goal={3,5,7};

Game game=new Game();
game.set(locCell, goal);

int x;  //用户输入的数字:
int num=0;    //用户输入的次数
int numOfHit=0;   //用户击中目标的次数
while(true){
System.out.println("Please enter a number:");
BufferedReader is =new BufferedReader(new InputStreamReader(System.in));
String inputline=is.readLine();
x=Integer.parseInt(inputline);

num++;
if(game.judge(x)){
numOfHit++;
System.out.println("hit!");
}
else{
System.out.println("Miss!");
}

if(3==numOfHit){
System.out.println("Your socor is "+num);
System.out.println("Game Over!");
break;
}


}
}

}

[解决办法]

import java.io.*;
public class Game {
    private int numOfHit;             //保存打中的个数
    private int numOfGuesses;         //保存猜测的次数
    private int[] locationCells;      //保存模板
    private int[] location;           //保存需要击中的目标

    public Game (){} 
    public Game (int[] cell,int[] loc){
        locationCells=cell;
        location=loc;
    }
     
//    private int[] check;              //检测数组
//    private int i;                    //检测的变量


     
    public void set(int[] cell,int[] loc){
        locationCells=cell;
        location=loc;
    }
     
    public boolean judge(int x){
        for(int cell:location){
            if(cell==x)
                return true;
        }
        return false;    
    }

    public void start(){
       int x;  //用户输入的数字:
        int num=0;    //用户输入的次数
        int numOfHit=0;   //用户击中目标的次数
        while(true){
            System.out.println("Please enter a number:");
            BufferedReader is =new BufferedReader(new InputStreamReader(System.in));
            String inputline=is.readLine();
            x=Integer.parseInt(inputline);
             
            num++;
            if(game.judge(x)){
                numOfHit++;
                System.out.println("hit!");
            }
            else{
                System.out.println("Miss!");
            }
             
            if(3==numOfHit){
                System.out.println("Your socor is "+num);
                System.out.println("Game Over!");
                break;
            }
                 
             
        }

    }
     
}





import java.io.*;
public class GameDrive {
 
    /**
     * @param args
     * @throws IOException 
     */


    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        int[] locCell={0,1,2,3,4,5,6,7,8};
        int[] goal={3,5,7};
        Game game=new Game(locCell, goal);
        game.start();
     
    }
 
}

热点排行
Bad Request.