Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用方法在Java中进行简单计算_Java_Methods - Fatal编程技术网

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

使用方法在Java中进行简单计算,java,methods,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 S

我找不出我的数学错误在哪里。我就是不明白为什么我的
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;
 }
下面是我调用该方法的方式:

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

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

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