-1
私のwhileループは、負の数を入力するまでうまく動作しますが、何が間違っていますか?負の数を入力すると、ループが停止し、コードの実行が終了します。ループ全体を再起動して、budgetRemainingが0になるまで入力を続けることができます。私は助けてくれてありがとう!負の数を挿入した場合を除いて、whileループが再起動しているようですか?
Scanner enterPrice = new Scanner(System.in);
double budgetRemaining = 100;
double itemPrice;
System.out.println("Please enter price of item:");
itemPrice = enterPrice.nextDouble();
if (itemPrice < budgetRemaining) {
budgetRemaining -= itemPrice;
if (itemPrice < 0) {
budgetRemaining += itemPrice;
System.out.println("Sorry, you have entered an invalid amount.");
} while (budgetRemaining > 0 && itemPrice >= 0) {
System.out.println("You have a remaining budget of $" + budgetRemaining + ". Please enter price of item:");
System.out.println(itemPrice = enterPrice.nextDouble());
if (itemPrice < budgetRemaining) {
budgetRemaining -= itemPrice;
if (itemPrice < 0) {
budgetRemaining += itemPrice;
System.out.println("Sorry, you have entered an invalid amount. ");
}
} else if (itemPrice > budgetRemaining) {
System.out.println("Sorry, your item exceeds your budget.");
}
if (itemPrice == budgetRemaining) {
budgetRemaining = 0;
System.out.println("You have reached your maximum budget. Thank you for shopping with us!");
}
}
}
であるあなたがもしあまりにも多くの書類を持っている場合、それは停止したくない場合は、あなたのwhileループから
&& itemPrice >= 0
基準を削除します。論理を考え、条件チェックの数を減らす方法を見てみましょう。 whileループの外側にwhileループを、次にwhileループの中にいくつかの条件を設定して、動作を判断する必要があります。あなたはwhileループで条件を固定することによってそれを動作させることができますが、それは最善の解決策ではありません。 – clinomaniacデバッガの使い方を知っていますか? – user1803551
私はそれが何であるかに精通していますが、私はそれを使う方法を正確には分かりません。私はそれを調べると思います –