(Java) Sum of While

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



(Java) Sum of While



So I wanted to sum all of the numbers i get from system.out.print except 0 (the last one).


public static void main(String args)

Scanner lukija = new Scanner(System.in);
int km = 0, luku = 0;
while (km < 10)
System.out.print("Anna ajetut kilometrit (0 lopettaa): ");
luku = lukija.nextInt();
km++;
if (luku == 0)
break;



while (luku != 0)
km = km + luku;
System.out.println("Anna ajetut kilometrit (0 lopettaa): ");
luku = lukija.nextInt();

System.out.println("Yhteensä " + luku + " kilometriä");



Heres, what the Scanner should like:


Anna ajetut kilometrit (0 lopettaa): 12
Anna ajetut kilometrit (0 lopettaa): 13
Anna ajetut kilometrit (0 lopettaa): 10
Anna ajetut kilometrit (0 lopettaa): 5
Anna ajetut kilometrit (0 lopettaa): 0
**Yhteensä 40 kilometriä**



Mine :


Anna ajetut kilometrit (0 lopettaa): 12
Anna ajetut kilometrit (0 lopettaa): 13
Anna ajetut kilometrit (0 lopettaa): 10
Anna ajetut kilometrit (0 lopettaa): 5
Anna ajetut kilometrit (0 lopettaa): 0
**Yhteensä 0 kilometriä**





Welcome to Stack Overflow. Please take a few minutes to read ericlippert.com/2014/03/05/how-to-debug-small-programs for tips on how to debug your code.
– Code-Apprentice
48 secs ago





Print km instead of luku.
– Elliott Frisch
33 secs ago


km


luku









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS