Counting by 10 in java with a while loop [closed] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Counting by 10 in java with a while loop [closed] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

The goal of this code is to count to 50 by 10’s but I cant seem to figure out how to do it with a while loop. Would I multiply the integerntp by 10?

import java.util.Scanner;

public class CountingWhile {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type in a message, and I'll display it how ever many times you want");
        System.out.print("Message: ");
        String message = keyboard.nextLine();
        System.out.print("Number of times printed: ");
        int ntp = keyboard.nextInt();

        int n = 10;
        while (n <= ntp) {
            System.out.println(n + ". " + message);
            n += 10; //its the same as "n = n + 1" or "n++"
        }
    }
}

Answer

I’m not sure if I understand your question, but this code will print nothing. You tell the program to print some message 5 times but already at the beginning condition in a loop won’t be true, sice 10 <= 5 is false. Why don’t you just multiply number, which you give to program by your interval (in this case 10)?

We are here to answer your question about Counting by 10 in java with a while loop [closed] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji