线程“main”中的异常java.util.NoSuchElementException错误

我在以下任务中遇到问题:

“编写一个程序,以日月年(8 23 2000)的形式接受任意两个日期,用空格分隔,并计算两个日期之间的总天数,包括开始日期和结束日期。闰年是可以被4除尽的一年,除了百年以外,必须可以被400整除,比如1600或2000(1800不是闰年),你可以用任何输入的日期(或存储的日期在一个文件中),但最终必须运行下面显示的数据,屏幕上的系统输出是可以接受的。“

到目前为止我有这个代码,它编译:

import java.util.Scanner;
import java.io.*;

public class Project3
{
public static void main(String[] args) throws IOException
{

  int m1, d1, y1, m2, d2, y2;
  Scanner scan = new Scanner(new FileReader("dates.txt"));

  for(int i = 0 ; i < 6; ++i)
  {

     m1 = scan.nextInt();
     d1 = scan.nextInt();
     y1 = scan.nextInt();
     m2 = scan.nextInt();
     d2 = scan.nextInt();
     y2 = scan.nextInt();

     System.out.println("The total number of days between the dates you entered are: " + x(m1,    m2, d1, d2, y1, y2));
  }
  } 
   public static int x (int m1, int d1, int y1, int m2, int d2, int y2) 
  {
  int total = 0;
  int total1 = 0;
  int total2 = 0;

  if( m1 == m2 && y1 == y2)     
  {                             
     total = d2 - d1 +1;
  }

  else if ( y1 == y2 )
  {
     total = daysInMonth( m2 , y2 ) - d1 + 1;
  }

  for(int i = m1; i < m2 ; ++i)  
  {
     total1 += daysInMonth( m2, y2 );
  }

  for (int i = y1; i < y2; i++)
  {
     total2 += daysInYear ( y2 );
  }

  total += total1 + total2;
  return total;
 }

//Methods       
 public static boolean isLeap(int yr)
{
  if(yr % 400 == 0)
     return true;
  else if (yr % 4 == 0 && yr % 100 !=0)
     return true;
  else return false;

 }

  public static int daysInMonth( int month , int year)
 {  
  int leapMonth;

  if (isLeap(year) == true)
  {
     leapMonth = 29;
  }
  else
  {
     leapMonth = 28;
  }

  switch(month)
  {
     case 1:
     case 3:
     case 5:
     case 7:
     case 8:
     case 10:
     case 12: return 31;
     case 4:
     case 6:
     case 9:
     case 11: return 30;
     case 2: return leapMonth;
     }
   return 28;     
  }

 public static int daysInYear(int year)
 { 
  if (isLeap(year))
     return 366;
  else 
     return 365;

 }
}

我遇到的问题是我输出的数据文件中的日期不正确。 我输入这两个日期7 4 17761 1 1987并得到723795而不是正确答案76882 。 我输入的任何其他日期,他们也是不正确的。

这也是我得到的错误。

您输入的日期之间的总天数为:723795
线程“main”java.util.NoSuchElementException中的异常
在java.util.Scanner.throwFor(Scanner.java:862)
在java.util.Scanner.next(Scanner.java:1485)
在java.util.Scanner.nextInt(Scanner.java:2117)
在java.util.Scanner.nextInt(Scanner.java:2076)
在Project3.main(Project3.java:15)

数据文件:

7 
4 
1776        
1 
1 
1987 

请任何帮助,非常感谢!


首先,当你调用函数x时,你以错误的顺序传递参数。 该函数被声明为x(m1,d1,y1,m2,d2,y2),但是您要调用x(m1,m2,d1,d2,y1,y2)。 这很难发现。 :)

另外,我已经根据您的原始代码清理了代码。 请注意,您需要在循环中使用变量“i”而不是“y1”或“m2”

例如,

for(int i = m1; i < m2 ; ++i)  
{
  total1 += daysInMonth( m2, y2 );
}

这段代码没有意义。 你想使用daysInMonth(i,y2);

这是我的建议:

public static void main(String[] args) throws IOException
{

    int m1, d1, y1, m2, d2, y2;
    Scanner scan = new Scanner(new FileReader("dates.txt"));

    for(int i = 0 ; i < 6; ++i)
    {

        m1 = scan.nextInt();
        d1 = scan.nextInt();
        y1 = scan.nextInt();

        m2 = scan.nextInt();
        d2 = scan.nextInt();
        y2 = scan.nextInt();

        System.out.println("The total number of days between the dates you entered are: " + x(m1, d1, y1, m2, d2, y2));
    }
} 
public static int x (int m1, int d1, int y1, int m2, int d2, int y2)  {
    int total = 0;
    total = d2 - d1 + 1;
    if (y1 == y2) {
        for(int i = m1; i < m2 ; ++i) {
            total += daysInMonth(i, y2);
        }
    } else {
        for (int i = m1; i <= 12; i++) {
            total += daysInMonth(i, y1);
        }
        for (int i = y1+1; i < y2; i++) {
            total += daysInYear(i);
        }
        for (int i = 1; i < m2 ; i++) {
            total += daysInMonth(i, y2);
        }
    }
    return total;
}

结果是:

java Project3
The total number of days between the dates you entered are: 76882

这是整个代码:

import java.util.Scanner;
import java.io.*;

public class Project3
{
    public static void main(String[] args) throws IOException
  {

      int m1, d1, y1, m2, d2, y2;
      Scanner scan = new Scanner(new FileReader("dates.txt"));

      for(int i = 0 ; i < 6; ++i)
      {

          m1 = scan.nextInt();
          d1 = scan.nextInt();
          y1 = scan.nextInt();

          m2 = scan.nextInt();
          d2 = scan.nextInt();
          y2 = scan.nextInt();

          System.out.println("The total number of days between the dates you entered are: " + x(m1, d1, y1, m2, d2, y2));
      }
  } 
public static int x (int m1, int d1, int y1, int m2, int d2, int y2)  {
    int total = 0;
    total = d2 - d1 + 1;
    if (y1 == y2) {
        for(int i = m1; i < m2 ; ++i) {
            total += daysInMonth(i, y2);
        }
    } else {
        for (int i = m1; i <= 12; i++) {
            total += daysInMonth(i, y1);
        }
        for (int i = y1+1; i < y2; i++) {
            total += daysInYear(i);
        }
        for (int i = 1; i < m2 ; i++) {
            total += daysInMonth(i, y2);
        }
    }
    return total;
}

//Methods       
 public static boolean isLeap(int yr)
{
  if(yr % 400 == 0)
     return true;
  else if (yr % 4 == 0 && yr % 100 !=0)
     return true;
  else return false;

 }

  public static int daysInMonth( int month , int year)
 {  
  int leapMonth;

  if (isLeap(year) == true)
  {
     leapMonth = 29;
  }
  else
  {
     leapMonth = 28;
  }

  switch(month)
  {
     case 1:
     case 3:
     case 5:
     case 7:
     case 8:
     case 10:
     case 12: return 31;
     case 4:
     case 6:
     case 9:
     case 11: return 30;
     case 2: return leapMonth;
     }
   return 28;     
  }

 public static int daysInYear(int year)
 { 
  if (isLeap(year))
     return 366;
  else 
     return 365;

 }
}
链接地址: http://www.djcxy.com/p/96117.html

上一篇: Exception in thread "main" java.util.NoSuchElementException Error

下一篇: How can i insert multiple input in one line in 2dArray looping