Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Storing a temporary value of a constantly changing variable

I would like to know, is it possible to store a temporary value of a variable before it switches to another one.

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int input = Integer.parseInt(scanner.nextLine());
    String input1 = String.valueOf(input);
    int counter = 1;

    for (int i = input1.length() - 1; i >= 0 ; i--) {
        char charValueOfI = input1.charAt(i);
        String StringValueofI = String.valueOf(charValueOfI);
        int intValueofI = Integer.parseInt(StringValueofI);
        counter = 1;

        for (int j = intValueofI; j >0 ; j--) {
            counter = counter * j; // if input = 145, first value saved under counter will be 120, can that value of 120 be saved somewhere, before counter changes back to 1 under the first for loop

        }
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to have a sum variable before the for cycles, and in the first for, after the second, have the sum add the counter.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading