Jump to content

Beginner Java programming question....

Scanner console = new Scanner(System.in);

int sum = 0;

for (int i = 1; i <= 100; i++) {

System.out.print("Type a number: ");

sum = sum + console.nextInt();

}

System.out.println("The sum is " + sum);

 

The following is some code that my instructor has provided for me. However there is one thing I am just not fully understanding. The value from the loop is i, so how does that get passed onto any other part of the program? It looks to me as though the Int sum, would be completely separate from the for loop.

Link to comment
https://linustechtips.com/topic/896026-beginner-java-programming-question/
Share on other sites

Link to post
Share on other sites

11 minutes ago, Doomerson said:

Scanner console = new Scanner(System.in);

int sum = 0;

for (int i = 1; i <= 100; i++) {

System.out.print("Type a number: ");

sum = sum + console.nextInt();

}

System.out.println("The sum is " + sum);

 

The following is some code that my instructor has provided for me. However there is one thing I am just not fully understanding. The value from the loop is i, so how does that get passed onto any other part of the program? It looks to me as though the Int sum, would be completely separate from the for loop.

The iterator doesn't necessarily have to be interacted with or accessed in the body of a for loop. For loops can be used as simple tools to execute a block of code multiple times, where the iterator (in your case i) isn't needed.

Spoiler

My main desktop, "Rufus":

Spoiler

PC Specs:

CPU: AMD Ryzen 5 1600

CPU Cooler: Cooler Master MasterLiquid Lite 120

RAM: 2x8gb Corsair Vengence DDR4 Red LED @ 3066mt/s

Motherboard: MSI B350 Gaming Pro Carbon

GPU: XFX RX 580 GTR XXX White 

Storage: Mushkin ECO3 256GB SATA3 SSD + Some hitachi thing

PSU: Seasonic Focus Plus Gold 650W

Case: Corsair Crystal 460X

OS: Windows 10 x64 Pro Version 1607

Retro machine:

Spoiler

PC Specs:

CPU: Intel Core 2 Quad Q9550

CPU Cooler: Stock heatsink

RAM: GSkill 4gb DDR2 1066mt/s

Motherboard: Asus P5n-e SLI

GPU: 8800 GTS 640mb, I swap between that and my 8800 GTS 512mb

Storage: Seagate 320gb right from 2006

PSU: Ultra 600W 

Case: Deepcool Tesseract SW

OS: Windows XP SP3 32-bit, Linux Mint 18.2 Cinnamon 64-bit, Manjaro Deepin x64 (sorta)

Mac Pro Early 2008: Dual Xeon X5482s w/ 32GB RAM & HD 5770 running macOS High Sierra

More PC's

 

Link to post
Share on other sites

8 minutes ago, Doomerson said:

int sum = 0;

is declared and initialized outside and before the loop.
 

 

10 minutes ago, Doomerson said:

sum = sum + console.nextInt();

The first time through the loop sum = 0 + console.nextInt();

 

The loop executes for 100 times.

sum is your running total and finally outputs after the loop.

 

1 minute ago, panther420 said:

System.out.println("The sum is " + sum);

This is just an exercise.  Exercises based on real life can overly complex when you are trying to demonstrate programming.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×