2014-09-12 228 views
-2

我有一個學校的任務和我的生活我無法弄清楚爲什麼我的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 

} 

它符合和適用於數學,這不是我的錯誤信息。任何幫助表示讚賞!

+1

你確定你輸入s爲另一個int(即7),而你沒有得到「無效的類別選擇」 – 2014-09-12 20:51:18

+0

* *代碼*將*顯示「無效」消息(當它進行折扣計算時)if已輸入「6」。檢查它*是*編譯,最新編譯*的結果是*正在運行的是什麼。 – user2864740 2014-09-12 20:51:27

+0

我把6可以得到「無效的類別選擇」 – user1071777 2014-09-12 20:51:38

回答

0

如果在請求下一行輸入之前驗證輸入,它是否工作? (即將輸入掃描碼與邏輯流程內聯地移動)

+0

請在評論部分發表評論。 – 2014-09-12 20:55:02

+0

如果你考慮我的評論,我建議的答案應該是顯而易見的 – khriskooper 2014-09-12 20:56:37

+0

@khriskooper不要讓問題聽起來像評論。 *自信地將它作爲回答與陳述*;提供代碼更改/示例也有幫助。 (請注意接收與現在的投票答案的區別,但OP的陳述仍不明確,很可能是他正在運行舊版本。) – user2864740 2014-09-12 21:00:21

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注意到這一點。

+0

我認爲這是正確的方法 – khriskooper 2014-09-12 20:59:02

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 ISN」 t由用戶指定,直到後來在他/她的解決方案中。

+0

默認情況下不是需要,因爲你有如果檢查一下! – StackFlowed 2014-09-12 21:10:58

+0

@Aeshang我知道。我只是把它作爲原始代碼的反映。它使OP更容易遵循。 – 2014-09-12 21:11:33