为什么我的扫描仪不会出现在我的for-loop中?

时间:2013-09-30 22:06:53

标签: java java.util.scanner

这是我的代码。所有帮助将不胜感激。 PS我没有做出任何骗局或类似的事情,最重要的部分应该是个笑话。

import java.io. ; import java.util。;

public class ShoppingSpree//Nick
{
   public static void main(String args[])
   {
     int maxitems = 3;
     double damountofmoneywon = 100.00;
     double moneyleft = damountofmoneywon;
     for(moneyleft = 100.00; moneyleft == 0.00;)
     {
     System.out.println("You have won $100 for being the 1,000,000 visitor to this       site.");
     System.out.println("You may buy up to 3 items costing no more than $100.");
     System.out.println("Enter the cost of your item: ");
     Scanner itemonecost = new Scanner(System.in);
     double ditemonecost = itemonecost.nextDouble();
     if(ditemonecost <= moneyleft)
       {
       System.out.println("You have enough money for this item.");
       moneyleft = moneyleft - ditemonecost;
       }
     else if(moneyleft == 0)
       {
         System.out.println("You have no more money");
         break;
       }
      else
       {
         System.out.println("You don't have enough money, try again");
      }                 
     }
   }

}

1 个答案:

答案 0 :(得分:2)

您需要将for循环中的moneyleft终止条件反转为

for(moneyleft = 100.00; moneyleft > 0.00;)

目前它正在立即评估 false ,以便循环终止而不会开始。

编辑:

更好的是,您可以删除该行

double moneyleft = damountofmoneywon;

并将 for 条件设置为:

for(double moneyleft = damountofmoneywon; moneyleft > 0.00;)

因为目前对damountofmoneywon的任务在100.0的任务中丢失