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

时间:2017-02-23 03:44:25

标签: java loops input java.util.scanner

对于这个问题,我需要找到两个点之间的距离给定两个点的坐标和值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很新,还在学习,所以我感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您可以使用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);      
}