-
February 9th, 2012, 01:21 AM
#1
Declaring multiple string variables
I was hoping someone could help me to declaring multiple string variables. I tried something like this:
Code:
System.out.print("Enter your move (U/D/L/R)>");
String movePlayer= "R", "L", "D", "U";
movePlayer = move.next();
int x=0, y=0;
if(movePlayer == "R") {
grid[y][x-1]='.';
x--;
The part starting at: String movePlayer= "R", "L", "D", "U";
It won't let me declare them that way. Im not sure what to do.
I'm trying to validate the other letters so I can use them in "if statements" to make a player move in a game. Thank you for the help in advance.
-
February 9th, 2012, 10:17 AM
#2
Re: Declaring multiple string variables
Do you mean you want to declare an array of Strings?
Your code is confusing, why initialise movePlayer to any value when on the next line you set it to the the value returned by move.next().
BTW when comparing String values you need to use the equals() method and not ==.
-
February 9th, 2012, 11:26 AM
#3
Re: Declaring multiple string variables
I guess I would want to declare an array of Strings. As far as the movePlayer variable, I was trying to tell java that I want the values to be U, D, L, R input commands scanned in from the user. Pretty much what I want the program to do is when the user types in "U+(Enter)" the player will move up one spot, and so on and so forth for the other commands.
-
February 9th, 2012, 11:40 AM
#4
Re: Declaring multiple string variables
Here's my what I have for my program so far:
Code:
import java.util.Scanner;
import java.util.Random;
public class Adventure {
public static void main(String[] args) {
// Create 2D array for game board.
char grid[][]= new char[10][10];
Scanner move = new Scanner(System.in);
System.out.println("Here is the current game board:");
System.out.println("-------------------------------");
for(int i=0; i<grid.length; i++) {
for(int j=0; j<grid.length; j++) {
double Random = Math.random();
if(Random <=.05) {
grid[i][j]='*';
}
else if(Random > .06 && Random <= .15) {
grid[i][j]='X';
}
else {
grid[i][j]='.';
}
grid[0][0]='P';
grid[9][9]='T';
System.out.print(grid[i][j]);
}
System.out.println("");
}
System.out.print("Enter your move (U/D/L/R)>");
String movePlayer= "R";
movePlayer = move.next();
int x=0, y=0;
if(movePlayer == "R") {
grid[y][x]='.';
x--;
}
//if(movePlayer == "L") {
//grid[y][x]='.';
//x++;
//}
//if(movePlayer == "D") {
//grid[y][x]='.';
//y++;
//}
//if(movePlayer == "U") {
//grid[y][x]='.';
//y--;
//}
//if(grid[y][x]=='*') {
//System.out.println("You fell in a pit. Game Over.");
//}
//if(grid[y][x]=='X') {
//System.out.println("That spot is blocked. Please enter another move.");
//}
else {
grid[y][x]='P';
}
System.out.print(grid[y][x]);
}
}
I commented out the if statements towards the end so I can run the program without it throwing me errors.
-
February 9th, 2012, 11:42 AM
#5
Re: Declaring multiple string variables
I guess I would want to declare an array of Strings
To create a string array and initialise it in one go do:
Code:
String[] movePlayer= new String[]{"R", "L", "D", "U"};
As far as the movePlayer variable, I was trying to tell java that I want the values to be U, D, L, R input commands scanned in from the user.
It doesn't do that.
If you only want to handle those 4 characters then you don't need the array, just check if the input is one of the 4 characters and take the appropriate action. Remember is use equals() or equalsIgnoreCase() to do the test.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|