更新for循环中的变量

时间:2014-06-16 22:27:14

标签: java for-loop

我的计划目标是计算一段时间后我会有多少家商店。我提供了遵循它的程序的等式。每个商店的成本是30000美元。我开始有3家商店和他们的收入10000美元/店,所以我从我的商店获得的总金额是30000美元/月。现在,一个月后,我将能够开设新店,我的店铺将增加到4,依此类推。 7个月后,我的商店将是21,总金额必须是210000美元。你会如何修改代码来做到这一点。如果你想确保你可以用纸来计算它。我的代码中的问题是,一旦总金额达到60000或更多,程序减去30000并将计数器和商店增加1,而它必须增加计数器一次和商店两次。因为60000 $是2家商店的成本等等。

public class investmentCalculation 
    /** the class describe the plan of investment. Each shop costs 30000  */
        {
         private int income ;
         private int shop ;
         private int firstMoney ;
         private int totalMoney1;
         private int totalMoney;
        //instructor  
        public investmentCalculation( )
        {
          income =10000;
           shop = 3;
            /** here is the cost of shop establishment */
           firstMoney = 30000; 
           totalMoney1 = 0;
        }
         // here the method to achieve the goal
        public int SetFor()
        { for(int i= 0; i < 7; i++)
        {   
         totalMoney1 = totalMoney1 + totalMoney - firstMoney;
/**here is    0      =  0          + 60000      - 30000; 
   I got the total money from this equation
   totalMoney = shop * income;
              = 6    * 10000;


now the shop should be 8. However, after compiling the program the output is 7.


         shop++;
         totalMoney = shop * income; /** once the TotalMoney = 30000(FirstMoney) should open new shop and so on. Once the iteration reach i=7 the TotalMoney must be 210000 */  

        shop++;
         totalMoney = shop * income;

         }
             System.out.println("the total money is: "+totalMoney);
             return shop;}


            }


            /** this class to test the investmentCalculation*/
            public class investmentCalculationTester 
           {

            public static void main(String [] args)
           {
            investmentCalculation TT = new investmentCalculation();
            int A =  TT.SetFor();
            System.out.println("the shops are:   "+A);

            }}

1 个答案:

答案 0 :(得分:0)

试试这个。我想你应该重新计算总钱。因为店铺数是21,所以是正确的。请重新计算总金额;它永远不会是210000。

public class investmentCalculation {
private int income;
private int shop;
private int firstMoney;
private int totalMoney1;
private int shopsAdded;

public investmentCalculation() {
    income = 10000;
    shop = 3;
    firstMoney = 30000;
    totalMoney1 = 0;
    shopsAdded = 0;
}

// here the method to achieve the goal
public int SetFor() {
    for (int i = 0; i < 7; i++) {
        totalMoney1 += income * shop;
        shopsAdded = totalMoney1 / firstMoney;  //Calculate how many shops can be added with the money you have.
        shop += shopsAdded;                     //Increment the shops by the newly added shops.
        totalMoney1 -= firstMoney * shopsAdded; //Decrement the cost required to form the new shops.
    }
    System.out.println("the total money is: " + totalMoney1);
    return shop;
}

/** this class to test the investmentCalculation */
public static void main(String[] args) {
    investmentCalculation TT = new investmentCalculation();
    int A = TT.SetFor();
    System.out.println("the shops are:   " + A);
}

}`