(JAVA) How do I access the values calculated in a for loop after the for loop is finished iterating?
My task is to calculate subtotals using values from the user dependent on how many items they are buying. I used a nested for loop with if loops to calculate the subtotals based on what the price is (user input) and quantity of items (user input). I was able to get the loop to run how I need to; however, after the for loop I am tasked to add all of the subtotals that the for loop generated together for a final subtotal. I am unsure of how to store the values from the for loop to access and add them all together at the end. Thank you very much for any help.
Answer:
You can use an array to store the values/outputs of the for loop, to be used later in the program as shown below:
public class HelloWorld{
public static void main(String
[]args){
//create an array to store outputs of the for
loop
int[] subtotals = new int[10];
int p = 0; //just for illustration
int n = 1; //just for illustration
int subtotal = 0;
int total = 0;
System.out.println("Subtotals");
for(int i=0;i<10;i++){
subtotal = n+p;
System.out.println(subtotal);
//putting the outputs/values in an array
subtotals[i]= subtotal;
p++;
n++;
}
/*The computation values of the first for loop was stored in
array
, which can be access later.
*/
for(int j=0;j<10;j++){
total += subtotals[j];
}
//print the total of subtotals
System.out.println("Total =
"+total);
}
}
Sample run:

---------------------------------------------------------------------------------------------------------------------------------
(JAVA) How do I access the values calculated in a for loop after the for loop...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...
Java. I am having a hard time figuring out how to take user input and then write a method that returns the answer. All the examples I find have numbers hard coded. Can you show me how this is done. For example, maybe add two numbers. Thank you
I should use the array and loop to create a java program
according to the instruction, but I have no idea how to do
it.
Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...
how can I Use fscanf() inside a loop to read each line in C language? also how can I take the values from the file and add them together for example if the file contents are 2 4 6 I want to read the first line and add 2 then the second one and add 4 and then the third and 6 and finally return the total 12 and stop reading. I do not want to store the values in...
Having trouble with the do while/while loop and the switch
statement. I got some of the switch statement but cant get the
program to repeat itself like it should.What i have so far for my
code is below. Any help is appreciated... i am not sure what I am
doing wrong or what i am missing. I am completely lost on the while
loop and where and how to use it in this scenario.
import java.util.Scanner;
public class sampleforchegg {...
package _solution;
/**
This program demonstrates how numeric types and operators behave in Java
Do Task #1 before adding Task#2 where indicated.
*/
public class NumericTypesOriginal {
public static void main (String [] args) {
//TASK #2 Create a Scanner object here
//identifier declarations
final int NUMBER = 2 ; // number of scores
int score1 = 100; // first test score
int score2 = 95; // second test score
final int BOILING_IN_F = 212; // boiling temperature
double fToC;...
In this assignment, you will write a program in C++ which uses files and nested loops to create a file from the quiz grades entered by the user, then reads the grades from the file and calculates each student’s average grade and the average quiz grade for the class. Each student takes 6 quizzes (unknown number of students). Use a nested loop to write each student’s quiz grades to a file. Then read the data from the file in order...
Create a loop that will populate a listbox with random values 14 pts The random numbers should range for ranging from 0 to 10 (10 must be a potential number that will be added). Keep adding values to the listbox until the sum of the values in the listbox equals or exceeds 100. After adding the values, tell me how many items are in the listbox, and the sum of the numbers, displaying these values in labels. Create a...
Programming assignment for Java:
Do not add any other instance variables to any class, but you
can create local variables in a method to accomplish tasks. Do not
create any methods other than the ones listed below.
Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...