This is one part of the whole assignment. I'm getting stuck so I'm doing them piece by piece Prompt for 7 days of calories consumed by day, starting with Sunday and going through Saturday and put the calories into an array. Print the array along with a nicely formatted header (printf is a requirement for this part of the program).
Welcome To Questioncove is there anymore to this ?
yes there is
Generally speaking, on average it takes you 3500 calories each day to maintain your weight, while 3500 extra calories generally equals 1 pound of weight gain (So, 7000 calories in one day would mean that you gained 1 pound that day). Prompt for 7 days of calories consumed by day, starting with Sunday and going through Saturday and put the calories into an array. Print the array along with a nicely formatted header (printf is a requirement for this part of the program). Write a method to calculate the total amount of calories consumed for the week and print the information in a nice readable format. Write a method to calculate the average number of calories consumed in the week to the nearest tenth and print the information in a nice format. Write a method to find if you gained or lost weight this week. Subtract your ideal calories to maintain your weight (7 * 3500) from the calories you consumed for the week. Did you gain or lose weight this week and by how many pounds to the nearest tenth of a pound? Print the information in a nice format. Use the following test input to ensure your program is running properly: Sunday - 4700 calories Monday - 2500 calories Tuesday - 2000 calories Wednesday - 1800 calories Thursday - 3000 calories Friday - 1500 calories Saturday - 3500 calories
@smokeybrown
The directions and expectations for this program are very well-defined, so it should be straightforward for us to design the logic in order to make it work. As the question says, we can use an array in order to store the calorie data used for the calculations. Since we know that we'll be getting a calorie count for each of 7 days, we can start by defining an array of size 7. Our next step would probably be to fill this array with our data. The question states that the program will prompt the user to enter this data for each of 7 days, so we'll need to use a function that takes input from the user. Make sure that this input is of the proper type; for whole-number calorie counts, we'll probably go with positive integers. You may consider code in order to check that the user input is valid before the program continues. Once the input is received, you'll want to store the information in the array. After prompting the user for the Sunday calorie count, for instance, you would put that number into the first index of the array. Monday's calorie number would go into the next index, and so on until Saturday. You'll be asking for 7 day's worth of input in all. You can consider writing a loop of some sort or simply hard-code each input prompt separately. After the array is filled, the first task of the program is to print the contents of the array along with a header. You can use the formatting available through the printf() function to accomplish this, although the details of your formatting are up to you. You will likely combine your own strings and the contents of the array in order to accomplish this. The question then tasks us with writing several methods to perform various calculations and present certain output based on the contents of the array we have. The first method adds the total calorie count of the entire week and prints this information. To sum all of the numbers inside the array, you may consider using a loop that starts at the beginning of the array and iterates through each index in order to add the numbers to a variable that stores the sum. You can then use printf() to display the result The next method calculates the average daily calorie intake for the week. The average would simply be the sum as calculated in the previous method divided by the total number of days The last method compares the "ideal calorie count" (7*3500) to the actual calorie count, which is the sum found in the first method. You may take the difference to find out if weight was gained or lost. You can divide the difference by 3500 to find out how many pounds were gained or lost. This is simply an outline of one way you can accomplish the goals of the program. There are many ways to code it in a working way, but this is the basic logic. Please let me know if you have any questions or suggestions. I will try to be online later if you need any more specific help as well :)
Thanks for the clarification.
I apologize if this is wrong, but I suck at coding
This is what I've done so far
I see the code you shared, and I'll post it here in text form for reference: import java.util.Scanner; public class Exercise32 { public static void main( String args[] ) { //local variables double average = 0.0; // create Scanner to obtain input from command window Scanner input = new Scanner(System.in); int number1; // first number to add int number2; // second number to add int number3; // third number to add int number4; // fourth number to add int number5; // fifth number to add int total = 24500; //calories System.out.print( "Input Monday Calories: " ); // prompt number1 = input.nextInt(); // read first number from user System.out.print( "Input Tuesday Calories: " ); // prompt number2 = input.nextInt(); // read second number from user System.out.print( "Input Wednesday Calories: " ); // prompt number3 = input.nextInt(); // read third number from user System.out.print( "Input Thursday Calories: " ); // prompt number4 = input.nextInt(); // read fourth number from user System.out.print( "Input Friday Calories: " ); // prompt number5 = input.nextInt(); // read fifth number from user if ( number1 == number2 ) System.out.printf( "%d == %d\n", number1, number2 ); if ( number1 != number2 ) System.out.printf( "%d != %d\n", number1, number2 ); if ( number1 < number2 ) System.out.printf( "%d < %d\n", number1, number2 ); if ( number1 > number2 ) System.out.printf( "%d > %d\n", number1, number2 ); if ( number1 <= number2 ) System.out.printf( "%d <= %d\n", number1, number2 ); if ( number1 >= number2 ) System.out.printf( "%d >= %d\n", number1, number2 ); } }
I am confused about a few things with this code. It seems like you prompt the user for 5 inputs, Monday through Friday, instead of the 7 daily calorie counts mentioned in the question. You also hard-coded the value for "total", but calculating this value as a sum of the inputs would make more sense for what the program is trying to accomplish I'm also not sure what the comparison between number1 and number2 is meant to accomplish, since this does not seem to relate to the question A few stylistic suggestions: you can declare the input variables on the same line that you prompt the user for input, for cleaner looking code. You can also include a prompt message inside the input function. For example, you can write int number1 = input.nextInt(); rather than declaring the variables on separate lines. The result will be the same, it's more a matter of preference In fact, since the numbers in the variables will be put into an array, you do not need to declare that many separate variables. Instead, one option would be to declare a single variable and to load the value into the array immediately after receiving the input. Then, the variable can be used to take the next input. For example: int[] calArr = new int[7]; System.out.print( "Input Monday Calories: " ); int nextCal = input.nextInt(); calArr[0] = nextCal; System.out.print( "Input Tuesday Calories: " ); nextCal = input.nextInt(); calArr[1] = nextCal; ... And so on In fact, it would also be possible to load the input values directly into the array, like so: System.out.print( "Input Monday Calories: " ); calArr[0] = input.nextInt(); System.out.print( "Input Tuesday Calories: " ); calArr[1] = input.nextInt(); ... Assuming, of course, that we start with Monday. If we start with Sunday, you'd want to edit the prompts appropriately. Either of these methods has the benefit of saving some memory since fewer variables are used, but in this case they do not make a big difference. Simply suggestions to consider. It would also be possible to use a for-loop to handle the input prompts. Instead of copying very similar code each time you want to prompt the user for input, a loop would save space in the code and improve readability. However, the process I have in mind for this involves an additional string array to hold the day names (Monday; Tuesday; Wednesday, etc). In any case, this is not necessary. Your approach is perfectly workable. I just wanted to give some food for thought In general, your code looks like it can be run, but it does not quite accomplish everything that the question asks for. I encourage you to continue with confidence. I will try to continue looking over your work in case you need any guidance :)
have been working on this all nihgt.
Join our real-time social learning platform and learn together with your friends!