if else语句(初学者)

时间:2014-09-12 20:47:28

标签: java if-statement

我有一份学校作业,对于我的生活,我无法弄清楚为什么我的其他声明没有打印出我需要它的行。如果用户输入除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

}   

它符合并适用于数学,而不是我的错误信息。任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:2)

你的程序运行正常。我认为问题在于您应该在每次输入时检查输入的有效性,而不是在用户输入所有数据之后检查输入的有效性:

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。感谢Cyber​​neticTwerkGuruOrc注意到这一点。

答案 1 :(得分:0)

如果在要求下一行输入之前验证输入,它是否有效? (即,将输入扫描码与逻辑流一起移动)

答案 2 :(得分:0)

只要您获得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没有被用户分配,直到他/她的解决方案。