|
Hi, hope there is someone that can help me. I have been trying to make a loop so every time you press 0 you quit the program, I've been trying all kind of while loops where (value !=0) should terminate the loop but it doesn't seemes to work, so I would really appreciate some help.
public static void main(String[] args) {
final int CALENDAR_START_DATE = 1582;
String another = "y";
Scanner scan = new Scanner(System.in);
while (another.equalsIgnoreCase("y"))
{
System.out.print("Enter the year to be tested(0 to quit): ");
int year = Integer.parseInt(scan.nextLine());
String isLeapYear = new String(year + " is a leap year.");
String isNotLeapYear = new String(year + " is not a leap year.");
String errorMessage = new String("Invalid input, has to be greater or equal to 1582");
if (year%4==0 && year%100!=0 && year%400!=0 && year>=CALENDAR_START_DATE||
year%4==0 && year%100==0 && year%400==0 && year>=CALENDAR_START_DATE)
System.out.println(isLeapYear);
else if (year%4!=0 && year%100!=0 && year%400!=0 && year>=CALENDAR_START_DATE ||
year%4==0 && year%100==0 && year%400!=0 && year>=CALENDAR_START_DATE)
System.out.println(isNotLeapYear);
else
{
System.out.println(errorMessage);
System.out.println();
}
System.out.print("Test another year (y/n)? ");
another = scan.nextLine();
}
}
}
|
|
|
import java.util.Scanner;
public class QuitTest {
public static void main(String[] args) {
final int CALENDAR_START_DATE = 1582;
boolean another = true;
Scanner scan = new Scanner(System.in);
while (another)
{
System.out.print("Enter the year to be tested(0 to quit): ");
String entry = scan.nextLine();
if(entry.equals("0"))
break;
int year = Integer.parseInt(entry);
String isLeapYear = year + " is a leap year.";
String isNotLeapYear = year + " is not a leap year.";
String errorMessage = "Invalid input, has to be greater or equal to 1582";
if (year%4==0 && year%100!=0 && year%400!=0 && year>=CALENDAR_START_DATE ||
year%4==0 && year%100==0 && year%400==0 && year>=CALENDAR_START_DATE)
System.out.println(isLeapYear);
else if (year%4!=0 && year%100!=0 && year%400!=0 &&
year>=CALENDAR_START_DATE || year%4==0 && year%100==0 &&
year%400!=0 && year>=CALENDAR_START_DATE)
System.out.println(isNotLeapYear);
else
{
System.out.println(errorMessage);
System.out.println();
}
System.out.print("Test another year (y/n)? ");
entry = scan.nextLine();
if(entry.equalsIgnoreCase("n"))
another = false;
}
}
}
|
|
|
|
|
It¨s great, thanks for the held :D
|
|
|
|
|
|
|
|