2016-03-17 33 views
0
import java.util.*; 
import java.util.Scanner; 
import java.io.*; 


class Point 
{ 
    /* Method to find the quadrant of both the points p and q*/ 

    public String quadrant(double xp, double yp, double xq, double yq){ 


     Scanner keyboard= new Scanner(System.in); 
     System.out.print("Enter the first value for Xp: "); 
     xp = keyboard.nextDouble(); 
     Scanner keyboard1 = new Scanner(System.in); 
     System.out.print("Enter the first value for Yp: "); 
     yp = keyboard.nextDouble(); 
     Scanner keyboard2= new Scanner(System.in); 
     System.out.print("Enter the first value for Xq: "); 
     xq = keyboard.nextDouble(); 
     Scanner keyboard3= new Scanner(System.in); 
     System.out.print("Enter the first value for Yq: "); 
     yq = keyboard.nextDouble(); 
     String p_quadrant=getQuadrant(xp,yp); 
     String q_quadrant=getQuadrant(xq,yq); 
     return "Point p is at "+p_quadrant+" and Point q is at "+q_quadrant; 
    } 

    /* Method to get the quadrant of each passed point*/ 
    public String getQuadrant(double x, double y){ 
     if(x==0 && y==0){ 
      return "Origin"; 
     } 
     else if(x==0){ 
      return "Y-axis"; 
     } 
     else if(y==0){ 
      return "X-axis"; 
     } 
     if (x >= 0) { 
    return (y >= 0 ? "1st Quadrant":"4th Quadrant"); 
     } else { 
return (y >= 0 ? "2nd Quadrant":"3rd Quadrant"); 
    } 

    } 
    /* Method to get the euclidean distance between p and q */ 
    public double euclidean(double xp, double yp, double xq, double yq){ 
    double euc_distance = 0.0; 

    double x_square=Math.pow((xq-xp), 2); 
    double y_square=Math.pow((yq-yp), 2); 
    euc_distance= Math.sqrt(x_square+y_square); 

    return euc_distance; 
    } 

    /* Method to calculate the slope */ 
    public double slope(double xp, double yp, double xq, double yq){ 

     double x_diff= xp-xq; 
     double slope=0.0; 

     /* Check applied to avoid a divide by zero error */ 
     if(x_diff == 0){ 
      System.out.println("Slope is undefined"); 
      System.exit(1); 
     } 
     else{ 
      slope=(yp-yq)/x_diff; 
     } 
     return slope; 
    } 


    public static void main (String[] args) throws java.lang.Exception 
    { 

    /* Creating an object of Points and calling each method individually and printing the value*/ 
    Points p = new Points(); 
    double euc=p.euclidean(2.3, 5.6,0.5,9); 
    String quad=p.quadrant(0, -5.6,0,0); 
    double slop=p.slope(0,0.5,0.6,9); 
    System.out.print("Euclidean:"+euc+"\n Quadrant:"+quad+"\n Slope:"+slop); 
    } 
} 

我找不出爲什麼我的掃描儀不工作;我也沒有得到任何錯誤。我的工作是要求用戶輸入所有要點。真的,我被卡住了,這是在幾個小時內發生的,而且我正在使用最新的eclipse和新的JDK。新編程和本網站XD。似乎不能讓我的掃描儀工作

當我運行程序時,我得到了這個結果;我沒有得到任何錯誤,要麼 歐幾里得:3.847076812334269 象限:點P是Y軸,點Q是在原產地 坡:14.166666666666668

+2

*我的掃描儀不工作*沒有絲毫描述您的問題。 – shmosel

+0

您需要清楚地描述'whats not working',包括期望的輸出和任何錯誤/代碼中發生錯誤的位置。 – pczeus

+0

使用next(),nextInt()或其他nextFoo()方法之後跳過nextLine()的可能重複(http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint- or-other-nextfoo-methods) –

回答

0

你需要更新你的提示,擺脫不必要的掃描儀的修復問題。您的提示都要求提供「第一個」值,並且keyboard1 -keyboard3從不使用。此代碼的工作一樣好:

Scanner keyboard= new Scanner(System.in); 
    System.out.print("Enter the first value for Xp: "); 
    xp = keyboard.nextDouble(); 
    System.out.print("Enter the first value for Yp: "); 
    yp = keyboard.nextDouble(); 
    System.out.print("Enter the first value for Xq: "); 
    xq = keyboard.nextDouble(); 
    System.out.print("Enter the first value for Yq: "); 
    yq = keyboard.nextDouble(); 
    String p_quadrant=getQuadrant(xp,yp); 
    String q_quadrant=getQuadrant(xq,yq); 
    return "Point p is at "+p_quadrant+" and Point q is at "+q_quadrant; 
} 

而且,在你的主要方法,而不是創建一個點()對象創建一個點()對象。它確實給了我一個錯誤。

+0

現在我收到了一個類錯誤,它說「類型點已經定義。」 – MrYOLOYOYo

+0

nvm它的工作,但我現在的結果是錯誤的;掃描儀正在工作,但現在我的價值沒有被使用。爲XP輸入第一個值:20 爲YP進入第一值:21 爲Xq的輸入的第一個值:36 爲Y染色體長臂進入第一值:23 歐幾里德:3.847076812334269 象限:點P是第一象限和點q在第一象限 坡度:14.166666666666668 – MrYOLOYOYo

+0

我不太確定你在做什麼。你需要解釋你期望的輸出結果,並且更清楚地解釋這個問題。 我可以看到你的上面的代碼是這樣的:你的getQuadrant()方法由一系列if()語句組成。您輸入的數字都大於1,因此執行的代碼是第38-40行的if()語句。這就是爲什麼你得到「象限1」來換取你的價值。除此之外,您需要詳細說明您要完成的任務。希望有所幫助。祝你好運。 –