循环两点之间的距离,q表示退出
这是我的情况:
我需要代码在循环中运行,一遍又一遍地向用户询问相同的问题,直到用户在任何点键入“q”来终止/退出循环,从而退出程序。
问题是我尝试使用 do-while/while 循环,并且这些循环仅在条件成立时才执行。但我需要条件(“q”)为假,以便它可以继续循环。如果条件为 true (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);
}
}'
This is my situation:
i need the code to run in a loop, over and over again asking the same question(s) to the user, until the user types a "q" for any point to terminate/exit the loop, thus exiting the program.
The problem is that i tried to used a do-while/while loop, and those loops executes only if the conditions comes out to be true. But i need the condition ("q") to be false so it can continue the loop. If the condition is true (input.equals("q")) , then it just does not nothing because instead of the integer/double, it will use a string ("q") to calculate the distance.
i have already figured out how to get the distance, the code as is works well, but is there any work-around that i can make the loop continue while the condition is false?
and by the way, i am just barely learning java just in case...
'.
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);
}
}'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案是使用
String line = input.nextLine()
而不是nextDouble()
。然后你可以有一个类似的方法:每次用户提供输入时都需要调用此方法:
这将退出循环。
现在,由于您有双精度数的字符串表示形式,因此您需要使用
Double.parseDouble(line)
将字符串转换为数字。然后你所要做的就是将所有内容都包含在无限循环中 ->
while(true) { }
并且,唯一退出循环的时间是
timeToExit
方法返回 true,并且您中断了循环。这一切都变成了这样:
The solution to this is to use
String line = input.nextLine()
instead ofnextDouble()
. Then you can have a method like:This method will need to be called each time the user provides input:
That will exit a loop.
Now, since you have a String representation of the double, you will need to use
Double.parseDouble(line)
to turn the String into a number.Then all you have to do is enclose everything in an infinite loop ->
while(true) { }
And, the only time it will exit the loop is if the
timeToExit
method returned true, and you break the loop.This all turns into something like:
只是一些伪代码:
如果用户输入 q,则 input.equals("q") 返回 true,然后将其取反并中断循环。
否则,用户输入另一个数字,例如 44,input.equals("q") 等于 false,该值被否定并且循环继续。
just some pseudocode:
If the user inputs q, input.equals("q") returns true which is then negated and breaks the loops.
Otherwise, the user inputs another number say 44, input.equals("q") equals false, which is negated and the loop continues.
我在你的代码中没有看到循环...:s
但是,你为什么不试试这个:
I don't see a loop in your code... :s
But, why don't you try this: