我有一个相当大的程序我正在研究,我无法弄清楚为什么一个while语句(在一个方法内)正在执行,当方法本身工作时。我知道方法本身是正确的,但程序的其余部分正在使其执行。
所以我的问题是,为了尝试解决这个问题,我应该寻找什么?
请记住,这只是我整个程序的一小段,以下代码可以使用!
以下是我的代码的问题区域:
import java.util.Scanner;
public class BoatTest
{
private double boatPrice;
private double downPayment;
private boolean boatIsNew;
//default constructor
public BoatTest()
{
boatPrice = 0.0;
downPayment = 0.0;
boatIsNew = false;
}//end method
//non-default constructor
public BoatTest(double boatPricePassed, double downPaymentPassed,
boolean boatIsNewPassed)
{
setBoatPrice(boatPricePassed);
setDownPayment(downPaymentPassed);
setBoatIsNew(boatIsNewPassed);
}//end method
//setters
public void setBoatPrice(double boatPricePassed)
{
Scanner keyboard = new Scanner(System.in);
//test > 0
while(boatPricePassed < 0)
{
System.out.println("Invalid, must be >= 0. ");
System.out.println("Please enter the boat price:");
boatPricePassed = keyboard.nextDouble();
}//end while
boatPrice = boatPricePassed;
}//end method
public void setDownPayment(double downPaymentPassed)
{
Scanner keyboard = new Scanner(System.in);
//test > 0
while(downPaymentPassed < 0)
{
System.out.println("Invalid, must be >= 0. ");
System.out.println("Please enter the down payment:");
downPaymentPassed = keyboard.nextDouble();
}//end while
//test if used > .30
while((downPaymentPassed < boatPrice * .30) && (boatIsNew == false))
{
System.out.println("Invalid, must be > " + (boatPrice * .30) + ".");
System.out.println("Please enter the down payment:");
downPaymentPassed = keyboard.nextDouble();
}//end while
downPayment = downPaymentPassed;
}//end method
public void setBoatIsNew(boolean boatIsNewPassed)
{
boatIsNew = boatIsNewPassed;
}//end method
//getters
public double getBoatPrice()
{
return boatPrice;
}//end method
public double getDownPayment()
{
return downPayment;
}//end method
public boolean getBoatIsNew()
{
return boatIsNew;
}//end method
//output
public String toString()
{
return
"\nBoat Price: " + boatPrice +
"\nDown Payment: " + downPayment +
"\nBoat Is New: " + boatIsNew;
}//end method
}//end class
以下是主要方法:
public class BoatTestMain
{
public static void main(String[] args)
{
System.out.println("Create first instance of Boat class");
BoatTest b1 = new BoatTest (29900, 9900, false);
System.out.println("Create second instance of Boat class");
BoatTest b2 = new BoatTest (10000, 0, false);
System.out.println("The first boat information:");
System.out.println(b1.toString());
System.out.println("The second boat information:");
System.out.println(b2.toString());
}//end main
}//end class
在setDownPayment中,具体来说:
while((downPaymentPassed < boatPrice * .30) && (boatIsNew == false))
{
System.out.println("Invalid, must be > " + (boatPrice * .30) + ".");
System.out.println("Please enter the down payment:");
downPaymentPassed = keyboard.nextDouble();
}//end while
我的整个程序在main方法中具有完全相同的值。然而,它不仅要求在船2上支付新的首付款,而且还要求船1,给我信息......
Problem:
Invalid, must be > 3000.0.
Please enter the down payment:
我现在已经查看了我的节目几个小时,并且无法解决我的生活问题。如果你们中的任何人能够对这个问题给出任何见解,那将非常感激。
如果有人想看看它,我可以发布整个代码,但它很长而且令人困惑。
答案 0 :(得分:1)
首先,我不会为您找到代码中的错误。调试自己的代码是你应该学习的东西。
回答你的问题:
什么会导致while循环执行,即使是假的?
什么都没有!
循环条件在某种意义上是 1 true。您可能不这么认为,并且/您可能不明白为什么,但它&gt;&gt;是&lt;&lt;真。
那你该怎么办?在这样的情况下,应用程序的行为方式对你来说毫无意义,首先要做的是使用调试器。
while
声明中设置断点。downPaymentPassed
,boatPrice
和boatIsNew
。我预测你会发现一个或多个变量没有你认为它应该具有的价值......那时候。
应该考虑的一个可能性是downPaymentPassed < boatPrice * .30
由于舍入误差而给出了意想不到的答案。浮点算术和浮点文字(如.30
)并不准确。 (我不是说这是问题,但它可能是......取决于变量包含的值。)
1 - 我在这里有点谨慎。理论上可能的是1)你有硬件故障,2)有编译器错误,3)你的计算机一直被宇宙射线摧毁。另一个可能的解释(在多线程应用程序中)是明显无法解释的结果是由于并发错误;例如共享变量的非同步访问。但是,你应该在这里忽略这些可能性。
答案 1 :(得分:-1)
我测试了你的代码,它运行正常。
编辑代码时会发生此类错误,但在编译或编辑和编译之前忘记保存,但在新名称或位置下保存并运行旧代码。
Create first instance of Boat class
Create second instance of Boat class
Invalid, must be > 3000.0.
Please enter the down payment:
4000
The first boat information:
Boat Price: 29900.0
Down Payment: 9900.0
Boat Is New: false
The second boat information:
Boat Price: 10000.0
Down Payment: 4000.0
Boat Is New: false