array size [10] need help!! any experts
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
Code java:
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
Re: array size [10] need help!! any experts
I am not really sure what your question is. What exactly are you asking? Please read the link in my signature on asking questions the smart way and post an SSCCE with a specific question, and we'll go from there.
Re: array size [10] need help!! any experts
When you have code that is repeated again and again and again with only a small consistent change from one to the next you should think about how to reduce the code to one instance by using a variable instead of hardcoding new numbers.
Look at the relationship between the value of i and the array index for these statements and the ones following it:
Code :
if (i==1){
total = getdata[0]; // total for intake breakfast + lunch + dinner
Is the index always one less than the value of i?
Re: array size [10] need help!! any experts
Quote:
Originally Posted by
KevinWorkman
I am not really sure what your question is. What exactly are you asking? Please read the link in my signature on asking questions the smart way and post an
SSCCE with a specific question, and we'll go from there.
sorry , is hard to type here if you have seen the program output. i am using methods for my java, as i am require to do an array size[10]
for 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
Re: array size [10] need help!! any experts
Quote:
Originally Posted by
Norm
When you have code that is repeated again and again and again with only a small consistent change from one to the next you should think about how to reduce the code to one instance by using a variable instead of hardcoding new numbers.
Look at the relationship between the value of i and the array index for these statements and the ones following it:
Code :
if (i==1){
total = getdata[0]; // total for intake breakfast + lunch + dinner
Is the index always one less than the value of i?
yeah i am figuring but i just cant do array really need help lol. for the question you ask. my index 1 represent my 1st selection choice in calories intake.
Re: array size [10] need help!! any experts
Have you worked on the change I suggested to get rid of the 4 if statements and reduce the code to a few statements that uses an index that is computed from the value of i to access the contents of the array?
try something like this:
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
Re: array size [10] need help!! any experts
Quote:
Originally Posted by
Norm
Have you worked on the change I suggested to get rid of the 4 if statements and reduce the code to a few statements that uses an index that is computed from the value of i to access the contents of the array?
try something like this:
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
yea, i am trying but i cant figure how to use method with arrays thats the problem... any ideas??? if i am not wrong i suppose to use array on my intergers values for breakfast,lunch,dinner. so it can store and loop back.
Re: array size [10] need help!! any experts
Quote:
how to use method with arrays
Can you explain what data you are having problems using arrays with.
You are using an array here:
total = getdata[i-1];