Results 1 to 5 of 5
- 08-04-2012, 06:08 PM #1
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
How do I make my println repeat itself?
Hey, I've been making my first from-scratch program and I wonder about an If statement:
Ignoring unrelated flaws of this code, my question is this: How do I make it so that, the next time you type a value > 12 or < 1, it reruns the same if statement so that the same line pops up consistently. Do I need to make a separate method for it and how would that method look like?Java Code:int bMonth; // bMonth is what the user types as his birthday month in the console, from 1-12. System.out.println("Please enter your birthday month, as a number from 1 to 12:"); bMonth = info.nextInt(); if (bMonth < 1 || bMonth > 12) { System.out.println("A year only has 12 months. Please enter your birthday month again:"); bMonth = info.nextInt(); } else { System.out.println("Now enter the present month of this year:"); bMonth = info.nextInt(); }
Thanks.
-
Re: How do I make my println repeat itself?
You need to enclose those statements in a while(...) loop so that they keep repeating until the while loop's boolean expression is false. For more on these constructs, please check out The While Loop.
- 08-04-2012, 06:20 PM #3
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
Re: How do I make my println repeat itself?
Ok, thanks. I'm familiar with the While loop but was unsure on its behavior. As I understand it now, a while loop just reruns itself constantly until it can get out of it's own circle, right?
Problem: It still just runs it once:
I know there's something I've not added here, but I'm just not sure what it is. Maybe I need to make a do while loop?Java Code:System.out.println("Now, please enter your birthday month (from 1-12):"); bMonth = info.nextInt(); while (bMonth > 12 || bMonth <= 0) { System.out.println("A year has 12 months. Please type your birthday month again:"); bMonth = info.nextInt(); } System.out.println("This is the next question in the program");Last edited by DrMadolite; 08-04-2012 at 06:40 PM.
-
Re: How do I make my println repeat itself?
Your loop works OK for me. My code that I used:
A do-while loop could work well here as well.Java Code:import java.util.Scanner; public class Foo001 { public static void main(String[] args) { Scanner info = new Scanner(System.in); System.out.println("Now, please enter your birthday month (from 1-12):"); int bMonth = info.nextInt(); while (bMonth > 12 || bMonth <= 0) { System.out.println("I'm sorry, but the year has 12 months, not more and definitely not 0. Please type your birthday month again:"); bMonth = info.nextInt(); } System.out.println("This is the next question in the program"); } }
- 08-04-2012, 06:45 PM #5
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
How do you make program repeat question?
By dexter in forum New To JavaReplies: 3Last Post: 02-17-2011, 01:58 AM -
can't make ui:repeat work
By kaghamdi in forum JavaServer Faces (JSF)Replies: 6Last Post: 12-31-2010, 11:43 AM -
can't make ui:repeat work
By kaghamdi in forum JavaServer Faces (JSF)Replies: 1Last Post: 09-26-2010, 07:19 PM -
Println VS system.out.println
By ccie007 in forum New To JavaReplies: 2Last Post: 05-20-2010, 08:52 AM -
difference between system.out.println() & out.println()
By wickedrahul9 in forum Advanced JavaReplies: 5Last Post: 10-18-2008, 11:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks