Hi Guys,
my choice 1. i key in the calories for the day but my program can calculate up till 4 days when i exit. so i am asking how to use array to calculate up to 10 days when i press 3 exit.
an example of my output // display
1. Enter Food and Intake per day
2. Display food intake status
3. Exit
so let say i key in 8 days in 1. Enter Food and Intake per day , my exit 3. should show 8 days of the calculated intake by using array
heres the code thanks alot
import java.util.*; // for function scanner input public class Diet { static int i = 0; static Scanner input = new Scanner (System.in).useDelimiter ("\r\n"); //system scann and print static int[] breakfast = new int[10]; // declare as int static int[] lunch = new int[10]; // delcare as int static int[] dinner =new int[10]; // declare as int static double[] getdata = new double [10]; // create an array of integers static char selection = 'x'; // dummy initialization public Diet() { } public static void main(String [] args) throws Exception{ while (selection !='Q'){ //loop //display on run System.out.println("**********Daily Food Intake**********"); System.out.println("1. Enter Food and Intake per day"); System.out.println("2. Display food intake status"); System.out.println("3. Exit"); System.out.println("*************************************"); System.out.print("Please Enter Your Choice: "); selection = (char)System.in.read(); // dummy selection to capture input System.in.read(); System.in.read(); if (selection == '1' || selection =='2' || selection == '3'){ // enter food(i) to store calories for the day using int array if (selection == '1') { for (int i = 0; i < 10; i++) breakfast[i]=0; lunch[i]=0; dinner[i]=0; { if (i == 0){ EnterFood(i); i = 1; } else if (i == 1){ EnterFood(i); i = 2; } else if (i == 2){ EnterFood(i); i = 3; } else if (i == 3){ EnterFood(i); i = 4; } } //else{ //System.out.println("You have enter for all 3 day"); // } } if (selection == '2') // choice 2, show calories for the day using selection 2 by totalcalories(i) { totalCalories(i); } if (selection == '3') // array store results and displayed the array data before terminates { System.out.println("Your Total intake for this day is " + getdata[0] + "Calories"); checkCalories(getdata[0]); System.out.println("Your Total intake for this day is " + getdata[1] + "Calories"); checkCalories(getdata[1]); System.out.println("Your Total intake for this day is " + getdata[2] + "Calories"); checkCalories(getdata[2]); System.out.println("Your Total intake for this day is " + getdata[3] + "Calories"); checkCalories(getdata[3]); breakfast[i]=0; lunch[i]=0; dinner[i]=0; selection = 'Q'; //while loop } } else{ System.out.println("Please enter an valid option"); // display an error message } //}//end do //while loop //while (selection !='Q'); }//end while }//end main //call method for Option 1-------------------------------------- public static void EnterFood(int i)throws Exception{ setBreakfast(); setLunch(); setDinner(); getdata[i] = breakfast[i] + lunch[i]+ dinner[i]; //store to array System.out.println("Total calories for the day = " + getdata[i]); } //DAY 1------------ public static void setBreakfast()throws Exception{ System.out.print("1. Enter Breakfast intake <calories> : "); breakfast[i] = input.nextInt(); //capture input } public static void setLunch()throws Exception{ System.out.print("2. Enter Lunch intake <calories> : "); lunch[i] = input.nextInt(); //capture input } public static void setDinner()throws Exception{ System.out.print("3. Enter Dinner intake <calories> : "); dinner[i] = input.nextInt(); //capture input } //call method for Option 2-------------------------------------- public static void totalCalories(int i){ double total = 0.0; if (i==1){ total = getdata[0]; // total for intake breakfast + lunch + dinner System.out.println("Your Total intake for this day is " + total + "Calories"); checkCalories(total); } if (i==2){ total = getdata[1]; // total for intake breakfast + lunch + dinner System.out.println("Your Total intake for this day is " + total + "Calories"); checkCalories(total); } if (i==3){ total = getdata[2]; // total for intake breakfast + lunch + dinner System.out.println("Your Total intake for this day is " + total + "Calories"); checkCalories(total); } if (i==4){ total = getdata[3]; // total for intake breakfast + lunch + dinner System.out.println("Your Total intake for this day is " + total + "Calories"); checkCalories(total); } breakfast[i] = 0; // reset after the diplaying day 1 and day 2 and day 3 lunch[i] = 0; dinner[i] = 0; } public static void checkCalories(double a){ // check the calories intake if a is low, healthy or too much if (a < 2000){ System.out.println("This intake is LOW"); } if (a>=2000 && a <= 2500){ System.out.println("This intake is Healthy"); } if (a > 2500){ System.out.println("This intake is TOO MUCH"); } } }//end class