2013-09-30 28 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循環的主體中使用它來控制它什麼時候終止。 –