2017-02-23 53 views
1

對於這個問題,我需要使用公式找到給定兩個點的座標和值p的兩點之間的距離。我得到了一個輸入行的程序,但我希望用戶能夠輸入多行並讓程序循環。因此,例如:如何在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

只要閱讀使用'input.nextLine'另一條線並按照第一行的相同方式處理它。 –

+0

你使用什麼樣的循環取決於你想如何結束循環。你是否想要固定次數,或者當用戶鍵入某個標記值時是否要退出循環。 –

+0

您應該將處理每行代碼移動到方法中。如果你使用Eclipse這樣的IDE,它會很輕鬆地爲你做。 –

回答

0

首先,你可以使用input.nextDouble()獲得雙重價值,你可以用trycatch處理不正確的輸入,你可以使用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);  
} 
相關問題