2015-10-15 107 views
-1

我正在研究一個基於2個類的程序,這個程序可以在使用異常處理的同時轉換軍事時間。我遇到了3個編譯器錯誤。前兩個是Time類,它說它找不到可變時間的符號。第三個錯誤與我如何在TimeConverterTest類中顯示我的結果有關,它說它無法找到militaryTime變量。異常處理錯誤

import javax.swing.*; 

public class Time 
{ 

    private int hours; 
    private int minutes; 
    private boolean afternoon; 
    String am_pm; 

    public Time(String time) 
    { 

    } 

    public String convertToMilitary(String militaryTime) 
    { 
     try{ 
      if (time == null) 
      { 
       throw new IllegalArgumentException ("No time was entered"); 

      } 
      else if (time > 5) 
      { 
       throw new IllegalArgumentException ("Less than 5 characters were entered"); 
      } 
      else 
      { 
       if (militaryTime.charAt(2) != ':') 
       { 
        throw new IllegalArgumentException ("The third character was not a colon"); 
       } 
       else if (!Character.isDigit(militaryTime.charAt(0))) 
       { 
        throw new IllegalArgumentException ("The first character was not passed as a digit"); 
       } 
       else if (!Character.isDigit(militaryTime.charAt(1))) 
       { 
        throw new IllegalArgumentException ("The second character was not passed as a digit"); 
       } 
       else if (!Character.isDigit(militaryTime.charAt(2)) ) 
       { 
        throw new IllegalArgumentException ("The fourth character was not passed as a digit"); 
       } 
       else if (!Character.isDigit(militaryTime.charAt(4))) 
       { 
        throw new IllegalArgumentException ("The fifth character was not passed as a digit"); 
       } 
       else 
       { 

        hours = Integer.parseInt(militaryTime.substring(0,2)); 
        minutes = Integer.parseInt(militaryTime.substring(3,5)); 

        if(hours > 23) 
        { 
         throw new IllegalArgumentException ("Hours passed were greater than 23"); 
        } 
        else if(minutes > 59) 
        { 
         throw new IllegalArgumentException ("Minutes passed were greater than 59"); 
        } 
        else if (hours > 12) 
        { 
         afternoon = false; 
        } 
        else if (hours == 0) 
        { 
         hours = 12; 

        } 
        else if (hours == 12) 
        { 
         afternoon = true; 

        } 
        else 
        { 
         System.out.println(hours + ":" + minutes); 
        } 

         } 

        }  
     } 
     catch (IllegalArgumentException exception) 
     { 
      JOptionPane.showMessageDialog(null, 
        exception.getMessage(), 
        "Invalid Argument", JOptionPane.ERROR_MESSAGE); 
      System.exit(0); 
     } 

     String am_pm = hours + ":" + minutes; 

     return "\n\n" + hours + ":" + minutes + " " + am_pm + "\n\n"; 
    } 

} 

和TimeConverterTest是

import javax.swing.*; 
import java.util.*; 
import java.util.ArrayList; 
import java.util.regex.Pattern; 

public class TimeConverterTest { 

    public static final String[] menuChoice = {"Convert Military Time to Standard", 
      "Convert Standard Time to Military", 
      "Exit the System"}; 

    public static void main(String[] args) 
    { 
     while(true){ 
      JFrame frame = new JFrame("TIME CONVERSION MACHINE"); 
      String option = (String) JOptionPane.showInputDialog(frame, 
        "What do you want to do?", 
        "Time Conversion Machine", 
        JOptionPane.QUESTION_MESSAGE, 
        null, 
        menuChoice, 
        menuChoice[0]); 

      int choice = Arrays.asList(menuChoice).indexOf(option) + 1; 

      System.out.print(choice); 


      Scanner input; 

      switch (choice) { 
       case 1: 


        input = new Scanner(System.in); 

        // Prompt user to enter Military time with message "Please enter Military Time:" 
        //CODE to make it work 

        System.out.printf("Please enter Military Time: "); 

        // Create militaryTime variable to accept input 
        //CODE to make it work; 

        String time; 
        time = input.nextLine(); 
        // Create new 'timeToConvert' object using militaryTime 
        //CODE to make it work 

        Time timeToConvert = new Time(time); 


        // Using your newly created object call the necessary method to convert the time and 
        // Display your results 


        //Display your results 
        String results = timeToConvert.convertToMilitary(militaryTime); 
        JOptionPane.showMessageDialog(null,results); 

        break; 

       case 2: 


       case 3: 

        System.exit(0); 

      } 

     } 
    } 

}// end of class TimeConverterTest 

回答

0

你會得到cannot be resolved to a variable編譯錯誤,如果變量沒有聲明

Time

public String convertToMilitary(String militaryTime) { 
    try { 
     if (militaryTime == null) { 
      throw new IllegalArgumentException("No time was entered"); 

     } else if (militaryTime.length() < 5) { 
      throw new IllegalArgumentException("Less than 5 characters were entered"); 

TimeConverterClass

 String results = timeToConvert.convertToMilitary(time); 
0

您必須聲明的時間變量作爲

String time; 

    public Time(String time) 
    { 
     this.time = time; 
    } 
在Time類

,以便它可以用來和你在

// Create militaryTime variable to accept input 
//CODE to make it work; 

String time; 
time = input.nextLine(); 

獲取字符串時在放然後傳遞變量爲

//Display your results 
    String results = timeToConvert.convertToMilitary(time); 
0

你是正確的通過time可變成Time類的構造函數,但是,您從來沒有真正將該變量傳輸到類中。下面我宣佈了一個實例變量time,低於am_pm,並在構造函數中設置它等於傳入的time變量。

private int hours; 
private int minutes; 
private boolean afternoon; 
String am_pm; 
String time; 

public Time(String time) 
{ 
    this.time = time; 
} 

您似乎對方法參數和方法返回值感到困惑。您需要在構造函數內傳遞time變量並在類中使用它,或者直接將其傳遞給convertToMilitary,而不必通過構造函數。

convertToMilitary的返回值是您打算的軍事時間,而參數應該只是常規時間值。牢記這一點,你的代碼應該看起來像這樣。

String time; 
time = input.nextLine(); 
// Create new 'timeToConvert' object using militaryTime 
//CODE to make it work 

Time timeToConvert = new Time(time); 
// Using your newly created object call the necessary method to convert the  time and 
// Display your results 

//Display your results 
String militaryTime = timeToConvert.convertToMilitary(time); 
JOptionPane.showMessageDialog(null,militaryTime);