Hi, I am kind of stuck on how to display numbers between an inputted number and a specifed number, for example 10 and 5, how would I go about doing this?

can anyone help me please?

Recommended Answers

All 7 Replies

Try a for loop, start would be one of the numbers and the end the other. Check to see which one is greater in case you need to display them in descending order, by reducing the index instead of increasing.

import javax.swing.JOptionPane;

public class Numbers
{
	public static void main(String[] args)
	{
		String input = JOptionPane.showInputDialog("Enter a whole number greater than 10");

		int rows = Integer.parseInt(input);

		for (int i = rows; i > 5; i++)

		{
				System.out.println (rows);
		}
	}

}

im just getting an endless output of the input number

im just getting an endless output of the input number

Because you are increasing the index in the loop. If you want this:
11,10,9,8,7,6 then the numbers will be decreasing so you need to decrease the index at the loop.

import javax.swing.JOptionPane;

public class Numbers
{
	public static void main(String[] args)
	{
		String input = JOptionPane.showInputDialog("Enter a whole number greater than 10");

		int rows = Integer.parseInt(input);

		for (int i = 5; i > rows; i++)

		{
				System.out.println (rows);
		}
	}

}

Ive changed the code but I now recieve nothing

That is not what I told you to change. Read carefully your initial for-loop. Then read my comments and then read your notes or a book about what a for-loop does and what its arguments do.

Hi,
Try this code if you haven't solved the problem yet.
Hope it helps you and others having the same problem.

Please correct me if I'm wrong.

Cheers,

import javax.swing.JOptionPane;
public class Numbers
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog("Enter a whole number greater than 10");
int rows = Integer.parseInt(input);
for (int i = 5; i <= rows; i++)
{
System.out.println (i);
}

}

}

Hi,
Try this code if you haven't solved the problem yet.
Hope it helps you and others having the same problem.

This is a good example. The only difference is that the OP wants the numbers to be printed descending:
10, 9, 8, 7, 6

But you don't have to give him the entire solution. By using your example and my suggestions he should be able to figure this out on his own.


We give them enough information to correct their mistakes and reach to the solution on their own.

There is nothing more we can say to this one. If he can't solve the problem with all that help, we will not hand him the ready solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.