我有一個學校的任務和我的生活我無法弄清楚爲什麼我的else語句不打印出我需要它的線。如果用戶輸入1,2,3,4或5以外的任何內容,我需要它輸出無效的條目。如果其他語句(初學者)
這是我到目前爲止有:
/**
*Proj2.java
*This project will be able to ask the user which category
they fall in to and then ask how many tickets they want and
if they want parking added.
It will then calculate the total cost for all tickets, with discounts
and parking.
*/
import java.util.Scanner;
import java.text.DecimalFormat;
public class Proj2 {
public static void main(String[] args) {
{ Scanner s = new Scanner (System.in);
final double general = 80;
final double parking = 25;
final double alumniDiscount = .1;
final double facultyDiscount = .15;
final double militaryDiscount = .2;
final double studentDiscount = .5;
//This declares all the constants for this program
int numberOfTickets = 0;
int selection = 0;
double ticketsPrice = 0;
double finalCost = 0;
//declares the variables
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("\n**Welcome to the KSU Homecoming ticketing app for Fall 2014**");
System.out.println("\t -----Show your Purple Pride!-----\n\n\n");
//this displays the header for the customer
System.out.println("Please select from the following categories:\n\n"
+ "1) Student\n"
+ "2) Alumni\n"
+ "3) Faculty & Staff\n"
+ "4) Military\n"
+ "5) General Public\n");
//this list out all the options the customer can choose from
System.out.print("Selection: ");
selection = Integer.parseInt (s.nextLine());
System.out.print("\nPlease enter the number of tickets you would like to purchase: ");
numberOfTickets = Integer.parseInt(s.nextLine());
System.out.print("\nWould you like to purchase a parking pass for the game?\n"
+ "Select Y or N: ");
char parkingChoice= (s.nextLine()).charAt(0);
//questions for the user to input their answer
if (selection == 1) {
ticketsPrice = ((general - (general * studentDiscount)) * numberOfTickets);
}
else if (selection == 2) {
ticketsPrice = ((general - (general * alumniDiscount)) * numberOfTickets);
}
else if (selection == 3) {
ticketsPrice = ((general - (general * facultyDiscount)) * numberOfTickets);
}
else if (selection == 4) {
ticketsPrice = ((general - (general * militaryDiscount)) * numberOfTickets);
}
else if (selection == 5) {
ticketsPrice = general * numberOfTickets;
}
else {
System.out.println("Invalid Category selection");
}
//calculations based on which selection the user chooses
if (parkingChoice == 'Y' || parkingChoice == 'y') {
finalCost = ticketsPrice + parking;
System.out.println("\n\nYou ordered " + numberOfTickets + " tickets" + " with parking for a total cost of: $" + df.format(finalCost)+"\n");
}
else if (parkingChoice == 'N' || parkingChoice == 'n') {
finalCost = ticketsPrice;
System.out.println("\n\nYou ordered " + numberOfTickets + " tickets" + " without parking for a total cost of: $" + df.format(finalCost)+"\n");
}
//whether to add parking or not
System.out.println("Enjoy the game and a Wildcat Victory!");
} // end main
} // end class
}
它符合和適用於數學,這不是我的錯誤信息。任何幫助表示讚賞!
你確定你輸入s爲另一個int(即7),而你沒有得到「無效的類別選擇」 – 2014-09-12 20:51:18
* *代碼*將*顯示「無效」消息(當它進行折扣計算時)if已輸入「6」。檢查它*是*編譯,最新編譯*的結果是*正在運行的是什麼。 – user2864740 2014-09-12 20:51:27
我把6可以得到「無效的類別選擇」 – user1071777 2014-09-12 20:51:38