Re: help me plz with my code
What is the question?
Please explain what your problem is. Show the error messages or show what the program does and what is wrong with its output.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
A problem I see is that you are using == to compare Strings. You need to use the equals method to compare the contents of objects. == is for comparing primitives.
Re: help me plz with my code
thanks
here is the copy
package chapt10;
import java.util.Scanner;
public class walk {
private static int currentx;
/**
* @param is
*/
public static void maps(int[][] is) {
// TODO Auto-generated method stub
maps(new int [10][10]);
Scanner sc = new Scanner (System.in);
String choice = "Y";
while (!choice.equalsIgnoreCase(choice))
System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
setCurrentx(5);
int currentY = 5;
boolean quitGame = false;
while (!quitGame)
{
System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
System.out.println("Enter 'Q' to quit the game.");
String userInput = sc.nextLine();
if (userInput == "N")
{
currentY = currentY + 1; // move the player one unit north.
}
else if (userInput == "S")
{
setCurrentx(getCurrentx() - 1); // move the player one unit south.
}
else if (userInput == "E")
{
currentY--; // move the player one unit east.
}
else if (userInput == "W")
{
setCurrentx(getCurrentx() - 1); // move the player one unit west
}
else if (userInput == "Q")
{
quitGame = true;
continue;
}
else
{
System.out.println("I didn't recongnize that comman,Please try again.");
}
System.out.println("Tou are at the position" + currentY + "," + currentY + ",");
}
}
public static int getCurrentx() {
return currentx;
}
public static void setCurrentx(int currentx) {
walk.currentx = currentx;
}
}
and the console show me this message
java.lang.NoSuchMethodError: main
Exception in thread "main"
Re: help me plz with my code
There must be a main() method where the java program starts your program that is defined this way:
public static void main(String[] args) {
...
}
That is the only place the java program knows to start your program.
Re: help me plz with my code
is not this one
public static void maps(int[][] is) {
Re: help me plz with my code
Computers are very fussy about spelling and many other things. You must give it exactly what it wants.
The String[] args parameters are where the java command will pass in commandline options that follow the class name when you start your program. The name MUST be main
Re: help me plz with my code
thank you so much
here is the code
package chapt10;
import java.util.Scanner;
public class walk {
public static void main(int[][] is){
main(new int [10][10]);
Scanner sc = new Scanner (System.in);
String choice = "Y,x";
while (!choice.equalsIgnoreCase(choice))
System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
int currentx = 5;
int currentY = 5;
boolean quitGame = false;
while (!quitGame)
{
System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
System.out.println("Enter 'Q' to quit the game.");
String userInput = sc.nextLine();
if (userInput == "N")
{
currentY = currentY + 1; // move the player one unit north.
}
else if (userInput == "S")
{
}
else if (userInput == "E")
{
currentY--; // move the player one unit east.
}
else if (userInput == "W")
{
}
else if (userInput == "Q")
{
quitGame = true;
continue;
}
else
{
System.out.println("I didn't recongnize that comman,Please try again.");
}
System.out.println("Tou are at the position" + currentY + "," + currentY + ",");
}
}
}
-------
but why the " int currentx = 5; "
is not correct
Re: help me plz with my code
Please post the FULL text of any error messages here.
Or explain what your problem is.
Re: help me plz with my code
You need to use the equals method to compare the contents of objects like Strings. The == operator is for comparing primitives.
Re: help me plz with my code
when I run it the eclpis says :
java.lang.NoSuchMethodError: main
Exception in thread "main
Re: help me plz with my code
Quote:
java.lang.NoSuchMethodError: main
See post#4 and #6 about your missing main method.
Re: help me plz with my code
Did you read reply #4 by Norm?
Re: help me plz with my code
yeah i got it and i change it to
public static void main(String is){
but when i run it still did not the result.........anyways thank you
Re: help me plz with my code
It MUST BE EXACTLY as I posted in post#4 EXACTLY!!!
Re: help me plz with my code
Re: help me plz with my code
sorry guys I am not really good with java
I made this code but i'm still having trouble with if else statement any idea?
package chapt10;
import java.util.Scanner;
public class walk {
public static void main(String[][] args ){
Scanner sc = new Scanner (System.in);
String choice = "Y";
while (!choice.equalsIgnoreCase(choice))
{
System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
int[][] currentY,currentx = new int[5][5];
int currentY1 = 5;
int currentx1 = 5;
boolean quitGame = false;
while (!quitGame)
{
System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
System.out.println("Enter 'Q' to quit the game.");
String userInput = sc.nextLine();
if (userInput == "N") {
currentY = currentY + 1;
} else if (userInput == "S") {
currentx = current + 1;
} else if (userInput == "E") {
currentY--;
} else if (userInput == "W") {
currentx--;
} else if (userInput == "Q")
{
quitGame = true;
continue;
} else {
System.out.println("I didn't recongnize that comman,Please try again.");
}
System.out.println("Tou are at the position" + currentY1 + "," + currentY1 + ",");
}
}
}
Re: help me plz with my code
thank you guys here is the final
package Assignment5;
import java.util.InputMismatchException;
import java.lang.Math;
import java.util.Scanner;
public class Assignment5 {
static int x;
static int y;
static String[][] a2 = new String[10][10];
public static void main(String[] args) {
int x=5,y=5;
for (int r=0; r<a2.length; r++)
{
for (int c=0; c<a2[r].length; c++)
{
a2[r][c]="0";
}
}
for (int r=0; r<a2.length; r++)
{
int num;
num= (int) Math.random()*10;
for (int c=0; c<a2[r].length; c++)
{
if(num>=0&&num<=3)
{
a2[r][c]="1";
}
if(num>=4&&num<=7)
{
a2[r][c]="2";
}
if(num>=8&&num<=10)
{
a2[r][c]="0";
}
}
}
while(true)
{
Display.setText("\nEnter Direction: Type n for north, s for south, e for east, w for west : ");
Scanner userInput = new Scanner(System.in);
char direction;
try
{
String Enter = userInput.next();
direction= Enter.charAt(0);
System.out.print("Enter the lowercase");
direction = Enter.charAt(0);
}
catch(InputMismatchException e)
{
Display.setText("Error! Invalid Enter. Please Enter lowercase.");
continue;
}
// print array in rectangular form
switch( direction )
{
case 'n':
y=y-1;
if (y<0)
{
y=y+1;
Display.setText("you cannot go beyond the limits!!!");
}
CallingMethod(x,y);
a2[x][y]="a";
break;
case 's':
y=y+1;
if (y>9)
{
y=y-1;
Display.setText("you cannot go beyond the limits!!!");
}
CallingMethod(x,y);
a2[x][y]="a";
break;
case 'e':
x=x+1;
if (x>9)
{
x=x-1;
Display.setText("you cannot go beyond the limits!!!");
}
CallingMethod(x,y);
a2[x][y]="a";
break;
case 'w':
x=x-1;
if (x<0)
{
x=x+1;
System.out.println("you cannot go beyond the limits!!!");
}
CallingMethod(x,y);
a2[x][y]="a";
break;
}
}
}
public static void CallingMethod(int x,int y)
{
Display.setText(" The direction is");
Display.setText(" x position is: "+ x +" y position is: "+ y);
if(a2[x][y].equalsIgnoreCase("1"))
{
Display.setText("You Found The Treasure!!!");
}
if(a2[x][y].equalsIgnoreCase("2"))
{
Display.setText("You Found The Jewel!!!");
}
if(a2[x][y].equalsIgnoreCase("0"))
{
Display.setText("OOPS nothing is there!!!");
}
for (int r=0; r< a2.length; r++)
{
for (int c=0; c<a2[r].length; c++)
{
Display.setText(" " + a2[c][r]);
}
Display.setText(" ");
}
}
}
Re: help me plz with my code
Since you didn't post any question, I guess you've solved it.
Re: help me plz with my code
here is the question for the last code :
Add a function to your program you made during class.
This function should loop through each of the dimensions two arrays.
Use Math.Rand to randomly select Yes or No.
If yes, place something in that X,Y coordinate. It can be a gem, treasure, or something else of note. Use your imagineation.
The end result should be a randomly populated X/Y coordinate ‘world
Re: help me plz with my code
To get help with your code, you will have to post your code and your questions.
Please wrap posted code in code tags. See: BB Code List - Java Programming Forums or Go Advanced and use the #icon
Re: help me plz with my code
hAI guise i be following this thread 4 a while now nd i gt same problem as matt.
i dno wat i wrong i follow exacli wat u said 2 matt ye.
public class HelloWod {
public void naim static[int](){
"hello wordl;"
}
}
dno y it isnt worked. thanx.
Re: help me plz with my code
Sorry, I don't speak Welsh.
Re: help me plz with my code
Haha! <3
Yeah it takes the greatest's of minds to be able to conquer excellent Welsh literature.