.useDelimiter 解析逗号但不解析负号
所以我查看了几个相关的问题,但似乎仍然找不到我的答案(我猜是因为它是具体的)。我正在尝试在 Java 中使用 Scanner.useDelimiter 方法,但无法让它正常工作...这是我的困境...
我们应该编写一个程序,它采用 X、Y 坐标并计算两点之间的距离。显然,一种解决方案是分别扫描每个 x 和 y 坐标,但这对我来说很草率。我的计划是要求用户输入坐标“x,y”,然后使用 Scanner.nextInt() 方法获取整数。但是,我必须找到一种方法来忽略“,”,当然,我可以使用 useDelimiter 方法来做到这一点。
根据其他线程,我必须理解正则表达式(还没有)才能放入 useDelimiter 方法,并且我已经让它忽略逗号,但是,用户有可能输入负数作为坐标(其中技术上是正确的)。如何让 useDelimiter 忽略逗号,但仍然识别负号?
这是我第一次来这里,这是我的代码:
import java.util.Scanner;
import java.text.DecimalFormat;
public class PointDistanceXY
{
public static void main(String[] args)
{
int xCoordinate1, yCoordinate1, xCoordinate2, yCoordinate2;
double distance;
// Creation of the scanner and decimal format objects
Scanner myScan = new Scanner(System.in);
DecimalFormat decimal = new DecimalFormat ("0.##");
myScan.useDelimiter("\\s*,?\\s*");
System.out.println("This application will find the distance between two points you specify.");
System.out.println();
System.out.print("Enter your first coordinate (format is \"x, y\"): ");
xCoordinate1 = myScan.nextInt();
yCoordinate1 = myScan.nextInt();
System.out.print("Enter your second coordinate (format is \"x, y\"): ");
xCoordinate2 = myScan.nextInt();
yCoordinate2 = myScan.nextInt();
System.out.println();
// Formula to calculate the distance between two points
distance = Math.sqrt(Math.pow((xCoordinate2 - xCoordinate1), 2) + Math.pow((yCoordinate2 - yCoordinate1), 2));
// Output of data
System.out.println("The distance between the two points specified is: " + decimal.format(distance));
System.out.println();
}
}
感谢您的帮助,我期待着帮助其他人!
So I have looked at a couple of related questions, but still can't seem to find my answer (I guess because it's specific). I'm trying to use the Scanner.useDelimiter method in Java and I can't get it to work properly... here is my dilemma...
We are supposed to write a program that takes a X, Y coordinate and calculates the distance between the two points. Obviously, one solution is to scan for each x and y coordinate separately, but this is sloppy to me. My plan is to ask the user to input the coordinate as "x, y" and then grab the integers using the Scanner.nextInt() method. However, I have to find a way to ignore the "," and of course, I can do that with the useDelimiter method.
According to other threads, I have to understand regex (not there yet) to put in the useDelimiter method and I've got it to ignore the commas, HOWEVER, there is a possibility that a user inputs a negative number as a coordinate (which is technically correct). How do I get useDelimiter to ignore the comma, but still recognize the negative sign?
This is my first time on here, here is my code:
import java.util.Scanner;
import java.text.DecimalFormat;
public class PointDistanceXY
{
public static void main(String[] args)
{
int xCoordinate1, yCoordinate1, xCoordinate2, yCoordinate2;
double distance;
// Creation of the scanner and decimal format objects
Scanner myScan = new Scanner(System.in);
DecimalFormat decimal = new DecimalFormat ("0.##");
myScan.useDelimiter("\\s*,?\\s*");
System.out.println("This application will find the distance between two points you specify.");
System.out.println();
System.out.print("Enter your first coordinate (format is \"x, y\"): ");
xCoordinate1 = myScan.nextInt();
yCoordinate1 = myScan.nextInt();
System.out.print("Enter your second coordinate (format is \"x, y\"): ");
xCoordinate2 = myScan.nextInt();
yCoordinate2 = myScan.nextInt();
System.out.println();
// Formula to calculate the distance between two points
distance = Math.sqrt(Math.pow((xCoordinate2 - xCoordinate1), 2) + Math.pow((yCoordinate2 - yCoordinate1), 2));
// Output of data
System.out.println("The distance between the two points specified is: " + decimal.format(distance));
System.out.println();
}
}
Thanks for your help and I look forward to helping other people down the line!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为单独询问 x 和 y 会更容易(对于命令行类型的程序来说更传统)
示例:
但是,如果您坚持同时执行这两个操作并使用分隔符,您可以尝试使用返回行作为分隔符而不是“,”,因为您必须将其分隔两次,记住,一次在 x 之后,然后在 y 之后再次。但这会让你回到相同的结果。问题是如果你想使用分隔符并同时接收它,你需要分隔两次。我建议改为查看字符串的 .split 函数。
另一种方法是使用 .split(", ");函数,其中“,”是分隔符。
示例:
希望这有帮助,祝您愉快。
I think it would be easier (and more conventional for the command line type of programs) to just ask for x and y separately
Example:
However if you insist on doing both at the same time and using a delimeter you could try using the return line as a delimeter instead of the ", " because you would have to delimit it twice remember, once after x and then again after y. But that sort of brings you back to the same result. The problem is that you need to delimit it twice if you want to use a delimeter and take it in at the same time. I'd suggest taking a look at the .split function of a string instead.
Another approach would be to use the .split(", "); function where ", " is your delimiter.
Example:
Hope this helps, Enjoy.