我在标题中编写了问题的代码,似乎遇到了一些问题。任何人都可以给我一些关于我做错事的见解或提示。特别是代码中的“if ... else”部分。
这是问题#9。如果单击该链接,它将显示问题的打印输出。 http://s21.postimg.org/nl2tmf5tj/Screen_Shot_2013_09_21_at_6_44_46_PM.png
这是我的代码:
import java.util.*;
public class Ch3Ex9 {
/**
* This method asks user for an x,y coordinates of a point, then returns the
* distance to the origin and which quadrant the point is in.
*/
public static void main(String[] args)
{
double xCoord; //x Coordinant initialized to 3
double yCoord; //y Coordinant initalized to 4
double hypo; //hypotenuse
//declare an instance of Scanner to read the datastream from the keyboard
Scanner keyboard = new Scanner(System.in);
//get x Coordinant from the user
System.out.print("Please enter the X coordinant: ");
xCoord = keyboard.nextDouble();
//get Y Coordinate from the user
System.out.print("Please enter the Y coordinant: ");
yCoord = keyboard.nextDouble();
//calculate the hypotenuse which is the length to the origin
hypo = Math.hypot(xCoord, yCoord);
System.out.println("The hypotenuse is "+hypo);
//determine which Quadrant the user-inputted point resides in
if (xCoord>0) && (yCoord>0) //
System.out.println("Point lies in Quadrant I.");
else if (xCoord<0) && (yCoord>0)
System.out.println("Point lies in Quadrant II.");
else if (xCoord<0) && (yCoord<0)
System.out.println("Point lies in Quadrant III.");
else if (xCoord>0) && (yCoord<0)
System.out.println("Point lies in Quadrant IV.")
}
}
答案 0 :(得分:4)
括号太多了。变化
if (xCoord>0) && (yCoord>0)
到
if (xCoord>0 && yCoord>0)
和其他人一样。