使用方法在Java中进行简单计算

时间:2014-11-23 00:44:02

标签: java methods

我无法找出数学错误的位置。我只是看不出为什么我的determineCableSrvs方法继续没有收到任何值。

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);

1 个答案:

答案 0 :(得分:2)

您将subscription设置为0并且在将其传递给determineCableSrv方法之前从不更改它。您对cableSrvs执行相同的操作,因此方法的返回值将为0. cableSrvs变量未在方法内部读取,因此它可能不应该在输入参数中从...开始。