2015-10-18 49 views
-1

創建一個簡單的學費程序,我陷入了(我認爲是)收尾。需要使用JOptionPane多個答案,如果/如果其他

問題是我不知道如何計算「小時數」變量,以便在聲明後的所有內容中保持不變。

這是我與Java的第一年,所以請原諒我的壞風格。

import java.util.*; 
import javax.swing.JOptionPane; 

/** 
The application calculates the cost of tuition for one semester at OCC. 
*/ 

public class TuitionOCC 
    { 
public static void main(String[] args) 
    { 

    String input; 
    int residency; 
    byte credits; 
    int residentTuition; 
    int nonTuition; 
    int internationalTuition; 
    int hours; 
    double tuition; 


    residentTuition = 82; 
    nonTuition = 154; 
    internationalTuition = 216; 


    JOptionPane.showMessageDialog(null, "Calculate your tuition!",  "Tuition",JOptionPane.INFORMATION_MESSAGE); 

    input = JOptionPane.showInputDialog("Are you a:\n" + 
    "1- College District Resident\n" + 
    "2- Non-Resident of College District\n" + 
    "3- Out-of-State or International Student");    

    residency = Integer.parseInt(input); 

    if (residency == 1) 
    { 


    input = JOptionPane.showInputDialog("How many credit hours are you taking?: "); 

    hours = Integer.parseInt(input); 
    if (hours <=0) 
    JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE); 
} 

else if (residency == 2) 
{ 

residency = nonTuition; 

    input = JOptionPane.showInputDialog("How many credit hours are you taking?: "); 

    hours = Integer.parseInt(input); 
    if (hours <=0) 
    JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE); 
} 
else if (residency == 3) 
{ 

residency = internationalTuition; 

input = JOptionPane.showInputDialog("How many credit hours are you taking?: "); 

    hours = Integer.parseInt(input); 
    if (hours <=0) 
    JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE); 
} 


else 
     { 
     JOptionPane.showMessageDialog(null, "You must enter a 1, 2, or 3\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE); 
      System.exit(0); 
    } 


tuition = hours * residency; 


if 
    (residency == 1) 
    { 
     JOptionPane.showMessageDialog(null, hours + " hours at $ " + residentTuition + "per hour yields a tuition of " + tuition); 
    } 

else if 
    (residency == 2) 
    { 
     JOptionPane.showMessageDialog(null, hours + " hours at $ " + nonTuition + "per hour yields a tuition of " + tuition); 
    } 

else if 
    (residency == 3) 
    { 
     JOptionPane.showMessageDialog(null, hours + " hours at $ " + internationalTuition + "per hour yields a tuition of " + tuition); 
    } 



     } 



} 

回答

0

如果你想的時間常數,那麼你可以做這樣的事情:

input = JOptionPane.showInputDialog("How many credit hours are you taking?: "); 
final hours = Integer.parseInt(input); 

你也可以把JOptionPane中的if語句,這樣你就不會在重新分配的值頂部每個if語句塊。 同樣,你可以這樣做你的代碼來縮短一切。

import java.util.*; 
import javax.swing.JOptionPane; 

/** 
The application calculates the cost of tuition for one semester at OCC. 
*/ 

public class TuitionOCC { 
    public static void main (String[] args) { 

     String input; 
     int residency; 
     byte credits; 
     int residentTuition; 
     int nonTuition; 
     int internationalTuition; 
     double tuition; 


     residentTuition = 82; 
     nonTuition = 154; 
     internationalTuition = 216; 


     JOptionPane.showMessageDialog(null, "Calculate your tuition!",  "Tuition",JOptionPane.INFORMATION_MESSAGE); 

     input = JOptionPane.showInputDialog("Are you a:\n" + 
     "1- College District Resident\n" + 
     "2- Non-Resident of College District\n" + 
     "3- Out-of-State or International Student");   

     residency = Integer.parseInt(input); 

     input = JOptionPane.showInputDialog("How many credit hours are you taking?: "); 
     final int hours = Integer.parseInt(input); 

     if (hours <= 0) { 
      JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE); 
     } 
     else { 
      tuition = hours * residency; 
      if (residency == 1) { 
       JOptionPane.showMessageDialog(null, hours + " hours at $ " + residentTuition + "per hour yields a tuition of " + tuition); 
      } else if (residency == 2) { 
       JOptionPane.showMessageDialog(null, hours + " hours at $ " + nonTuition + "per hour yields a tuition of " + tuition); 
      } else if (residency == 3) { 
       JOptionPane.showMessageDialog(null, hours + " hours at $ " + internationalTuition + "per hour yields a tuition of " + tuition); 
      } else { 
       JOptionPane.showMessageDialog(null, "You must enter a 1, 2, or 3\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE); 
       System.exit(0); 
      } 
     } 
    } 
} 
相關問題