2014-07-15 117 views
-1

這裏是我的問題:我的for循環應該在循環過程中旋轉,當它達到值7時,它應該從1開始。在java中的循環過程中旋轉for循環過程

和我的newcost值應該始終在增加值。

這裏是我的代碼:

import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 

import org.joda.time.DateTime; 
import org.joda.time.Days; 

public class PayCalculation { 

    int cost,cost1=0; 
    private String fromdate, todate, npersons, nrooms, rooms; 
    long newcost; 
    public String execute() throws Exception{ 

     DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 

     Date date1 = dateFormat.parse(fromdate); 
     //Date date2 = dateFormat.parse(todate); 
     Calendar c = Calendar.getInstance(); 
     c.setTime(date1); 
    // c.setTime(date2); 
     int dayOfWeek1 = c.get(Calendar.DAY_OF_WEEK); 
     // int dayOfWeek2 = c.get(Calendar.DAY_OF_WEEK); 
     Date past = dateFormat.parse(fromdate); 
     Date today = dateFormat.parse(todate); 
     int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); 

     System.out.println(dayOfWeek1 + " day of the week"); 

     System.out.println(days + " no of days"); 
     int i; 
     for(i=0 ; i<=days ; i++){ 


      System.out.println(i); 

      if(i==1 || i==7){ 

       System.out.println("sun or sat"); 
       cost1=1500; 
      // newcost=cost1; 
      } 
      if(i==2 || i==3 || i==4 || i== 5|| i==6){ 


       System.out.println(" other days"); 
       cost=1000; 
     // newcost=cost; 
      } 
      /*if(i==7){ 
       System.out.println("i==7"); 
       i=1; 
      } 
*/   newcost=0+cost+cost1; 

      System.out.println(newcost+" new cost"); 

     } 

     System.out.println(newcost); 
     return "success"; 

    } 
+1

你的問題是什麼? –

+0

那麼,下面的答案有幫助嗎? –

回答

0

我不明白爲什麼i==1i==7應參照星期天和星期六,因爲days是天數,我不要在您的代碼看看它據說1是星期天。但無論如何,假設它是正確的,你應該這樣做:

for(i=1 ; i<=days ; i++){ 
    if(i==1 || (i>=7 && (i%8==0 || i%7==0))){ 
     System.out.println("sun or sat"); 
    }else{ 
     System.out.println(" other days"); 
    } 
} 

我不知道它是否符合你的需要,但因爲我很抱歉地說,你的問題是有點含糊

0

你可以使用模數學來獲得星期幾,如int weekday = (i % 7) + 1(加上當天的一些偏移量)。要在每次迭代中增加newCost,請使用newcost += cost + cost1

但是,這將在每週後增加newCost,2500,因爲您在循環中不重置costcost1。將cost = 0cost1 = 0添加到循環的頂部,或者更好地,將您的循環體改爲newCost += (weekday==1 || weekday==7) ? 1500 : 1000

for (int i = 0; i <= days; i++) { 
    int weekday = ((dayOfWeek1 + i) % 7) + 1; 
    newCost += (weekday == 1 || weekday == 7) ? 1500 : 1000; 
} 
0

你的代碼有一些失誤,反正你可以在另一個櫃檯,保持天(防止無限循環)的數量的軌道放置並在i=7每次出現時重置i,下面的代碼工作: -

int i; 
    int dayscounter=days; 
    for(i=0 ; i<=days && dayscounter>0 ; i++ ,dayscounter--){ 

    System.out.println(dayscounter+":"+i); 
     if(i==2 || i==3 || i==4 || i== 5|| i==6){ 

      System.out.println(" other days"); 
      newcost+=1000; 
     } 
     else if(i==1 || i==7){ 

      System.out.println("sun or sat"); 
      newcost+=1500; 

      if(i==7){ 
       i=0; //reset i counter 


      } 

     } 

     System.out.println(newcost+" new cost"); 

    } 

    System.out.println(newcost); 

還要注意有沒有需要成本和COST1變量,就可以直接增加newcost,因爲反正在每次迭代中僅10001500將被添加到newcost

的一個值
0
//mention this one in global level. 


List<Date> dates = new ArrayList<Date>(); 

//mention all this in local level. 
    String str_date = fromdate; 
     String end_date = todate; 

     System.out.println(fromdate+ "from date"); 
     System.out.println(todate + "to date "); 

     DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
     startDate = (Date) formatter.parse(str_date); 
     endDate = (Date) formatter.parse(end_date); 
     long interval = 24 * 1000 * 60 * 60; 
     long endTime = endDate.getTime(); 
     long curTime = startDate.getTime(); 

    while (curTime <= endTime) { 
      dates.add(new Date(curTime)); 
      curTime += interval; 
     } 

//gettig this mon,tue,.... cost from database from the another class. 

accost = crc.getAc(ac);/*this is the calling function to the other class to get the cost from mon . . . . . sunday .*/ 
     mon = accost[0]; 
     tue = accost[1]; 
     wed = accost[2]; 
     thr = accost[3]; 
     fri = accost[4]; 
     sat = accost[5]; 
     sun = accost[6]; 

    for (int i = 0; i < dates.size(); i++) { 
      Date lDate = (Date) dates.get(i); 
      // System.out.println(i); 
      // System.out.println(dates.size()); 
      System.out.println(lDate); 

      Calendar c = Calendar.getInstance(); 
      c.setTime(lDate); 

      int dayOfWeek1 = c.get(Calendar.DAY_OF_WEEK); 

      System.out.println(dayOfWeek1 + " day of the week"); 

      int sundaycost = sun; 
      int saturdaycost = sat; 
      int moncost = mon; 
      int tuecost = tue; 
      int wedcost = wed; 
      int thrcost = thr; 
      int fricost = fri; 

      if (dayOfWeek1 == 1) { 

       cost = cost + sundaycost; 
       System.out.println("weekend sunday"); 
       System.out.println(cost); 

      } 
      if (dayOfWeek1 == 7) { 

       cost = cost + saturdaycost; 
       System.out.println("weekend saturday"); 
       System.out.println(cost); 
      } 
      if (dayOfWeek1 == 2) { 

       System.out.println("weekday monday"); 
       cost = cost + moncost; 

      } 
      if (dayOfWeek1 == 3) { 

       System.out.println("weekday tuesday"); 
       cost = cost + tuecost; 

      } 
      if (dayOfWeek1 == 4) { 

       System.out.println("weekday wednesday"); 
       cost = cost + wedcost; 

      } 
      if (dayOfWeek1 == 5) { 

       System.out.println("weekday thrusday"); 
       cost = cost + thrcost; 
      } 
      if (dayOfWeek1 == 6) { 
       System.out.println(); 
       System.out.println("weekday friday"); 
       cost = cost + fricost; 
      } 
     }