如果其他语句(初学者)
我有一个学校的任务和我的生活我无法弄清楚为什么我的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
}
它符合和适用于数学,这不是我的错误信息。任何帮助表示赞赏!
如果在请求下一行输入之前验证输入,它是否工作? (即将输入扫描码与逻辑流程内联地移动)
请在评论部分发表评论。 – 2014-09-12 20:55:02
如果你考虑我的评论,我建议的答案应该是显而易见的 – khriskooper 2014-09-12 20:56:37
@khriskooper不要让问题听起来像评论。 *自信地将它作为回答与陈述*;提供代码更改/示例也有帮助。 (请注意接收与现在的投票答案的区别,但OP的陈述仍不明确,很可能是他正在运行旧版本。) – user2864740 2014-09-12 21:00:21
你的程序工作得很好。我认为这个问题是你要每次输入和时间检查输入的有效性不是之后的所有数据由用户已经输入了:
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("\nPlease enter the number of tickets you would like to purchase: ");
numberOfTickets = Integer.parseInt(s.nextLine());
if (numberOfTickets < 0)
{
System.out.println("Invalid number of tickets");
return;
}
System.out.print("Selection: ");
selection = Integer.parseInt(s.nextLine());
// 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");
return;
}
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);
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");
}
else
{
System.out.println("Invalid parking choice");
return;
}
// whether to add parking or not
System.out.println("Enjoy the game and a Wildcat Victory!");
}
PS:这将是这样一些switch
更好的多声明:)
编辑:更改代码先设置numberOfTickets
。感谢CyberneticTwerkGuruOrc注意到这一点。
我认为这是正确的方法 – khriskooper 2014-09-12 20:59:02
只是做一个简单的检查,只要你得到selection
输入:
//beginning code goes here
System.out.print("Selection: ");
selection = Integer.parseInt (s.nextLine());
//simple check
if(selection < 1 || selection > 5){
System.out.println("Invalid Category selection");
}else{
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);
switch(selection){
case 1:
ticketsPrice = ((general - (general * studentDiscount)) * numberOfTickets);
break;
case 2:
ticketsPrice = ((general - (general * alumniDiscount)) * numberOfTickets);
break;
case 3:
ticketsPrice = ((general - (general * facultyDiscount)) * numberOfTickets);
break;
case 4:
ticketsPrice = ((general - (general * militaryDiscount)) * numberOfTickets);
break;
case 5:
ticketsPrice = general * numberOfTickets;
break;
default://this case is not needed. i kept it in so its easier to understand.
System.out.println("Invalid Category selection");
break;
}
//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!");
}
//ending code goes here.
注意:@ortis原来的答案将无法正常工作(虽然它得到了upvotes:/),因为numberOfTickets
ISN” t由用户指定,直到后来在他/她的解决方案中。
默认情况下不是需要,因为你有如果检查一下! – StackFlowed 2014-09-12 21:10:58
@Aeshang我知道。我只是把它作为原始代码的反映。它使OP更容易遵循。 – 2014-09-12 21:11:33
你确定你输入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