2013-09-30 29 views
-1

这是我的代码。所有的帮助将不胜感激。 PS我不是在做一个骗局或类似的东西,最上面的部分应该是个玩笑。为什么我的扫描仪不出现在我的for循环中?

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"); 
     }     
    } 
    } 

}

+3

你在问什么? –

+1

检查你的循环,它不增加任何东西。 –

+0

你忘了问一个问题。 – Prateek

回答

2

您需要反转moneyleft终止条件在for循环是

for(moneyleft = 100.00; moneyleft > 0.00;) 

目前,它会立即计算到如此循环结束没有开始。

编辑:

更重要的是,你可以删除行

double moneyleft = damountofmoneywon; 

并修复条件是:

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

因为它是目前分配给damountofmoneywon正在失去100.0的分配

+0

在for循环的声明中仍然需要递增/递减条件,否则发生时会出现无限循环。 –

+0

这覆盖了 moneyleft = moneyleft - ditemonecost –

+0

呵呵我没有意识到,如果你在标题之前声明了一个变量,你可以在for循环的主体中使用它来控制它什么时候终止。 –