簡體   English   中英

線程“主”中的異常java.util.NoSuchElementException錯誤

[英]Exception in thread “main” java.util.NoSuchElementException Error

我在執行以下作業時遇到了麻煩:

“編寫程序接受月日年(2000年8月23日)形式的任何兩個日期,並用空格分隔,並計算兩個日期之間經過的總天數,包括開始和結束的天數。記住leap年可以除以4的年份,百年必須將其除以400的年份,例如1600或2000(1800不是a年)。您可以使用任何類型的日期(或存儲的日期)來測試程序在一個文件中),但最終必須使用下面顯示的數據運行。可以在屏幕上顯示系統輸出。”

到目前為止,我已經有了這段代碼,並且可以編譯:

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
線程“主”中的異常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;

 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM