我正在开发一个允许用户输入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时有点生气,谢谢! :)
答案 0 :(得分:0)
问题:点(3, 0)
(一般情况下)问题是,如果某点高于x轴,您的资格应仅取决于{{1} }。但是,您的if检查取决于y coordinate
和x coordinate
。
示例:强>
y 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
{1}}
以下是代码应如何与注释解释:
x-axis
答案 1 :(得分:0)
我想我在你的代码中看到了错误。用于查找点位置的if语句不正确。你有其中一个:
if( x > 0 || x < 0 && y > 0 )
您在if语句中所需要的只是
if(y > 0)
因为如果y&gt; 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.");
答案 2 :(得分: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.");
将始终评估为false。 y
不能同时为> 0
和y < 0