2017-11-25 83 views
-1

因此我有一個學校項目,需要我編寫代​​碼並實現方法來證明用戶輸入的3個座標集合將是某個三角形。代碼主外部距離我的3分

我試圖寫一行代碼來獲得我的3點P1,P2,P3之間的距離。我不知道我是如何參考我建立的座標,如x1,x2,x3,y1,y2,y3。 這裏是我的代碼:

import javax.swing.JOptionPane; 

public class EpsteinProj3 
{ 
    public static void main (String args[]) 
    { 
    String input1 = JOptionPane.showInputDialog("Enter coordinate A"); 
    String input2 = JOptionPane.showInputDialog("Enter coordinate B"); 
    String input3= JOptionPane.showInputDialog("Enter coordinate C"); 
    System.out.println("Your Triangle is: " + input1 + " " + input2 + " " + input3); 

    //double temp = Double.parseDouble(input1); 
    //System.out.println("" + temp); 

    //double temp1 = Double.parseDouble(input2); 
    //System.out.println("" + temp1); 

    //double temp2 = Double.parseDouble(input3); 
    // System.out.println("" + temp2); 

     Double x1, x2, x3; 
     Double y1, y2, y3; 

     x1 = Double.parseDouble(input1.substring(1, input1.indexOf(","))); 
     y1 = Double.parseDouble(input1.substring(input1.indexOf(",") +1, input1.length()-1)); 
     Point P1 = new Point(x1, y1); 

     x2 = Double.parseDouble(input2.substring(1, input2.indexOf(","))); 
     y2 = Double.parseDouble(input2.substring(input2.indexOf(",") +1, input2.length()-1)); 
     Point P2 = new Point(x2, y2); 

     x3 = Double.parseDouble(input3.substring(1, input3.indexOf(","))); 
     y3 = Double.parseDouble(input3.substring(input3.indexOf(",") +1, input3.length()-1)); 
     Point P3 = new Point(x3, y3); 


     System.out.println("Point A is " + P1.toString()); 
     System.out.println("Point B is " + P2.toString()); 
     System.out.println("Point C is " + P3.toString()); 

     //scalene, isosceles, right 


} 

public static double distanceBetween() 
{ 
    double Dis1 = Math.sqrt((x1-x2)^2 + (y1- y2)^2); 
    Dis1 = Math.sqrt((x1-x2)^2 + (y1- y2)^2); 
} 

} 
+0

什麼是確切的錯誤和堆棧跟蹤? – Li357

+0

這是因爲括號不是有效數字的一部分。它應該是'input.substring(1,input.indexOf(「,」))''和'input.substring(input.indexOf(「,」)+ 1,input.length - 1)'以排除雙方括號。 – Li357

+0

此問題與正在使用的IDE無關。無需添加標籤。 –

回答

-2

那是因爲你開始從字符串的第一個元素讀書。 如果您確信該格式將永遠是(POSX,波西),然後

x = Double.parseDouble(input1.substring(1, input1.indexOf(","))); 
    y = Double.parseDouble(input1.substring(input1.indexOf(",") +1, input1.length()-1)); 
+0

最後一個括號如何? – Li357

+0

但是用你的代碼替換了我的x和y代碼,現在我的錯誤是:線程「main」中的異常java.lang.NumberFormatException:對於輸入字符串:「5.0)」 –

+0

抱歉,不記得最後一次parentesis。看到我編輯的答案@LionelEpstein – joao86

0

我使用解析點following正則表達式。這允許可選(),後的可選空間。

請注意,此示例使用java.awt.Point,因此您應該將Integer.parseInt替換爲Double.parseDouble以實現擺動。

public class StringToPoint { 
private static final Pattern POINT_PATTERN = Pattern.compile("(?:\\()?([0-9]+)?,(?:)?([0-9]+)?(?:\\))?"); 

private static Point parsePoint(String point){ 
    Matcher matcher = POINT_PATTERN.matcher(point); 
    if (!matcher.matches() || matcher.groupCount()!=2){ 
     throw new IllegalArgumentException(String.format("%s cannot be parsed as point",point)); 
    } 
    return new Point(
     Integer.parseInt(matcher.group(1)), 
     Integer.parseInt(matcher.group(2)) 
    ); 
} 

@Test 
public void test(){ 
    TestCase.assertEquals(new Point(123,456), parsePoint("(123,456)")); 
    TestCase.assertEquals(new Point(123,456), parsePoint("(123, 456)")); 
    TestCase.assertEquals(new Point(123,456), parsePoint("123, 456")); 
    TestCase.assertEquals(new Point(123,456), parsePoint("123,456")); 
} 
} 
+0

我欣賞幫助,但它不完全是我在找什麼。我試圖讓我的3點P1,P2和P3之間的距離。我做了一個方法:public static double distanceBetween(Point P1,Point P2)試圖獲得這兩個點之間的距離。但它裏面我有雙Dis1 = Math.sqrt((x1-x2)^ 2 +(y1-y2)^ 2); Dis1 = Math.sqrt((x1-x2)^ 2 +(y1-y2)^ 2);這是錯誤的,因爲X1和Y1等不被識別 –

+0

不,您編輯的問題要求點之間的距離。原來的問題是關於從字符串解析點。請不要編輯問題以提出另一個問題,而是打開另一個問題。這就是我爲什麼低估了你的問題。 – Bedla

+0

即時通訊如果我沒有遵守規則,我很抱歉。我原本提出了一個新問題,但是這讓我在詢問第一個問題後等了90分鐘。我只是想找到一種方法來增加這個問題。抱歉 –