2017-11-11 136 views
0

当我在下面的代码中输入“today”时,执行if(date_holder.contains("/"))。我该如何解决? 对不起,我的代码不成熟。任何帮助表示赞赏。当我给今天打印今天的日期与您提及,但您的代码更改为以下格式Java代码逻辑错误

import java.util.*; 
 
import java.io.*; 
 
import java.text.*; 
 
/** 
 
* Write a description of class Driver here. 
 
* 
 
* @author (your name) 
 
* @version (a version number or a date) 
 
*/ 
 
public class Driver 
 
{ 
 
    //extra credit 
 
    //do you want to enter a date or use todays date can use api to get it 
 
    public static void main(String[] args) 
 
    { 
 
     Scanner keyboard = new Scanner(System.in); 
 
     int day = 0; 
 
     int month = 0; 
 
     int year = 0; 
 
     char choice; 
 
     String date_holder; 
 
     StringTokenizer stok; 
 
     int count = 0; // use this variable to create all of the sub menus 
 
     //for example 0 is the first sub menu 1 s the next sub menu 
 
     
 
     while(count == 0) 
 
     { 
 
      System.out.println("Enter a date or type 'today' for todays date \nor enter 'quit' to quit: "); 
 
      date_holder = keyboard.next(); 
 
      date_holder.trim();//trim any white space 
 
      System.out.println("[email protected]# " + date_holder); 
 
      if(date_holder.contains("/")) 
 
      { 
 
       stok = new StringTokenizer(date_holder,"/"); 
 
       month = Integer.parseInt(stok.nextToken()); 
 
       day = Integer.parseInt(stok.nextToken()); 
 
       year = Integer.parseInt(stok.nextToken()); 
 
       Date date = new Date(day, month, year); 
 
       count = 1; 
 
       while(count == 1) 
 
       { 
 
        do 
 
        { 
 
         //zz 
 
         System.out.println("Current Date: " + month + "/" + day + "/" + year + 
 
         "\nEnter 'a' if you want to add days to the date \n" + 
 
         "or enter a 's' if you want to subtract the date \n" + 
 
         "or enter a 'd' get the days between the current date and another date \n " + 
 
         "or enter a 'f' if you want to format the date.\n"); 
 
         choice = keyboard.next().charAt(0); 
 
        }while(choice != 'a' && choice != 's' && choice != 'd' && choice != 'f'); 
 
        String number; //to store the user's number input 
 
        String answer; 
 
        if(choice == 'a')//too add the date 
 
        { 
 
         System.out.println("Enter a number to add:"); 
 
         number = keyboard.next(); 
 
         date.add(Integer.parseInt(number)); 
 
         answer = date.toString(); 
 
         System.out.println("answer: " + answer); 
 
         count = 0; 
 
        } 
 
        else if(choice == 's')//too subtract the date 
 
        { 
 
         System.out.println("Enter a number of days to subtract:"); 
 
         number = keyboard.next(); 
 
         date.subtract(Integer.parseInt(number)); 
 
         answer = date.toString(); 
 
         System.out.println("answer: " + answer); 
 
         count = 0; 
 
        } 
 
        else if(choice == 'd')//too get the days between 
 
        { 
 
         int inMonth, inDay, inYear = 0; 
 
         System.out.println("Enter a date to use with " + month + "/" + day + 
 
         "/" + year + ":"); 
 
         number = keyboard.next(); 
 
         if(number.contains("/")) 
 
         { 
 
          stok = new StringTokenizer(number,"/"); 
 
          inMonth = Integer.parseInt(stok.nextToken()); 
 
          inDay = Integer.parseInt(stok.nextToken()); 
 
          inYear = Integer.parseInt(stok.nextToken()); 
 
          Date inDate = new Date(inDay, inMonth, inYear); 
 
          Date outDate = new Date(day, month, year); 
 
          if(year < inYear) 
 
          { 
 
           System.out.println("The days between " + date.toString() + 
 
           "and" + inDate.toString() + " is " + inDate.daysBetween(outDate) + " days."); 
 
          } 
 
          else 
 
          { 
 
           System.out.println("The days between " + date.toString() + 
 
           "and" + inDate.toString() + " is " + outDate.daysBetween(inDate) + "days."); 
 
          } 
 
          count = 0; 
 
         } 
 
        } 
 
        else if(choice == 'f')//too change the format and (extra credit) 
 
        { 
 
         do 
 
         { 
 
          //zz 
 
          System.out.println("Current Date: " + month + "/" + day + "/" + year + 
 
          "\nEnter 's' if you want the format day//month//year \n" + 
 
          "or enter a 'l' if you want the format day of month, year \n" + 
 
          "or enter a 'j' to get the Julian date \n" + 
 
          "or enter a 'h' if you want to get the horoscope Zodiac of the date.\n" + 
 
          "or enter a 'c' if you want to get the Chinese Zodiac of the date.\n"+ 
 
          "or enter a 'e' if you want to get the Easter date of the year.\n"); 
 
          choice = keyboard.next().charAt(0); 
 
         }while(choice != 's' && choice != 'l' && choice != 'j' && choice != 'h' && choice != 'c' && choice != 'e'); 
 
         //I don't need any if statments to verify the choices because 
 
         //it is all handled in the getDate() method 
 
         System.out.println(date.getDate(choice)); 
 
         count = 0;//end the submenu loop 
 
        } 
 
       } 
 
      } 
 
      else if(date_holder.equals("today")) 
 
      { 
 
       DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); 
 
       Calendar cal = Calendar.getInstance(); 
 
       System.out.println(dateFormat.format(cal)); 
 
      } 
 
      else if(date_holder.equals("quit")) 
 
      { 
 
       count = -1; 
 
      } 
 
     } 
 
     System.out.println("goodbye!"); 
 

 
    } 
 
}

+0

TL; DR。请发布[mcve]。此外,“对我的代码sl Sorry不乐” - 如果您认为它很sl,,请在发布之前修正它。 – c0der

回答

0

你的代码工作正常。

else if(date_holder.equals("today")) 
      { 
       DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); 
       Calendar cal = Calendar.getInstance(); 
       System.out.println(dateFormat.format(cal.getTime())); 
      } 

您不能格式化日历对象,您必须使用getTime方法来获取Date对象。 您的代码看起来相当笨拙,请研究更多关于Date api here