lundi 5 novembre 2018

how to use If statements with boolean and to read user input

Hi i did a post yesterday about this problem but was completely wrong with how i wrote so ive spent the night and this morning trying again and still cant get my head around how to do it.

what im trying to do is test my "IsInRoom" method by getting the user to input two coordinates i cant figure out how to get the user input read by the IF statement and everytime is use r.IsInRoom it comes up with an error saying "the method IsInRoom (Point) in the type room is not applicable for the arguments double.

I have tried so many different ways of doing this and everytime i keep ending up back in the same place. if any one has any ideas that would be great im really new to java so would be great if you could give me some pointers on how to use these functions properly

thank you

import java.awt.Point;
import java.io.Reader;
import javax.swing.JOptionPane;
import java.util.Random;

public class Room {
private static int X; 
private static int Y; 
private static Point P; 
private int[] coords;
Random ranGen;

public Room(String rstr) {
    StringSplitter S = new StringSplitter(rstr, " ");
    coords = S.getIntegers();
}

public Point getRandomPoint (Random ranGen){
    return new Point(coords[0] +1 + ranGen.nextInt(coords[2] - coords[0] -2), (coords[1] +1 + ranGen.nextInt(coords[3] - coords[1] -2)));

    }



public String toString() { room
return "Room " + coords[0] + ", " + coords[1] + ", " + coords[2] + ", " + coords[3] + " Door " + coords[4] + ", " + coords[5];

}
public boolean IsInRoom(Point xy) {
     P = new Point (X,Y);

    return (xy.getX() > coords[0] && xy.getX() < coords[2] && xy.getY() > coords[1] && xy.getY() < coords[3]);
}
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Room r = new Room("0 0 5 5 0 2"); // create room
    System.out.println(r.toString()); // and print it

    String userIn = JOptionPane.showInputDialog
    (null, "please enter two coordinates one X and one Y separated by a space");
    JOptionPane.showMessageDialog(null, r.IsInRoom(P) + userIn);

    if (r.IsInRoom(P.getX() ) ) {
    System.out.println( (int) P.getX() + "," + (int) P.getY() + "Is in room");
    }
    else if (r.IsInRoom(P.getY() ) ){
    System.out.println((int) P.getX() + "," + (int) P.getY() + "Is in room");
else (r.IsInRoom(P.getXY() ) ) { 
System.out.println ((int) P.getX() + "," + (int) P.getY() + "Is not in room");
    }
    }
  }
 }

Aucun commentaire:

Enregistrer un commentaire