Home > front end >  Java For Loop that iterates through days of the week for a month?
Java For Loop that iterates through days of the week for a month?

Time:02-15

Working on a project for university to calculate the amount of calories someone burns in a month. First thought was I need a for loop to iterate through all the days of the week, taking in user input and assigning it to the appropriate variable. Loop doesn't work as intended and I believe it's because of

for(int i = 0; i < 7; i )

but I'm not sure what to replace this with. My initial idea is another variable but I wouldn't know what to set it to other than 7 which doesn't change anything. Code Screenshot

 public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    
    int monCalorie;
    int tueCalorie;
    int wedCalorie;
    int thuCalorie;
    int friCalorie;
    int satCalorie;
    int sunCalorie;
    int weekCalorie;
    
    
    System.out.println("Please enter the amount of calories you burned for the week starting on Monday");
    
    for(int i = 0; i < 7; i  ) {
        monCalorie = scnr.nextInt();
        System.out.println("Calories burnt on Monday: "   monCalorie);
        
        tueCalorie = scnr.nextInt();
        System.out.println("Calories burnt on Tuesday: "   tueCalorie);
        
        wedCalorie = scnr.nextInt();
        System.out.println("Calories burnt on Wednesday: "   wedCalorie);
        
        thuCalorie = scnr.nextInt();
        System.out.println("Calories burnt on Thursday: "   thuCalorie);
        
        friCalorie = scnr.nextInt();
        System.out.println("Calories burnt on Friday: "   friCalorie);
        
        satCalorie = scnr.nextInt();
        System.out.println("Calories burnt on Saturday: "   satCalorie);
        
        sunCalorie = scnr.nextInt();
        System.out.println("Calories burnt on Sunday: "   sunCalorie);
        
        weekCalorie = monCalorie   tueCalorie   wedCalorie   thuCalorie   friCalorie   satCalorie   sunCalorie;
    }

CodePudding user response:

If you don't actually need the week counter to display it to the user, then this should solve your problem :

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        int monthCalories = 0;


        System.out.println("Please enter the amount of calories you burned for the week starting on Monday");

        for (int i = 0; i <= 30; i  ) {
            int inputCalories = scnr.nextInt();
            monthCalories  = inputCalories;
            System.out.print("Calories burnt on ");
            switch (i%7) {
                case 0:
                    System.out.println("Monday: "   inputCalories);
                    break;
                case 1:
                    System.out.println("Tuesday: "   inputCalories);
                    break;
                case 2:
                    System.out.println("Wednesday: "   inputCalories);
                case 3:
                    System.out.println("Thursday: "   inputCalories);
                    break;
                case 4:
                    System.out.println("Friday: "   inputCalories);
                    break;
                case 5:
                    System.out.println("Saturday: "   inputCalories);
                    break;
                case 6:
                    System.out.println("Sunday: "   inputCalories);
            }
        }
        System.out.println("You burnt "   monthCalories   " this month, congrats !");
    }

Modulo is a really convenient operator to cycle inside a loop

CodePudding user response:

public static void main(String[] args) 
{
    Scanner scnr = new Scanner(System.in);

    String[] weekDays= { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; 
    List<Integer> calorieOfWeeks = new ArrayList<Integer>(); // keeps weekly data of calories. it can be used for monthly data. if you don't need weekly data you can keep it as an integer.

    int daysInMonth=30; // choose how many day a month includes
    int dayIndex=2;     //choose starting day of month. 0=monday, 1=tuesday...
    int weekCalorie=0;  // keeps calories of current week

    System.out.println("Please enter the amount of calories you burned for the week starting on Monday");

    for(int i=dayIndex; i<daysInMonth dayIndex; i  )
    {
        System.out.print("Calories burnt on "    weekDays[i%7]    ": ");
        weekCalorie =scnr.nextInt();

        if(i%7==6  || i==dayIndex daysInMonth-1) // if it is sunday or month ended, it prints data of the week
        {  
            calorieOfWeeks.add(weekCalorie);
            System.out.println( "Calories burnt on "   calorieOfWeeks.size()    ".week : "   weekCalorie);
            weekCalorie=0;
        }
    }  
}
  •  Tags:  
  • java
  • Related