这是我的情况:循环距离,并退出
我需要的代码在一个循环中运行,一遍又一遍的问同样的问题(一个或多个)用户,直到用户键入任意点的“q”来终止/退出循环,从而退出程序。
问题是我试图使用do-while/while循环,并且只有当条件成立时才执行这些循环。但我需要条件(“q”)为假,以便它可以继续循环。如果条件为真(input.equals(“q”)),那么它并不是什么都不是,因为它不会使用整数/双精度来使用字符串(“q”)来计算距离。
我已经想出了如何获得距离,代码效果很好,但有没有解决方法,我可以让循环继续而条件为假?
顺便说一句,我只是勉强学习java以防万一......
”。
import java.*;
public class Points {
public static void main(String[] args){
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the first X coordinate: ");
double x1 = input.nextDouble();
System.out.print("Enter the first Y coordinate: ");
double y1 = input.nextDouble();
System.out.print("Enter the second X coordinate: ");
double x2 = input.nextDouble();
System.out.print("Enter the second Y coordinate: ");
double y2 = input.nextDouble();
System.out.println("(" + x1 + ", " + y1 + ")" + " " + "(" + x2 + ", " + y2+ ")");
double getSolution = Math.sqrt(((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1)));
System.out.println(getSolution);
}
}'
最后一门功课的问题在用户实际尝试:) – 2010-10-18 20:47:55
你知道'!'操作符吗? – nmichaels 2010-10-18 20:48:02