Question

(JAVA) How do I access the values calculated in a for loop after the for loop...

(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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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:

---------------------------------------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
(JAVA) How do I access the values calculated in a for loop after the for loop...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help on creating a while loop for my Java assignment. I am tasked to...

    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...

    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...

    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,...

    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,...

  • Having trouble with the do while/while loop and the switch statement. I got some of the...

    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...

    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...

    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...

  • Programming assignment for Java: Do not add any other instance variables to any class, but you...

    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:...

  • CIS 1111 Programming Files

    Description:In this assignment, you will write a program in C++ that uses files and nested loops to create a file for real estate agents home sales and then read the sales from the file and calculates the average sale for each agent. Each agent sold 4 homes.  Use a nested loop to write each agent’s sales to a file. Then read the data from the file in order to display the agent’s average sale and the average for all sales....

  • Java I: Create a Java program that will accept the price of an item and the...

    Java I: Create a Java program that will accept the price of an item and the quantity being purchased of that item. After accepting the input, calculate the amount of the purchase (based on the price and quantity), calculate the sales tax on the purchase, then output the product price, quantity, subtotal, sales tax, and the total sale based on the output format shown below. Structure your file name and class name on the following pattern: The first three letters...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT