为什么这个程序会立即循环,而不是在应该循环的时候循环? import java.util.Scanner; 导入java.util.InputMismatchException; 公共课堂作业2{ 公共静态int t1; 公共静态int t2; 公共静态int x; 公共静态int y1; 公共静态int m1; 公共静态int d1; 公共静态int y2; 公共静态int m2; 公共静态int d2; 公共静态无效日期1(){ int x=0;//用于循环的整数。 如果长度=5,则执行//要遵循的过程。 {//在循环限制下通知程序要做什么。 扫描仪=新的扫描仪(System.in); 尝试 { System.out.println(“请输入第一个日期”); System.out.println(“请输入年份:”); y1=scanner.nextInt(); System.out.println(“请输入月份:”); m1=scanner.nextInt(); System.out.println(“请输入日期:”); d1=scanner.nextInt();} catch(InputMismatchException InputMismatchException)//输入错误字符时出错。 { scanner.nextLine(); System.err.printf(“必须输入整数。请重试。\n”);//提示用户输入整数。 }//将循环设置为三次尝试。 } 而(xj){ 如果(m1==1+3+5+7+8+10+12){ t1=((365*y1)+d1+31); } 其他的 如果(m1==2){ t1=((365*y1)+d1+28); } 其他的 如果(m1==4+6+9+11){ t1=((365*y1)+d1+30); } x=x+1;//将循环设置为三次尝试 而(x
我的循环没有正确响应的原因是什么?当输入非整数或输入日期的总天数小于693502(1990年1月1日)时,如何设置正确的错误捕获和循环。您应该删除while行末尾的分号,并在括号中包含在循环中的区域周围排序,即{和}您的循环条件错误。例如,在为什么这个程序会立即循环,而不是在应该循环的时候循环? import java.util.Scanner; 导入java.util.InputMismatchException; 公共课堂作业2{ 公共静态int t1; 公共静态int t2; 公共静态int x; 公共静态int y1; 公共静态int m1; 公共静态int d1; 公共静态int y2; 公共静态int m2; 公共静态int d2; 公共静态无效日期1(){ int x=0;//用于循环的整数。 如果长度=5,则执行//要遵循的过程。 {//在循环限制下通知程序要做什么。 扫描仪=新的扫描仪(System.in); 尝试 { System.out.println(“请输入第一个日期”); System.out.println(“请输入年份:”); y1=scanner.nextInt(); System.out.println(“请输入月份:”); m1=scanner.nextInt(); System.out.println(“请输入日期:”); d1=scanner.nextInt();} catch(InputMismatchException InputMismatchException)//输入错误字符时出错。 { scanner.nextLine(); System.err.printf(“必须输入整数。请重试。\n”);//提示用户输入整数。 }//将循环设置为三次尝试。 } 而(xj){ 如果(m1==1+3+5+7+8+10+12){ t1=((365*y1)+d1+31); } 其他的 如果(m1==2){ t1=((365*y1)+d1+28); } 其他的 如果(m1==4+6+9+11){ t1=((365*y1)+d1+30); } x=x+1;//将循环设置为三次尝试 而(x,java,eclipse,Java,Eclipse,我的循环没有正确响应的原因是什么?当输入非整数或输入日期的总天数小于693502(1990年1月1日)时,如何设置正确的错误捕获和循环。您应该删除while行末尾的分号,并在括号中包含在循环中的区域周围排序,即{和}您的循环条件错误。例如,在date1输入循环中,您有while(x
date1
输入循环中,您有while(x<3)代码>但是x
从未在该循环中被修改。首先,查看错误并将其编译
一半的while循环是独立的,不做任何事情李>
所有while循环都引用未设置的x
此外,您正在使用局部变量重写类级别x变量,这目前不是一个错误,但很可能导致一个错误
一般调试建议,将问题分解成小块,并确认小块工作。在开始下一部分之前保存工作(理想情况下是版本控制),以便您可以始终返回工作代码。如果这是作业,则应将其标记为作业。我真的希望这不是你所说的,因为我根本看不懂代码在做什么。你已经标记了这个Eclipse。Ctrl+Shift+F将自动使用合理的缩进设置此格式。这将帮助您查看程序的结构,这可能有助于您调试它。
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.");
}
}