有人可以告诉我为什么在方法B中输入20到25之间的任何数字时我的代码会终止吗?方法A可以正常工作,但问题是我还想输入如果他们选择其他软件包,可以节省多少。这是我的第二个Java类,但我不太喜欢它,如果它看起来不好,请原谅。
import java.util.Scanner;
public class Assingment13 {
public static double packageA = 9.95; // Package A price for a monthly subscription and before exceeding 10 hours
// of service
public static double packageAJ = 2.00; // price for every hour pass 10 for package A
public static double packageB = 13.95;// Package B price for a monthly subscription and before exceeding 20 hours
// of service
public static double packageBJ = 1.00; // price for every hour pass 20 for package B
public static double packageC = 19.95;
public static Scanner input = new Scanner(System.in); // declares scanner as "Input"
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out
.println("Welcome to your internet payment site\nPlease choose your packet :\n" + "[Enter -1 to quit]"); // Welcoming
// prompt
System.out.printf("%s%n%s%n%s%n%s%n%s", "[a] - Package A", "[b] - Package B", "[c] - Package C",
"[f] - enter f to end the program", "Enter response here: "); // print format for the package options
String userChoice = input.nextLine(); /// the choice that the user picks gets stored in the userChoice String
/*
* this switch statement compares the choice of the user and associates it with
* the correct case. Note that there is two cases with the same choice letter
* "A" & "a", "B" "b" and so on. This is to ensure that no matter if the user
* inputs an Upper-case or Lower-case as their answer, there wont be any issues
* and the correct case would be run by the compiler
*/
switch (userChoice) {
case "A":
myFunctionA(); // Performs the function corresponding to package A
// compareFunction(userChoice, packageA);
break;
case "a":
myFunctionA(); // Performs the function corresponding to package A
// compareFunction(userChoice, packageA);
break;
case "B":
myFunctionB(); // Performs the function corresponding to Package B
// compareFunction(userChoice, packageA);
break;
case "b":
myFunctionB(); // Performs the function corresponding to Package B
// compareFunction(userChoice, packageA);
break;
case "C":
myFunctionC(); // Performs the function corresponding to Package C
break;
case "c":
myFunctionC(); // Performs the function corresponding to Package c
case "F":
System.exit(0); // ends the program .. just like with using a sentinel value
break;
case "f":
System.exit(0); // ends the program .. just like with using a sentinel value
}
}
/*
* myfunctionA and myFunctionB hold two if statements inside their bodies. One
* of the if statements is perform only when the user has not exceeded their
* service time. The second If statement is only perform if the user exceeds his
* use time.
*/
public static void myFunctionA() {
System.out.println("Enter ammount of hours use of service: "); // Prompts the user
double userInput = input.nextDouble(); // saves the user input into the variable userInput
double Hours = userInput; // the number of hours the user, used the service for gets stored here
double extendedHours = 0; // this variable is where the extended hours get store. for example, every hour
// after 10 hours of service gets store here
double regularHours = 10; // this variable holds the value of regular hours of service
if (Hours <= 10) {
System.out.println("Your total amount due is " + packageA);
}
else if (Hours > 10)
{
extendedHours = Hours - regularHours; // obtains amount of Extra hours use by the client.
double extendedHours2 = extendedHours * 2; // calculates the total due after multiplying the extended hours
// by 2
double totalAmountDue = extendedHours2 + packageA; // calculates the total amount due
System.out.println("Since you Extended your use time by " + extendedHours + " Your total amount due is "
+ totalAmountDue);
}
}
public static void myFunctionB() {
System.out.println("Enter ammount of hours use of service: ");
double userInput = input.nextDouble();
double extendedHours = 0;
double regularHours = 20;
double Hours = userInput;
if (Hours < 20) {
System.out.println("Your total amount due is " + packageB);
} else if (Hours > 26) {
extendedHours = Hours - regularHours; // obtains amount of Extra hours use by the client.
double extendedHours2 = extendedHours * 1; // calculates the total due after multiplying the extended hours
// by 1
double totalAmountDue = extendedHours + packageB; // calculates the total amount due
double totalSave1 = totalAmountDue - packageC;
System.out.println("Since you Extended your use time by " + extendedHours + " Your total ammount due is "
+ totalAmountDue);
System.out.println(
"With an internet use of 26 or more hours of service, Package C would be a better plan in helping you save "
+ totalSave1 + " a month");
}
}
/*
* myFunctionC outputs the price of regular monthly use.
*/
public static void myFunctionC() {
System.out.println("Your ammount due for the month is: " + packageC);
}
}