我是java的初学者。我试图从头脑第一本java书中模拟单维阵列战舰代码。代码没有失败,但我无法使其正常工作。
示例: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
是包含战舰位置的数组。如果我猜任何数字除了1,它显示为未命中。但是,如果我连续三次猜测它(战列舰的长度),我就把它当作杀戮。我无法弄清楚这个问题。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
jGroupChatName = getIntent().getExtras().get("groupChatName").toString();
jUserName = getIntent().getExtras().get("groupUserName").toString();
jUserNeighbourhood = getIntent().getExtras().get("groupUserHome").toString();
jChatToolbar = (Toolbar) findViewById(R.id.allUSersToolBar);
jFirebaseCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
/*UserID Start - I was trying to retrieve the current user ID and add it to the Group Chat node,
under the selected Group Chat */
assert jFirebaseCurrentUser != null;
final String currentUserID = jFirebaseCurrentUser.getUid();
jChatRoot = FirebaseDatabase.getInstance().getReference().child("Group Chats").child(jGroupChatName).child(currentUserID);
/*UserID End*/
jChatMessageText = (EditText) findViewById(R.id.chatMessageText);
jChatSendTextBtn = (ImageButton) findViewById(R.id.chatSendTextBtn);
jChatSendTextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Map<String, Object> groupMap = new HashMap<String, Object>();
jGroupKey = jChatRoot.push().getKey();
jChatRoot.updateChildren(groupMap);
// Based on the UserID code at the top, the "currentUserID" is added as shown below
// My intentions are to have the UserID saved under the name of the Group Chat
// jGroupKey = (Name of selected Group Chat)
DatabaseReference groupMessageRoot = jChatRoot.child(jGroupKey).child(currentUserID);
Map<String, Object> groupMap2 = new HashMap<String, Object>();
groupMap2.put("name", jUserName);
groupMap2.put("msg", jChatMessageText.getText().toString());
groupMessageRoot.updateChildren(groupMap2);
}
});
}
答案 0 :(得分:1)
我可以帮助你改变这个功能。请尝试自行修复其余代码。它会给你重要的经验。
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
}
答案 1 :(得分:0)
我认为问题在于,在比较字符串时,你使用的是==而不是.equals(),所以你的代码应该是这样的:
if(result.equals("kill"))