循环两点之间的距离,q表示退出

发布于 2024-09-27 15:50:32 字数 1093 浏览 1 评论 0原文

这是我的情况:

我需要代码在循环中运行,一遍又一遍地向用户询问相同的问题,直到用户在任何点键入“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹沉浮 2024-10-04 15:50:32

解决方案是使用 String line = input.nextLine() 而不是 nextDouble()。然后你可以有一个类似的方法:

public static boolean timeToExit(String input) {
    return input.equalsIgnoreCase("q");
}

每次用户提供输入时都需要调用此方法:

if (timeToExit(line)) break;

这将退出循环。

现在,由于您有双精度数的字符串表示形式,因此您需要使用 Double.parseDouble(line) 将字符串转换为数字。

然后你所要做的就是将所有内容都包含在无限循环中 -> while(true) { }

并且,唯一退出循环的时间是 timeToExit 方法返回 true,并且您中断了循环。

这一切都变成了这样:

while (true) {
    ...
    System.out.print("Enter the first X coordinate: ");
    String x1 = input.nextLine();
    if (timeToExit(x1)) break;
    double x1_d = Double.parseDouble(x1);
    ...
}

The solution to this is to use String line = input.nextLine() instead of nextDouble(). Then you can have a method like:

public static boolean timeToExit(String input) {
    return input.equalsIgnoreCase("q");
}

This method will need to be called each time the user provides input:

if (timeToExit(line)) break;

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:

while (true) {
    ...
    System.out.print("Enter the first X coordinate: ");
    String x1 = input.nextLine();
    if (timeToExit(x1)) break;
    double x1_d = Double.parseDouble(x1);
    ...
}
静待花开 2024-10-04 15:50:32

只是一些伪代码:

while (! input.equals("q") )
// do something

如果用户输入 q,则 input.equals("q") 返回 true,然后将其取反并中断循环。

否则,用户输入另一个数字,例如 44,input.equals("q") 等于 false,该值被否定并且循环继续。

just some pseudocode:

while (! input.equals("q") )
// do something

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.

缘字诀 2024-10-04 15:50:32

我在你的代码中没有看到循环...:s

但是,你为什么不试试这个:

while(true)
{
  string input = // I don't remember the code to create a stream for standard input
  if(input == "q"){
    break;
  }
  else{
    java.util.Scanner inputWithNumbers = new java.util.Scanner(input);
    //---! All math operations here
  }
}

I don't see a loop in your code... :s

But, why don't you try this:

while(true)
{
  string input = // I don't remember the code to create a stream for standard input
  if(input == "q"){
    break;
  }
  else{
    java.util.Scanner inputWithNumbers = new java.util.Scanner(input);
    //---! All math operations here
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文