Also habe ich ein Kompositionsprogramm, das eine Point
Klasse hat und innerhalb dieser eine Line
Klasse, die die Point-Klasse isValid
Methode verwendet, um sicherzustellen, dass ein Benutzer korrekte Werte eingibt, bevor Sie fortfahren.Wie kann sichergestellt werden, dass Benutzer korrekte Koordinatenwerte eingeben?
Grundsätzlich, wenn sie die Werte in der enterLine()
in meiner Hauptklasse eingeben, sollten sie eingeben müssen, bis es die Regeln isValid()
in Point und validateLine()
in Zeile erfüllt.
// This method is in the Point class and will be used by validateLine()
public static boolean isValid(int x, int y)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int maxHeight = screenSize.height;
int maxWidth = screenSize.width;
if (x < 0 || y < 0 || x > maxWidth || y > maxHeight)
{
return false;
}
return true;
}
// This method is in the Line class, uses isValid() from Point Class
public static boolean validateLine(int x1, int y1, int x2, int y2, int width)
{
if (Point.isValid(x1, y1) && Point.isValid(x2, y2))
{
return true;
}
return false;
}
// This method is in main, it prompts user to enter two valid Points.
public static Line enterLine(int line, Scanner kb)
{
System.out.println("Please enter two x's and y's values, a width, and color");
int x1 = kb.nextInt();
int x2 = kb.nextInt();
int y1 = kb.nextInt();
int y2 = kb.nextInt();
int newWidth = kb.nextInt();
String color = kb.next();
return new Line(x1, x2, y1, y2, color, newWidth);
}
Und was ist das Problem? – vanje