有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

io Java:使用文本文件

我的主屏幕会这样加载:

Press 1 to find Artist List

Press 2 to Book Event

Press 3 to Exit Program

Menu 1: Artist List 2: Book Ticket 3: Exit Please select an option: 1 - 3

选项1显示了我存储在文本文件中的艺术家列表,这很好。但选项2是问题所在,当我选择选项2时,它显示如下:

Menu 1: Artist List 2: Book Ticket 3: Exit Please select an option: 1 - 3

2

Prompt Message: Please be advised that maximum tickets allowed to be purchased is 6.

Input Artist: beyonce

Number of Tickets required: 2

Input Customer Details

我的代码是:

案例2://预订场地

    String chosenArtist = " ";


    int availability = 0;

    int capacity = 0;  //for the calculation of capacity that will be left

    //is the text file that customer details are stored too
        String file_name = "/Users/petercanning/Desktop/customerFile.txt";

    WriteFile data = new WriteFile(file_name, true);

    //the prompt message displays when option 2 is selected
    System.out.println("\n" + "Prompt Message: ");
    System.out.println("Please be advised that maximum tickets allowed to be purchased is 6." + "\n");

    //input the artist name
    System.out.println("\n" + "Input Artist: ");
    chosenArtist = sc.nextLine();
    sc.nextLine();



    //error handling, if ticket is above or below number of tickets allowed
    while(numberTickets < 1 || numberTickets > 6){

    //input the number of tickets required
    System.out.println("\n" + "Number of Tickets required: ");
    numberTickets = sc.nextInt();

    if(numberTickets > 6)
    {
    //this error is displayed if the number of tickets selected is above 6
    System.out.println("Error: You have input the incorrect number of tickets allowed.");

    System.out.println("Number of Tickets required: ");
    numberTickets = sc.nextInt();
    }
    if(numberTickets < 1)
    {
    //this error is displayed if the number of tickets selected is below 1
    System.out.println("Error: You have input the incorrect number of tickets allowed.");

    System.out.println("Number of Tickets required: ");
    numberTickets = sc.nextInt();

    }

    }


    for (Artist A : artistList)

    //this is used to get the ticket price and times it by the number of tickets
    if (A.getArtist().equals(chosenArtist))
    {

    System.out.println("Total Price including fees: ��" + (A.getTicketPrice() * numberTickets + answer));
    }

    System.out.println("\n" + "Input Customer Details");

    //this is the input of customer details that will be saved to the customer file
    System.out.print("\n" + "First Name: ");
    firstName = sc.next();
    System.out.println("\n" + "Surname: ");
    secondName = sc.next();
    System.out.println("\n" + "Address: ");
    sc.nextLine();
    address = sc.nextLine();
    System.out.println("\n" + "Paymet Method: Visa/American Express/Mastercard");
    sc.nextLine();
    System.out.println("\n" + "Card Number: ");
    cardNumber = sc.nextLong();
    sc.nextLine();

    System.out.println("Payment Transaction complete ");

    System.out.println("First Name: " + firstName + "\nSurname: " + secondName + "\nAddress: " + address);

    data.writeToFile(artistList + " " + numberTickets + " " + firstName + " " + secondName + " " + address);


    System.out.println("Payment Successful");

    System.out.println("Customer booking saved!");

    System.out.print("Which artist do you wish to update - Enter Artist");
    chosenArtist = sc.next();

    for(Artist A : artistList)
    {
    if(A.getArtist().equals(chosenArtist)){
    System.out.println("Artist " + chosenArtist + " found");

    availability = (A.getAvailability());

    capacity = 1;
    }
    }

    if(capacity == 0){
    System.out.println("The person does not exist");
    }


    break;

由于某种原因,在我输入车票数量后,程序忽略了将车票数量与车票价格相加的部分,我无法理解为什么会这样做,也无法理解容量的更新是如何不起作用的

我的艺术家文本文件也如下所示:

beyonce hydro 44.40 1200

dan glasgow 23 120

BonJovi SECC 82 5000


共 (2) 个答案

  1. # 1 楼答案

    对于您所说的,看起来您的artistList是空的,但是您应该调试您的程序以确保它是空的。如果您对此有任何问题,请告诉我们您的编程环境是什么(命令行、eclipse等),然后让我们寻找解决方案

  2. # 2 楼答案

    所以这个代码不起作用了

    for (Artist A : artistList)
    
    //this is used to get the ticket price and times it by the number of tickets
    if (A.getArtist().equals(chosenArtist))
    {
    
    System.out.println("Total Price including fees: ��" + (A.getTicketPrice() * numberTickets + answer));
    }
    

    显然你的“如果”条件没有得到满足。我想说的是,你的文本文件有点不正确,或者你从文本文件中解析艺术家的名字并不是在解析你认为它是什么

    检查解析器