论坛首页 Java企业应用论坛

为什么条件判断总是跳出

浏览 2365 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-08-28  
IF条件判断中,总是无法将Scanner输入的字符串与“YES”进行比较,结果总是执行System.exit(0);个人无法解决,请有经验的Coder来帮忙看看。

import java.util.*;
import java.lang.Math;

public class TestPoint{
public static void main(String[] args){

System.out.println("please enter the x,y,z of a point:");
Scanner inX = new Scanner(System.in);
double inputX = inX.nextDouble();
Scanner inY = new Scanner(System.in);
double inputY = inX.nextDouble();
Scanner inZ = new Scanner(System.in);
double inputZ = inX.nextDouble();

System.out.println("the point you just have inputed is : x = " + inputX + ", y = " + inputY + ", z = " + inputZ);

Point p1 = new Point(inputX, inputY, inputZ);

System.out.println("the distance is :" + p1.getDistance(p1));
System.out.println("do you wanna change the value of x, y, z, do you? ");
Scanner in = new Scanner(System.in);
String inIf = in.next();
String YES = "YES";

if(inIf == YES){

Scanner chX = new Scanner(System.in);
double changeX = chX.nextDouble();

Scanner chY = new Scanner(System.in);
double changeY = chY.nextDouble();

Scanner chZ = new Scanner(System.in);
double changeZ = chZ.nextDouble();

p1.modifyPoint(changeX, changeY, changeZ);
System.out.println("the distance after modified is :" + p1.getDistance(p1));
}
else{
System.exit(0);
}

}
}

class Point{
Point(double x, double y, double z){
this.x = x;
this.y = y;
this.z = z;
}

public void modifyPoint(double _x ,double _y ,double _z){
x = _x;
y = _y;
z = _z;
}


public double getDistance(Point point){
result = Math.sqrt(point.x * point.x + point.y * point.y + point.z * point.z);
return result;
}

private double result;
private double x;
private double y;
private double z;
}
   发表时间:2014-08-28  
if(inIf == YES){ 改成 if(YES.equals(inIf))
1 请登录后投票
   发表时间:2014-08-28  
ssunny 写道
if(inIf == YES){ 改成 if(YES.equals(inIf))

解决了,回去看书。。。谢谢您了!
0 请登录后投票
   发表时间:2014-09-01  
equals
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics