hayirlibirseydusunmedigimeyeminederim
Yazdığım koda 2 tane input veriyorum; ilk input doğru output verirken ikincisinde hatalı sonuç alıyorum. Acil bir şekilde düzeltmem lazım soruyu, yardımcı olursanız çok sevinirim.Verilen input:5 51 2 NLMLMLMLMM3 3 EMMRMMRMRRMDoğru output:1 3 N5 1 EBenim output um:1 3 N1 5 Epackage roversonmars;publi
Yazdığım koda 2 tane input veriyorum; ilk input doğru output verirken ikincisinde hatalı sonuç alıyorum.
Acil bir şekilde düzeltmem lazım soruyu, yardımcı olursanız çok sevinirim.
Verilen input:
5 5
1 2 N
LMLMLMLMM
3 3 E
MMRMMRMRRM
Doğru output:
1 3 N
5 1 E
Benim output um:
1 3 N
1 5 E
package roversonmars;
public class RoversOnMars {
//Variables
public int x; // x-coordinate
public int y;// y-coordinate
public char compassPoint; // four cardinal compass point,i.e; North, South, West, East
public static int xCoor, yCoor; // static variables that represent upper-right coordinates of the plateau
//No-argument constructor
public RoversOnMars(){
x = 0;
y = 0;
compassPoint = 'N';
}
// Constructor to set the robotic rover's initial location
public RoversOnMars(int x, int y, char compassPoint){
setX(x);
setY(y);
this.compassPoint = compassPoint;
}
//Accessor method that returns x-coordinate value
public int getX(){
return x;
}
//Accessor method that returns y-coordinate value
public int getY(){
return y;
}
//Accessor method that returns compass point value, i.e; North, South, West, East
public char getCompassPoint(){
return compassPoint;
}
//Mutuator method that sets x-coordinate value
public void setX(int x){
this.x = x;
}
//Mutuator method that sets y-coordinate value
public void setY(int y){
this.y = y;
}
//Mutuator method that sets compass point value, i.e; North, South, West, East
public void setCompassPoint(int number){
switch(number){
case 1:
compassPoint = 'N';
break;
case 2:
compassPoint = 'S';
break;
case 3:
compassPoint = 'W';
break;
case 4:
compassPoint = 'E';
break;
default:
System.out.println("Fatal Error.");
break;
}
}
// Method that includes the letters 'L' for left, 'R' for right, 'M' for move commands.
//In case of 'L' or 'R' command, the rover does not move; just spin 90 degrees left or right respectively.
//'M' command makes the rover to move one grid according to where it faces through
//The method implements the given instructions to tell the rover how to explore the plateau, and prints the instructions in the end.
public void CommandLetters(String command){
for(int i=0; i<=command.length()-1; i++){
switch(command.charAt(i))
{
case 'L':
if(getCompassPoint() == 'N'){
setCompassPoint(4);
break;
}
else if(getCompassPoint() == 'E'){
setCompassPoint(2);
break;
}
else if(getCompassPoint() == 'W'){
setCompassPoint(1);
break;
}
else if(getCompassPoint() == 'S'){
setCompassPoint(3);
break;
}
case 'R':
if(getCompassPoint() == 'N'){
setCompassPoint(3);
break;
}
else if(getCompassPoint() == 'E'){
setCompassPoint(1);
break;
}
else if(getCompassPoint() == 'W'){
setCompassPoint(2);
break;
}
else if(getCompassPoint() == 'S'){
setCompassPoint(4);
break;
}
break;
case 'M':
if(getCompassPoint() == 'N'){
this.y = y+1;
this.x = x;
break;
}
else if(getCompassPoint() == 'E'){
this.x = x -1;
this.y = y;
break;
}
else if(getCompassPoint() == 'W'){
this.x = x+1;
this.y = y;
break;
}
else if(getCompassPoint() == 'S'){
this.y = y-1;
this.x = x;
break;
}
break;
default:
System.out.println("Fatal Error.");
break;
}
}
System.out.println(command);
}
//Method to define the upper-right coordinates of the plateau
public static void setPlateauCoordinate(int a, int b){
xCoor = a;
yCoor = b;
}
//Main method to test the program
public static void main(String[] args) {
RoversOnMars.setPlateauCoordinate(5, 5);
System.out.println(xCoor + " " + yCoor);
RoversOnMars rover1 = new RoversOnMars(1,2,'N');
System.out.println(rover1.getX() + " " + rover1.getY() + " " + rover1.getCompassPoint());
rover1.CommandLetters("LMLMLMLMM");
RoversOnMars rover2 = new RoversOnMars(3,3,'E');
System.out.println(rover2.getX() + " " + rover2.getY() + " " + rover2.getCompassPoint());
rover2.CommandLetters("MMRMMRMRRM");
System.out.println();
System.out.println(rover1.getX() + " " + rover1.getY() + " " + rover1.getCompassPoint());
System.out.println(rover2.getX() + " " + rover2.getY() + " " + rover2.getCompassPoint());
}
}
0
hayirlibirseydusunmedigimeyeminederim (
01.03.15)
sola sağa dönüşlerde ve 'M' komutunda koordinatları artırıp azaltırken karışıklık olmuş galiba. şurdaki örnek için doğru sonuç veriyor, örneklerle iyice kontrol et ama:
www.filedropper.com------------------------------------------
sola dönüş: N->W->S->E
sağa dönüş: N->E->S->W
North'a git: y = y+1
South'a git: y = y-1
East'e git : x = x+1
West'e git : x = x-1
şeklinde düşündüm
0
düzeldi, çok teşekkür ederim.
0
🌸
hayirlibirseydusunmedigimeyeminederim
(
01.03.15)