2014-09-30 21 views
0

我正在研究一個允許用戶輸入x和y的程序。用戶輸入x和y之後,程序會將這兩個點顯示爲一個有序對,並告訴用戶該點位於哪個象限內,如果它位於y軸的左側或右側,並且該點位於下方或上方x軸。輸出的一些例子將包括:關於我的程序關於圖上的一個點的問題

Enter x: 3.4 
Enter y: -8.2 
Point: (3.4, -8.2) 
The point is below the x-axis. 
The point is to the right of the y-axis. 
The point lies in quadrant IV. 

Enter x: 3 
Enter y: 0 
Point: (3, 0) 
The point is on the x-axis. 
The point is to the right of the y-axis. 

Enter x: 0 
Enter y: 0 
Point: (0, 0) 
The point is on the origin. 

這裏是我的代碼

import java.util.Scanner; 

public class Graph 
{ 
    public static void main(String[] args) 
    { 
     //declare variables 
     int x; 
     int y; 
     Scanner reader = new Scanner (System.in); // set up scanner to read user inputs 

     // prompt x and y 
     System.out.print ("Enter x:"); 
     x = reader.nextInt(); 

     System.out.print ("Enter y:"); 
     y = reader.nextInt(); 

     //display point 
     System.out.println("\nPoint: ("+x+","+y+")"); 


     //determine where the point is on a graph 
     if(x > 0 || x < 0 && y > 0) 
     System.out.println("\nThe point is above the x-axis."); 
      else 
     if (x > 0 || x < 0 && y < 0) 
      System.out.println("\nThe point is below the x-axis."); 


     if(x < 0 && y > 0 && y < 0) 
      System.out.println("\nThe point is to the left of the y-axis."); 
     else 
     if (x > 0 && y > 0 && y < 0) 
      System.out.println("\nThe point is to the right of the y-axis."); 
     else 
     if(x < 0 && y < 0) 
      System.out.println("\nThe point is in Quadrant III."); 
     else 
     if(x < 0 && y > 0) 
      System.out.println("\nThe point is in Quadrant II."); 
     else 
     if (x > 0 && y > 0) 
      System.out.println("\nThe point is in Quadrant I."); 
     else 
     if (x > 0 && y < 0) 
      System.out.println("\nThe point is in Quadrant IV."); 

     if (x == 0 && y == 0) 
      System.out.println("\nThe point is at the origin."); 
     if (y==0 && x < 0 || x > 0) 
      System.out.println("\nThe point is on the x-axis."); 
     else 
     if (x==0 && y < 0 || y > 0) 
      System.out.println("\nThe point is on the y-axis."); 
    }// end class 
} 

爲我種我將不勝感激幫助的鐵鏽使用java,謝謝! :)

+4

究竟是什麼問題?您需要充分描述您的問題併發布任何相關錯誤。 – 2014-09-30 23:21:08

+0

@BoristheSpider哦,是的,我忘了包括這一點。那麼當我編譯它說沒有錯誤,但是當我運行程序並鍵入(2,2)例如它給我 點:(2,2) 點是在x軸上方 點在象限我 點是在x軸 當我運行(3,0) 點是x軸 點是在x軸 它並沒有真正的工作之上正確的是我想說的 – jugo 2014-09-30 23:24:35

+2

@ jugo你應該編輯問題來添加任何附加信息。 – 2014-09-30 23:26:26

回答

0

問題:您的問題與點(3, 0)(和一般的)是你的一個點是否x軸以上資質應在y coordinate僅僅依賴。但是,您的支票取決於x coordinatey coordinate

實施例:

if(x > 0 || x < 0 && y > 0) 
    System.out.println("\nThe point is above the x-axis."); 

這首先檢查是否x > 0(3, 0),這是真實的,因此它說,該點在x軸的上方,即使y座標是不> 0並且這個檢查應該失敗。如果第一個表達式爲真,||語句將不會檢查運算符的兩側。

解決方案: 更改您的代碼只檢查x-coordinate當你需要看到的一點是相對於y-axis並檢查y-coordinate當你需要看到的一點是相對於x-axis

下面是代碼應如何與註釋解釋:

// The x coordinate does not affect if it is above or below the x-axis 
// Only check to see what the y coordinate is here 
if (y > 0) { 
    System.out.println ("\nThe point is above the x-axis."); 
} else if (y < 0) { 
    System.out.println ("\nThe point is below the x-axis."); 
} // Here you could also add a check to see if y == 0, the it is on the x-axis 

// Similarly, the y coordinate has nothing to do with what side of the 
// y-axis the point is. Check only the x coordinate here. 
if(x < 0) 
    System.out.println ("\nThe point is to the left of the y-axis."); 
else if (x > 0 && y > 0 && y < 0) 
    System.out.println ("\nThe point is to the right of the y-axis."); 

// According to your example desired output, this if-else block should be 
// separate from the one above because you always want one of these lines to 
// print 
if(x < 0 && y < 0) 
    System.out.println ("\nThe point is in Quadrant III."); 
else if(x < 0 && y > 0) 
    System.out.println ("\nThe point is in Quadrant II."); 
else if (x > 0 && y > 0) 
    System.out.println("\nThe point is in Quadrant I."); 
else if (x > 0 && y < 0) 
    System.out.println ("\nThe point is in Quadrant IV."); 

if (x == 0 && y == 0) 
    System.out.println ("\nThe point is at the origin."); 
// Again no need to check x here 
if (y==0) 
    System.out.println ("\nThe point is on the x-axis."); 
// Again no need to check y here 
else if (x==0) 
    System.out.println ("\nThe point is on the y-axis."); 
+0

感謝您的幫助! – jugo 2014-10-01 00:03:06

0

我想我看到了錯誤代碼。您的if語句查找點的位置不正確。您在其中一人有這樣的:

if(x > 0 || x < 0 && y > 0) 

所有你需要你的if語句

if(y > 0) 

因爲如果y> 0,點在x軸之上,無論價值來決定的x是。所以,你的if語句應該是這樣的:

if(x > 0) System.out.println("The point is above the y-axis."); 
else if(x < 0) System.out.println("The point is below the y-xis."); 
if(y > 0) System.out.println("The point is above the x-axis."); 
else if(y < 0) System.out.println("The point is below the x-axis."); 
if(x == 0) System.out.println("The point is on the y-axis."); 
if(y == 0) System.out.println("The point is on the x-axis."); 
if(x == 0 && y == 0) System.out.println("The point is on the origin."); 

您的象限代碼是正確的,但刪除別人的:

if(x < 0 && y < 0) 
      System.out.println ("\nThe point is in Quadrant III."); 
if(x < 0 && y > 0) 
      System.out.println ("\nThe point is in Quadrant II."); 
if (x > 0 && y > 0) 
      System.out.println("\nThe point is in Quadrant I."); 
if (x > 0 && y < 0) 
      System.out.println ("\nThe point is in Quadrant IV."); 
+0

不客氣,我很高興它爲你工作。 – 2014-10-01 00:04:53

0

此:

if(x < 0 && y > 0 && y < 0) 
    System.out.println("\nThe point is to the left of the y-axis."); 

else if (x > 0 && y > 0 && y < 0) 
    System.out.println("\nThe point is to the right of the y-axis."); 

will allways評估爲假。 y不能同時爲> 0y < 0