import java.util.Scanner;
import java.util.InputMismatchException;
public class assignment2 {
public static int t1;
public static int t2;
public static int x;
public static int y1;
public static int m1;
public static int d1;
public static int y2;
public static int m2;
public static int d2;
public static void date1() {
int x = 0; // Integer for looping.
do // Process to follow if length == 5.
{ // Notify program what to do while under loop limit.
Scanner scanner = new Scanner (System.in);
try
{
System.out.println("Please enter the first date ");
System.out.println ("Please enter the year: ");
y1=scanner.nextInt();
System.out.println("Please enter the month: ");
m1=scanner.nextInt();
System.out.println("Please enter the day: ");
d1=scanner.nextInt();}
catch (InputMismatchException inputMismatchException) // Error if wrong character is inputted.
{
scanner.nextLine() ;
System.err.printf("You must enter intergers. Please try again.\n"); // Prompt user to enter integers.
} // Set loop to three attempts.
}
while (x < 3) ;
int j = 693502;
if (t1 > j) {
if (m1==1 + 3 + 5 + 7 + 8 + 10 + 12) {
t1 = ((365*y1)+d1+31);
}
else
if (m1==2) {
t1 = ((365*y1)+d1+28);
}
else
if (m1==4 + 6 + 9 + 11); {
t1 = ((365*y1)+d1+30);
}
x = x + 1; // Set loop to three attempts
while (x < 3) ;
}
else {
System.err.printf ("Error. Please enter a date after Jan 1st 1900.\n") ;
}
}
public static void date2() {
int x = 0; // Integer for looping
do // Process to follow if length == 5
{
Scanner scanner = new Scanner (System.in);
try
{
System.out.println("Please enter the first date ");
System.out.println ("Please enter the year: ");
y2=scanner.nextInt();
System.out.println("Please enter the month: ");
m2=scanner.nextInt();
System.out.println("Please enter the day: ");
d2=scanner.nextInt();}
catch (InputMismatchException inputMismatchException) // Error if wrong character is inputted.
{
scanner.nextLine() ;
System.err.printf("You must enter intergers. Please try again. "); // Prompt user to enter integers.
}
x = x + 1; // Set loop to three attempts
}
while (x < 3) ;
int j = 693502;
if (t1 > j) {
if (m1==1 + 3 + 5 + 7 + 8 + 10 + 12) {
t1 = ((365*y1)+d1+31);
}
else
if (m1==2) {
t1 = ((365*y1)+d1+28);
}
else
if (m1==4 + 6 + 9 + 11); {
t1 = ((365*y1)+d1+30);
}
x = x + 1; // Set loop to three attempts.
while (x < 3) ;
}
else {
System.err.printf ("Error. Please enter a date after Jan 1st 1900. ");
}
}
public static void finaldate1() {
x = Math.abs(t1-t2);
}
public static void main(String[] args) {
date1();
date2();
finaldate1();
System.out.println("The difference between the two dates is: " + x + " days.");
}
}
我的循環沒有正確響應的原因是什麼?我怎樣才能設置一個適當的錯誤捕獲和循環,當非整數輸入或如果輸入日期的總天數小於693502(1990年1月1日)。這個程序爲什麼會立即循環,而不是當它應該是什麼時候?
我完全看不懂。 –
如果這是家庭作業,您應該將其標記爲家庭作業。而且我真的希望這不是你想要的,因爲我根本看不懂代碼在做什麼。 –
這更好:) –