Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一维阵列战列舰代码_Java - Fatal编程技术网

Java 一维阵列战列舰代码

Java 一维阵列战列舰代码,java,Java,我是java初学者。我试图模拟heads第一本java书中的一维阵列战舰代码。代码没有失败,但我无法使其正常工作 示例:[1,2,3]是包含战舰位置的阵列。如果我猜到除1以外的任何数字,它将显示为未命中。但如果我连续猜它是战列舰长度的三分之一,我就把它当作杀戮。我无法解决这个问题 你能帮帮我吗。代码发布如下: package battleship; public class battleship { public int[] battleship = new int[3]; p

我是java初学者。我试图模拟heads第一本java书中的一维阵列战舰代码。代码没有失败,但我无法使其正常工作

示例:
[1,2,3]
是包含战舰位置的阵列。如果我猜到除1以外的任何数字,它将显示为未命中。但如果我连续猜它是战列舰长度的三分之一,我就把它当作杀戮。我无法解决这个问题

你能帮帮我吗。代码发布如下:

package battleship;

public class battleship {
    public int[] battleship = new int[3];
    public int numofhits = 0;
    String result = "miss";//instance variables complete

    //setter method to initialize the array which holds the battle ship location
    public void setbattleshiploc(int startpos) {
        int i = 0;
        for(i = 0; i < battleship.length; i++) {
            battleship[i] = startpos + i;
            System.out.println(battleship[i]);
        }
        System.out.println("initialized array is: " + java.util.Arrays.toString(battleship));
    }

    //getter method to print the set battleship array
    public  int[] getbattleshiploc() {
        System.out.println("Battleship array is: " + battleship);
        return battleship;
    }

    //checking whether user guess inside the battleship array location
    public String guessloc(int guessnum) {
        //int i = 0;
        for(int cell : battleship) {
            System.out.println("Guessed number is: " + guessnum + " array element: " + battleship[cell]);
            System.out.println("cell: "+ cell + " ,battleship[cell]: " + battleship[cell] );

            if(cell == guessnum) {
                numofhits++;                
                if(numofhits == 3) {
                    result = "kill";
                    return result;
                }//if the hits are 3 then return kill indicating that no more battle ship is available
                else {
                    result = "hit";
                    return result;
                }//end inner else               
            }//end outer if

            else {
                //numofhits++;
                result = "miss";
                return result;
            }//end the if-else

        }//end for loop

        return "finished";
    }//end function guessloc

}//end class



package battleship;
import java.util.Scanner; 

public class gamelauncher {
public Scanner[] reader;    


    public static void main(String[] args) {
        String result = "miss";
        int numofguess = 0;
        //int loopnum = 0;
        battleship launchgame = new battleship();//launch the game
        int startpos = (int) (Math.random() * 5);//generate random number between 0-4
        //int[] location = {startpos, startpos + 1, startpos + 2};//initialize three consecutive array element 
        launchgame.setbattleshiploc(startpos);//set the array as the battleship location using setter

        //display the battleship position
        System.out.println("Battle shipt positions are: " +  startpos +" ," + startpos+1 + " ," + startpos+2);
        System.out.println("the battle ship array is: " + launchgame.getbattleshiploc());
        //int[] battleshiplocation = launchgame.getbattleshiploc();
        System.out.println(java.util.Arrays.toString(launchgame.getbattleshiploc()));

        while(result != "kill") {

            Scanner reader = new Scanner(System.in);  // Reading from System.in
            System.out.println("Enter a number: ");
            int guess = reader.nextInt(); //get the user input integer
            //reader.close();//close the scanner
            //loopnum++;

            numofguess++;
            if(guess < 0 || guess > 7) {
                System.out.println("Maximum space available is 7 units(0-6) and battleship length is 3 units, Please provide the location accordingly");
                continue;
            }//close if loop and go to the loop execution if the guess is not within the limits
            else {
                result = launchgame.guessloc(guess);
                System.out.println("response from guessing method: " + result);
                //get the status(hit/miss/kill) back from guess location method

                if(result == "kill") {
                    System.out.println("We have destroyed all the parts of battle ship and it took " + numofguess +" guesses"  );
                    break;//get out of the loop as we have destroyed everything
                }//end kill
                else if(result == "hit") {
                    System.out.println("You have destroyed " + launchgame.numofhits+" parts of batlleship, please continue");
                    continue;
                }
                else {
                    System.out.println("It's a miss dumbo, try again");
                    continue;
                }
            }//end outer else statement
        }//end while loop

    }//end main method

}//end class
package战舰;
公共级战舰{
公共int[]战舰=新int[3];
公共整数numofhits=0;
String result=“miss”//实例变量已完成
//setter方法初始化保存战舰位置的数组
公共无效收件地址(int startpos){
int i=0;
对于(i=0;i7){
System.out.println(“最大可用空间为7个单位(0-6),战列舰长度为3个单位,请提供相应的位置”);
继续;
}//关闭if循环,如果猜测不在限制范围内,则转到循环执行
否则{
结果=launchgame.guessloc(guess);
System.out.println(“来自猜测方法的响应:“+结果”);
//从猜测定位方法获取状态(命中/未命中/杀死)
如果(结果=“终止”){
println(“我们已经摧毁了战舰的所有部分,并花费了”+numfoguess+“猜测”);
break;//退出循环,因为我们已经破坏了一切
}//终止杀戮
否则如果(结果=“命中”){
System.out.println(“您已销毁”+启动游戏.numofhits+“蝙蝠飞船的部分,请继续”);
继续;
}
否则{
System.out.println(“是小飞象小姐,再试一次”);
继续;
}
}//结束外部else语句
}//边结束边循环
}//端主方法
}//末级

我认为问题在于,当您比较字符串时,您使用的是==而不是.equals(),因此您的代码应该如下所示:

if(result.equals("kill"))

我可以帮你把这个函数稍微修改一下。请尝试自行修复其余代码。它会给你重要的经验

public String guessloc(int guessnum) {
    for(int i=0;i<battleship.length;++i) {
        if(battleship[i] == guessnum) { //it's a hit
            battleship[i] = -1; //you cant hit it again, so change it
            if(++numofhits == 3) {
                return "kill";
            }else
                return "hit";
            }
    }
    return "miss"; //miss should be outside of the for loop
}
public String guessloc(int guessnum){

对于(int i=0;i您不应返回“miss”当一个部分丢失时,您应该立即返回它,只有当所有部分都丢失时才返回。如果该示例的目标是演示如何处理数组,我会将该书扔进垃圾箱。这里不是开玩笑。无论如何,不要将字符串与数字运算符进行比较,如!=、==等。而是使用string1.equals(string2)改进do
“kill”.equals(result)
因为
null.equals(“string”)=>NullPointerException
“string”。equals(null)=>false
:)通常是正确的。但在它们的实现中它是