我有一个学校的任务和我的生活我无法弄清楚为什么我的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