2014-11-23 56 views
1

我找不到我的數學錯誤在哪裏。我只是不明白爲什麼我繼續沒有收到我的determineCableSrvs方法的值。使用方法在Java中進行簡單計算

private static Scanner input = new Scanner(System.in); //STORES CUSTOMER NAME STRING VALUE 

public static void main(String[] args) 
{//BEGIN main() 

//Variables 

int subscription = 0; //STORES SUBSCRIPTION INTEGER VALUE 
int moviesOnDemand = 0; //STORES MOVIES ON DEMAND INTEGER VALUE 
double cableSrvs = 0; //STORES TOTAL CABLE SERVICE DECIMAL VALUE 
double totalMovieCharges = 0; //STORES TOTAL MOVIE CHARGES DECIMAL VALUE 
double total = 0; //STORES TOTAL DECIMAL VALUE 
char confirm = 'Y'; //LOOP CONTROL VARIABLE SET TO Y 
Calendar dateTime = Calendar.getInstance(); //STORES THE DATE VALUE 
String customerName = ""; 


while(Character.toUpperCase(confirm) == 'Y') //START OF WHILE LOOP AUTOMATICALLY ENTERS 
{ 
String customer = setCustNm(customerName); 


double cable = setCablePkg(subscription); 

double type = determineCableSrv(subscription, cableSrvs); 

int movies = setMoviePurchase(moviesOnDemand); 

totalMovieCharges = movies * 7; //CALCULATING THE TOTAL MOVIE ON DEMAND CHARGE  
total = totalMovieCharges + type; //CALCULATING THE OVERALL TOTAL 




    System.out.printf("%n%s %tD" 
         + "\nCustomer: %S" 
         + "%n%nCable Service: $%,21.2f" 
         + "%nMovies-On-Demand-HD: %,20.2f" 
         + "\n\nTOTAL DUE: $%,21.2f\n", 
        "SA CABLE CHARGES AS OF", dateTime, customer, type, 
        totalMovieCharges, total); 

    input.nextLine(); 


    askNewSubscription(confirm); 

    printThankYou(confirm); 

} 
} 

public static String setCustNm(String customerName) 

{ 

// 1st prompt 
System.out.printf("%n%n%nWELCOME TO SA CABLE %n"); 
System.out.printf("%nPlease enter your name: "); //PROMPTING USER TO ENTER NAME 
customerName = input.nextLine(); //CAPTURES USERS NAME 

return customerName; 
}   

public static int setCablePkg(int subscription) 
{ 
do{ 
// 2nd Prompt DISPLAYING THE DIFFERENT PACKAGES AVAILABLE FOR SUBSCRIPTION    
System.out.printf("%nSA CABLE - SUBSCRIPTION PACKAGES " + 
        "%n%n1. Basic: Local & major TV network channels       %s"  + 
        "%n2. Deluxe: Local, major TV, cable & 100 other channels     %s" + 
        "%n3. Premium: Deluxe package plus HEB, on-demand & 300 other channels %s", 
        "$35.00", "75.00", "110.00") ; 

    System.out.printf("%n%nSelect your cable subscription package: "); 
    subscription = input.nextInt();//CAPTURING USER INPUT 

    }while (subscription < 1 || subscription > 3); 

    return subscription; 
    } 

public static double determineCableSrv(int subscription, double cableSrvs) 
{ 

if(subscription == 1) // IF STATEMENT TO IDENTIFY THE PRICE OF THE TOTAL CABLE SERVICE    
{ 
    cableSrvs = 35; 
} 
else if(subscription == 2) 
{ 
    cableSrvs = 75; 
} 
else if(subscription == 3) 
{ 
    cableSrvs = 110; 
} 
return cableSrvs; 

}  



public static int setMoviePurchase(int moviesOnDemand) 
{ 
System.out.printf("%nSA CABLE - MOVIES " + 
        "%n%nEnter the number of Movies-On-Demand-HD purchases: "); 
moviesOnDemand = input.nextInt(); 
return moviesOnDemand; 
} 


public static char askNewSubscription(char confirm) 
{ 
System.out.printf("\nEnter 'Y' to continue with a new subscription or 'N' to exit: "); 
confirm = input.nextLine().charAt(0); 

return confirm; 

} 


public static char printThankYou(char confirm) 
{ 

if(Character.toUpperCase(confirm) == 'Y') 
{ 
    confirm = 'Y'; 
} 
if (Character.toUpperCase(confirm) != 'Y') 
{ 
    confirm = 'N'; 
    System.out.printf("Thank you for being a valued SA Cable customer!"); 

    System.exit(0); 
    } 

return confirm; 
} 

我在與這部分的麻煩:

public static double determineCableSrv(int subscription, double cableSrvs) 
{ 

if(subscription == 1) // IF STATEMENT TO IDENTIFY THE PRICE OF THE TOTAL CABLE SERVICE    
{ 
    cableSrvs = 35; 
} 
else if(subscription == 2) 
{ 
    cableSrvs = 75; 
} 
else if(subscription == 3) 
{ 
    cableSrvs = 110; 
} 
return cableSrvs; 

}  

這裏是我如何調用該方法:

double type = determineCableSrv(subscription, cableSrvs); 

我似乎無法得到的返回值。我需要的值:

total = totalMovieCharges + type; //CALCULATING THE OVERALL TOTAL 

這種打印出來聲明:

System.out.printf("%n%s %tD" 
         + "\nCustomer: %S" 
         + "%n%nCable Service: $%,21.2f" 
         + "%nMovies-On-Demand-HD: %,20.2f" 
         + "\n\nTOTAL DUE: $%,21.2f\n", 
        "SA CABLE CHARGES AS OF", dateTime, customer, type, 
        totalMovieCharges, total); 

回答

2

您設置subscription等於0並將其傳遞到determineCableSrv方法從未改變。你對cableSrvs做了同樣的處理,所以你方法的返回值將變爲0. cableSrvs變量不在方法內部讀取,所以它可能不應該以輸入參數開頭。