gpt4 book ai didi

java - 如何在Java中循环遍历多个输入行

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:24 27 4
gpt4 key购买 nike

对于这个问题,我需要使用公式在给定两个点的坐标和 p 值的情况下找到两点之间的距离。我让程序适用于一个输入行,但我希望用户能够输入多行并让程序循环遍历它们。例如:

1.0 1.0 2.0 2.0 1.0

1.0 1.0 2.0 2.0 2.0

可能是输入,程序将读取这两行并给出两个不同的距离值。我想知道,如何让扫描仪读取多行输入,然后循环遍历它们?这是我现在的代码:

public class Driver_lab3{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter 2 coordinate points and a p value, as follows : x1 y1 x2 y2 p : ");
String coordinatestring = input.nextLine();

int stringposition = 0;
double x1 = 0.0;
double x2 = 0.0;
double y1 = 0.0;
double y2 = 0.0;
double p = 0.0;
String temp = "temp";
String spacecheck = "space";
String subb4decimal = "temp sub";
String subafterdecimal = "temp sub2";
int decimalpos = 0;
int spacepos = 0;
double distance;


if(Character.isDigit(coordinatestring.charAt(stringposition))){
decimalpos = coordinatestring.indexOf('.', stringposition);
subb4decimal = coordinatestring.substring(stringposition, decimalpos);
spacepos = coordinatestring.indexOf(' ', decimalpos);
subafterdecimal = coordinatestring.substring(decimalpos, spacepos);

temp = subb4decimal + subafterdecimal;
x1 = Double.parseDouble(temp);
stringposition = spacepos + 1;

decimalpos = coordinatestring.indexOf('.', stringposition);
subb4decimal = coordinatestring.substring(stringposition, decimalpos);
spacepos = coordinatestring.indexOf(' ', decimalpos);
subafterdecimal = coordinatestring.substring(decimalpos, spacepos);

temp = subb4decimal + subafterdecimal;
y1 = Double.parseDouble(temp);
stringposition = spacepos + 1;

decimalpos = coordinatestring.indexOf('.', stringposition);
subb4decimal = coordinatestring.substring(stringposition, decimalpos);
spacepos = coordinatestring.indexOf(' ', decimalpos);
subafterdecimal = coordinatestring.substring(decimalpos, spacepos);

temp = subb4decimal + subafterdecimal;
x2 = Double.parseDouble(temp);
stringposition = spacepos + 1;

decimalpos = coordinatestring.indexOf('.', stringposition);
subb4decimal = coordinatestring.substring(stringposition, decimalpos);
spacepos = coordinatestring.indexOf(' ', decimalpos);
subafterdecimal = coordinatestring.substring(decimalpos, spacepos);

temp = subb4decimal + subafterdecimal;
y2 = Double.parseDouble(temp);
stringposition = spacepos + 1;

decimalpos = coordinatestring.indexOf('.', stringposition);
subb4decimal = coordinatestring.substring(stringposition, decimalpos);
spacepos = coordinatestring.indexOf(' ', decimalpos);
subafterdecimal = coordinatestring.substring(decimalpos, spacepos);

temp = subb4decimal + subafterdecimal;
p = Double.parseDouble(temp);
stringposition = spacepos + 1;

distance = Math.pow(((Math.pow((Math.abs(x1-x2)),p)) + (Math.pow((Math.abs(y1-y2)),p))),(1.0/p));
System.out.println(distance);
System.out.println(x1);
System.out.println(x2);
System.out.println(y1);
System.out.println(y2);
System.out.println(p);

stringposition = 0;
}
}
}

我对 java 还很陌生,并且仍在学习,所以我很感谢我能得到的任何帮助。提前致谢。

最佳答案

首先你可以使用input.nextDouble()来获取 double 值,你可以使用try catch来处理不正确的输入,你可以使用for循环来迭代输入

for (int i = 0; i < numOfInputs; i++){
try{
x1 = s.nextDouble();
y1 = s.nextDouble();
x2 = s.nextDouble();
y2 = s.nextDouble();
p = s.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Incorrect input");
}

// your logic here

distance = Math.pow(((Math.pow((Math.abs(x1-x2)),p)) + (Math.pow((Math.abs(y1-y2)),p))),(1.0/p));
System.out.println(distance);
System.out.println(x1);
System.out.println(x2);
System.out.println(y1);
System.out.println(y2);
System.out.println(p);
}

关于java - 如何在Java中循环遍历多个输入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42406611/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com