Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Stuck at the start

nielsvdbnielsvdb AntwerpMember Posts: 1

Hello everyone, I'm new to java and i'm kinda stuck at a point of an exercise.

Just a small question about the following code. I'm trying to get the user to give in 3 values. a starting value(begin variable), a number they'd like to increase with(verhoging variable) and the amount of times they'd like to do increase the starting number(aantal).
Aantalvermeerderingen is a variable i use to get to the "aantal' number and uitkomst is the number that should appear on the screen.

So if i'd give in 5, 5, 5 as input i should get 5,10,15,20,25, instead I get 510,10,10,10,10. Any idea what I did wrong?

import java.util.Scanner;

/**
* Created by Niels on 25/09/2015.
*/
public class reeksen {
public static void main(String[] args) {
int begin, aantal, verhoging, aantalvermeerderingen, uitkomst;
Scanner keyboard = new Scanner(System.in);
System.out.print("Geef een startwaarde: ");
begin = keyboard.nextInt();
System.out.print("Geef de waarde waarmee je wil verhogen: ");
verhoging = keyboard.nextInt();
System.out.print("Hoeveel getallen wil je afdrukken? ");
aantal = keyboard.nextInt();
aantalvermeerderingen = 1;
uitkomst = 0;
System.out.print(begin);
while(true) {
if (aantalvermeerderingen<aantal) {
uitkomst = begin + verhoging;
aantalvermeerderingen = aantalvermeerderingen + 1;
System.out.println(uitkomst);
}
if(aantalvermeerderingen==aantal) {
uitkomst = begin + verhoging;
System.out.println(uitkomst);
return;
}
}
}
}

Comments

  • pseudocoderpseudocoder Member Posts: 701 ✭✭✭

    if (aantalvermeerderingen<aantal) { uitkomst = begin + verhoging; ... }

    this part will always assign uitkomst the initial values contained within begin and verhoging, so given the user input of 5 5 5, you're getting uitkomst = 5 + 5; with each iteration of the loop.

    I don't know the syntax of java, but in general, try something like

    count = 0;
    
    // wait until the loop to output begin
    
    while(count < aantal)
    {
        output begin
        begin += verhoging
        increment count
    }
    

    HTH

Sign In or Register to comment.