-1

Here is my code. All help will be greatly appreciated. PS I'm not making a scam or anything like that, the top part is supposed to be a joke.

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

}

4

1 回答 1

2

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

for(moneyleft = 100.00; moneyleft > 0.00;)

目前它立即评估为,因此循环终止而没有开始。

编辑:

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

double moneyleft = damountofmoneywon;

并修复你条件为:

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

因为目前对 damountofmoneywon 的分配在分配给 100.0 时丢失了

于 2013-09-30T22:17:25.143 回答