[英]Cant seem to get my scanner working
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);
}
}
我不知道为什么我的扫描仪不工作。 我也没有错误。 我的工作是要求用户输入所有要点。 真的,我被卡住了,这应该在几个小时内完成,而且我正在使用带有新JDK的最新Eclipse。 编程和本站点XD的新手。
当我运行程序时,我得到了这个结果。 Euclidean:3.847076812334269象限:点p在Y轴上,点q在原点斜率上:14.166666666666668,我也没有任何错误
您需要更新提示,并摆脱不必要的扫描仪以解决此问题。 您的提示都要求输入“ first”值,并且从不使用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;
}
另外,在您的main方法中,您不是创建Point()对象,而是创建Points()对象。 它确实给我一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.