0

Please help me make an infinite loop: after the cases show their answer, the program should instantly ask the user again.

package calendartool;

import java.io.Console;

public class CalendarTool {


    public static void main(String[] args) {

        Console C = System.console();
        int month = Integer.parseInt(C.readLine("Please put a valid month: \n"));
        int year = Integer.parseInt(C.readLine("Please put a valid year: \n"));

            switch (month) {
                case 1:
                    System.out.println("The month is January!");
                    System.out.println("January has 31 days!");
                    break;

            }



    }
}
Katriel
  • 114,760
  • 19
  • 131
  • 163
ihatecodes
  • 61
  • 4

4 Answers4

4

i prefer, but it's a matter of taste...

while(true){
}
PbxMan
  • 7,421
  • 1
  • 35
  • 40
1

The infinite loop:

while(true) {
    //Your code here
}
Tom G
  • 3,595
  • 1
  • 19
  • 19
1

I've always been partial to:

for(;;)
{
    //Do stuff
}

If only because it's quicker to type.

MrLore
  • 3,713
  • 2
  • 25
  • 36
0

Use do-while loop to ask user that he wants to continue or not.

br is bufferredreader instance variable.

 char ch=(char)br.read();
 do
 {
System.out.println("press 1 for month jan");
System.out.println("do you want to continue(Y/N)");

}
while(ch=='Y'||ch=='Y');
Abhishekkumar
  • 1,112
  • 8
  • 24