-
stumped on do while loops
Having a little trouble creating a program.. Well to start I need to create a program that will list all of the numbers between two input values, in either ascending or descending order. The user will enter a lower bound and upper bound number through a scanner object, and enter whether they want Ascending or Descending via A/a or D/d. if the user enters an incorrect A/a or D/d the user will be stated error or what not.. but here is my code.. any help would be greatly appreciated. thank you.
Code:
import java.util.Scanner;
public class assign4
{
public static void main(String[] args)
{
int low, up;
Scanner input = new Scanner(System.in);
String A, a, D, d, check;
do
{
System.out.print("Enter lower bound integer: ");
low = input.nextInt();
System.out.print("Enter upper bound integer: ");
up = input.nextInt();
System.out.println("Lower bound: " + low);
System.out.println("Upper bound: " + up);
}
while (low <= up);
{
System.out.println("Error!! lower bound must be smaller.");
}
System.out.print("Would you like Ascending or Descending order? [A/a or D/d]: ");
A = input.check;
a = input.check;
D = input.check;
d = input.check;
while (check.equals("A,a"));
{
System.out.println(low + 1);
}
while (check.equals("D,d"));
{
System.out.println(top - 1);
}
}
}
-
stumped on do while loops
Ok, your first loop is a do - while. That means the code between the do and the while will be executed while the condition is true. See the Java Tutorial for details.
The code in brackets following your loop
Code:
{
System.out.println("Error!! lower bound must be smaller.");
}
is not part of the loop and will be executed once each time the program is run. If you want it printed as an error message, placing it in an if statement inside the loop would be more appropriate.
In this segment
Code:
System.out.print("Would you like Ascending or Descending order? [A/a or D/d]: ");
A = input.check;
a = input.check;
D = input.check;
d = input.check;
I'm not sure what you're doing. The input object is of class Scanner, which doesn't have a check method (scanner JavaDoc). Also, you don't really need to have a separate String object for each of the user choices; one object named "choice" or something similar would probably be good. Once you have the user input in choice (perhaps via input.nextLine()), you'd want to decide whether to do ascending or descending (if statement again?) based on that value. You can use toLower() to make your comparisons seem case insensitive.
Your last two loops, should loop based on whether the two input numbers are equal. Make sure you actually increment or decrement the values as top - 1 and low + 1 only create a modified value, it is not assigned anywhere. The increment (++) and decrement (--) operators can help you here.
If you don't really increment and decrement the values, your while loop will be endless and your program will never exit.
Keep trying and post your questions here.
Hope this helps,
alan
Last edited by ajhampson; 02-18-2009 at 01:57 PM.
Reason: Fix typo.
Similar Threads
-
Replies: 6
Last Post: 11-10-2007, 07:19 AM
-
By defyinggravity in forum AJAX
Replies: 0
Last Post: 02-07-2007, 04:35 PM
-
By rohan076 in forum Java
Replies: 5
Last Post: 03-29-2006, 06:15 PM
-
By Rythm in forum ASP.NET
Replies: 0
Last Post: 06-15-2003, 04:10 PM
-
By J. Giudice in forum ASP.NET
Replies: 0
Last Post: 10-12-2000, 10:16 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks