2017-02-23 11 views
1

この問題では、数式を使用して両方の点の座標と値pの2点間の距離を求める必要があります。私は1つの入力行のために働くプログラムを手に入れましたが、私はユーザが複数の行を入力してプログラムをループさせることを望みます。そう例えば:Javaで複数の入力行をループする方法

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にかなり新しいですし、まだ私は私が得ることができる任意の助けに感謝ので学習:これは私が今持っているコードです。前もって感謝します。

+0

を反復処理するためにforループを使用することができます最初の行と同じ方法で処理します。 –

+0

どのようなループを使用するかは、ループの終了方法によって異なります。あなたはそれを固定回数回したいのですか、あるいはユーザーが何らかのセンチネル値を入力したときにループを終了したいのですか? –

+0

各行を処理するコードをメソッドに移動する必要があります。あなたがEclipseのようなIDEを使用しているなら、かなり痛みを伴わずにそれをやり遂げます。 –

答えて

0

最初だけ `input.nextLine`を使用して別の行を読んで、あなたは二重の値を取得するためにinput.nextDouble()を使用することができますし、誤入力を処理するためにtrycatchを使用することができますし、あなたが入力

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);  
} 
関連する問題