bsky.prob
bsky.prob

Reputation: 1

Learning basic loops

I have next steps in my program:

  1. I need to enter how many times I would like to print a number.
  2. After that I have to input a value to start with.
  3. Finally the value has to be repeated as many times as the input of the print value.

// This is as far as I got..

import java.util.Scanner;
public class Series{
    public static void main(String[] args) {
        Scanner keyb = new Scanner(System.in);

        System.out.println("How many numbers do you want to print? ");
        int number = keyb.nextInt();

        System.out.println("What value would you like to start with? ");
        int value1 = keyb.nextInt();

        System.out.println("Typ increased value: ");
        int increase1 = keyb.nextInt();

        System.out.println(value1 + increase1);
    }
}

The result I get is this:

How many numbers do you want to print? 
5
What value would you like to start with? 
50
Typ increased value: 
500
550

My question is how do I get to print 5 numbers after each other that adds +500 for each printed value?

Upvotes: 0

Views: 91

Answers (3)

StackFlowed
StackFlowed

Reputation: 6816

There are 4 basic types of loops in java
1. while()
2. for()
3. do {... }while()
4. for each loop

First while loop :

while(Boolean_expression)
{
   //Statements
}

Second for loop :

for(initialization; Boolean_expression; update)
{
   //Statements
}

Third do while :

do
{
   //Statements
}while(Boolean_expression);

Fourth for each loop or advanced for loop:

for(declaration : expression)
{
   //Statements
}

As per you question you can do is:

for(int i = number; i < number + value1;)
{
    System.out.println(i);
    i += increase1;
}

Upvotes: 1

christopher
christopher

Reputation: 27336

The key lies in Java loops. Loops allow the same bit of code to be executed a desired amount of times, or until a certain condition is fulfilled. The syntax for a for loop is as follows:

for(declaration; loop condition; step)

so for example

for(int x = 0; x < 5; x++) {
     System.out.println("The number is " + x);
}

This will print:

The number is 0
The number is 1
...
The number is 4

Now you can see how this might apply to your code.

Some Pseudocode to get you started

Let's take the code you've got:

System.out.println(value1 + increase1);

So, you know this outputs the increased value, but you need to increase it each time. That means you need to keep track of the value:

runningTotal := value1;

for the amount of times to loop {
    runningTotal = runningTotal + increase1
    output runningTotal
}

Now turn this into Java and you've got your solution!

Extra Reading

This link goes through all of the loops that are offered in Java

Upvotes: 2

brso05
brso05

Reputation: 13222

public static void main(String[] args) {
        Scanner keyb = new Scanner(System.in);

        System.out.println("How many numbers do you want to print? ");
        int number = keyb.nextInt();

        System.out.println("What value would you like to start with? ");
        int value1 = keyb.nextInt();

        System.out.println("Typ increased value: ");
        int increase1 = keyb.nextInt();
        for(int i = 0; i < number; i++)
        {
             System.out.println(value1);
             value1 += increase1;
        }
    }

Try something like this.

Upvotes: 0

Related Questions