循环保持循环(已更正) - 在不需要时退出程序的重复出现问题

时间:2016-04-19 17:53:48

标签: java

我设法纠正了大多数正在发生的错误。现在唯一不能正常工作的问题是在使用菜单项1并尝试向阵列添加新事件时递增事件编号。当我添加一个项目时,它会添加ok,但它会覆盖已经存在的内容,而不是在最后一个之后获取下一个插槽,然后将事件编号递增一个。

package JavaEvent;

import java.util.Scanner;



import firstPrograms.My;
import firstPrograms.MyDate;
import firstPrograms.MyTime;

public class TestEvent 
{
public static Scanner key = new Scanner(System.in);


public static void main(String[] args)
{
    int noOfEvents = 25;  // number of stipulated events
    int opt = 0;
    boolean answer = false;

    Event [] allEvents = new Event [noOfEvents];

    allEvents[0] = new Event (1000, "West Ham United v Arsenal", "Premiership", new MyDate (9, 03, 2016),  new MyDate (9, 04, 2016), new MyTime (12, 45, 00), "Upton Park", 3500, 3498, 65.00, 0.025);
    allEvents[1] = new Event (1001, "Sunderland v Arsenal", "Premiership", new MyDate (24, 03, 2016),  new MyDate (24, 04, 2016), new MyTime (14, 05, 00), "Stadium of Light", 500, 500, 75.00, 0.05);
    allEvents[2] = new Event (1002, "Arsenal v Crystal Palace", "Premiership", new MyDate (17, 03, 2016),  new MyDate (17, 04, 2016), new MyTime (16, 05, 00), "Emirates Stadium", 100, 98, 125.00, 0.025);
    allEvents[3] = new Event (1003,"Arsenal v West Bromwich Albion", "Premiership", new MyDate (21, 03, 2016),  new MyDate (21, 04, 2016), new MyTime (19, 45, 00), "Emirates Stadium", 125, 72, 95.00, 0.025);
    allEvents[4] = new Event (1004, "Arsenal v Norwich City", "Premiership", new MyDate (30, 03, 2016),  new MyDate (30, 04, 2016), new MyTime (17, 30, 00), "Emirates Stadium", 1250, 1137, 110, 0.025);
    allEvents[5] = new Event (1005, "Manchester City v Arsenal", "Premiership", new MyDate (8, 04, 2016),  new MyDate (8, 05, 2016), new MyTime (16, 05, 00), "Etihad Stadium", 5000, 4796, 75.00, 0.05);
    allEvents[6] = new Event (1006, "Arsenal v Aston Villa", "Premiership", new MyDate (15, 04, 2016),  new MyDate (15, 05, 2016), new MyTime (19, 30, 00), "Emirates Stadium", 1250, 872, 16.00, 0.025);
    allEvents[7] = new Event (1007, "MLS All-Stars v Arsenal", "Friendly", new MyDate (28, 01, 2016),  new MyDate (28, 07, 2016), new MyTime (17, 00, 00), "Avaya Stadium", 500, 391, 75.00, 0.05);
    allEvents[8] = new Event (1008, "Chivas de Guadalajara v Arsenal", "Friendly", new MyDate (31, 01, 2016),  new MyDate (31, 07, 2016), new MyTime (19, 30, 00), "StubHub Center", 1250, 872, 16.00, 0.025);
    allEvents[9] = new Event (1009, "West Ham United v Manchester United", "Cup", new MyDate (13, 03, 2016),  new MyDate (13, 04, 2016), new MyTime (19, 00, 00), "Upton Park", 750, 615, 85.00, 0.075);
    allEvents[10] = new Event (1010, "Everton v Manchester United", "Cup", new MyDate (14, 04, 2016),  new MyDate (23, 04, 2016), new MyTime (17, 15, 00), "Wembley Stadium", 10000, 5000, 150.00, 0.015);
    allEvents[11] = new Event (1011, "Crystal Palace v Watford", "Cup", new MyDate (24, 03, 2016),  new MyDate (24, 04, 2016), new MyTime (16, 00, 00), "Wembley Stadium", 10000, 10000, 150.00, 0.015);
    allEvents[12] = new Event (1012, "FA Cup Final - TBC", "Cup", new MyDate (25, 04, 2016),  new MyDate (21, 05, 2016), new MyTime (15, 00, 00), "Wembley Stadium", 20000, 0, 150.00, 0.015);
    allEvents[13] = new Event (1013, "UEFA Champions League Final - TBC", "Europe", new MyDate (5, 5, 2016),  new MyDate (28, 05, 2016), new MyTime (19, 45, 00), "Stadio Giuseppe Meazza", 25000, 0, 350.00, 0.01);
    allEvents[14] = new Event (1014, "UEFA Europa League Final - TBC", "Europe", new MyDate (6, 05, 2016),  new MyDate (18, 05, 2016), new MyTime (19, 45, 00), "St. Jakob-Park", 20000, 0, 250.00, 0.015);

    do
    {
        Event.mainMenu("Event Menu"); // Calls the main menu
        opt = Event.option(1, 6); // sets the validation of the numbers entered by the user in the menu

        switch (opt)
        {
            case 1:
            {
                    System.out.println("\nAdd Events:");

                    int day = 0, month = 0, year = 0;
                    MyDate tempSaleDate;
                    MyDate tempEventDate;
                    int hour = 0, mins = 0, secs = 0;
                    MyTime tempTime;
                    String keyedAnswer = "";
                    int tempEventNo = 0;
                    //boolean answer = false;


                    for (int i = 0; i < allEvents.length; i++)
                    {       
                        //if (allEvents[i] != null)
                        //{
                            if (tempEventNo == allEvents[i].getEventNo())
                            {
                                tempEventNo += i++;
                            }
                            //else
                            //tempEventNo = allEvents[i].getEventNo()+1;
                            //events[size].setEventNo(events[size-1].getEventNo() + 1);


                            allEvents[i] = new Event();

                            allEvents[i].setEventNo(tempEventNo);

                            //allEvents[i].setEventNo((allEvents[i].getEventNo() + 1));

                            System.out.println("\n\tPlease enter name of event: ");
                            allEvents[i].setEventName(key.nextLine());
                            System.out.println("\tPlease enter the event's category: ");
                            allEvents[i].setCategory(key.nextLine());
                            System.out.println("\tPlease enter event location: ");
                            allEvents[i].setVenue(key.nextLine());
                            System.out.println("\tPlease enter the event's tickets date of sale (ie 1 - 31): ");
                            day = key.nextInt();
                            System.out.println("\tPlease enter the event's tickets month of sale (ie 1 - 12): ");
                            month = key.nextInt();
                            System.out.println("\tPlease enter the event's tickets year of sale (ie 2016): ");
                            year = key.nextInt();
                            tempSaleDate =  new MyDate(day, month, year);
                            allEvents[i].setDateOnSale(tempSaleDate);
                            key.nextLine();
                            System.out.println("\tPlease enter the event's date of event (ie 1 - 31): ");
                            day = key.nextInt();
                            System.out.println("\tPlease enter the event's month of event (ie 1 - 12): ");
                            month = key.nextInt();
                            System.out.println("\tPlease enter the event's year of event (ie 2016): ");
                            year = key.nextInt();
                            tempEventDate = new MyDate(day, month, year);
                            allEvents[i].setEventDate(tempEventDate);
                            key.nextLine();
                            System.out.println("\tPlease enter the event's start hour (ie 1am = 1, 10am = 10, 10pm = 22, etc): ");
                            hour = key.nextInt(); 
                            key.nextLine();
                            System.out.println("\tPlease enter the event's  start minutes (ie = 00, 15, 30, 45, etc): ");
                            mins = key.nextInt();
                            key.nextLine();
                            tempTime = new MyTime(hour, mins, secs);
                            allEvents[i].setEventTime(tempTime);
                            System.out.println("\tPlease enter the event's ticket allocation: ");
                            allEvents[i].setTicketsReceived(key.nextInt());
                            key.nextLine();
                            System.out.println("\tPlease enter the amount of tickets sold for the event: ");                    
                            allEvents[i].setTicketsSold(key.nextInt());
                            key.nextLine();
                            System.out.println("\tPlease enter the event's ticket cost: £");
                            allEvents[i].setTicketCost(key.nextDouble());
                            key.nextLine();
                            System.out.println("\tPlease enter the event's ticket commission rate: ");
                            allEvents[i].setCommissionRate(key.nextDouble());
                            key.nextLine();
                            System.out.println("\n\t\tWould you like to add another event (Y / N): ");
                            keyedAnswer = key.nextLine();

                            if (keyedAnswer.equalsIgnoreCase("n"))
                            {
                                answer = false;
                                break;
                            //continue;
                            }
                            else
                            {
                                answer = true;
                            //continue;
                            }
                        //}
                }

            }
            break;


            case 2: 
            {
                Event.headersEventNo();
                for (int i = 0; i < allEvents.length; i++)
                {
                    if (allEvents[i] != null)
                    {
                        System.out.println(allEvents[i].toString());
                    }
                }
            }
            break;

            case 3:
            {       
                do
                {
                    Event.searchMenu("Search Menu"); // Calls the search menu
                    opt = Event.option(1, 4); // sets the validation of the numbers entered by the user in the menu

                    int tempEventNo = 0;
                    int day = 0, month = 0, year = 0;
                    MyDate tempEventDate;
                    String tempEventCat = "";

                    switch (opt)
                    {
                        case 1:
                        {
                            System.out.println("Please enter the event no: ");
                            tempEventNo = key.nextInt();
                            key.nextLine();

                            for (int i = 0; i < allEvents.length; i++)
                            {
                                if (allEvents[i] != null)
                                {
                                    if (tempEventNo != allEvents[i].getEventNo())
                                    {
                                        i++;
                                    }
                                    else if (tempEventNo == allEvents[i].getEventNo())
                                    {
                                        System.out.println("\t" + allEvents[i]);//break;
                                    }
                                }
                            }
                        }
                        break;

                        case 2:
                        {
                            System.out.println("\tPlease enter the event's date of event (ie 1 - 31): ");
                            day = key.nextInt();
                            System.out.println("\tPlease enter the event's month of event (ie 1 - 12): ");
                            month = key.nextInt();
                            System.out.println("\tPlease enter the event's year of event (ie 2016): ");
                            year = key.nextInt();
                            tempEventDate = new MyDate(day, month, year);
                            key.nextLine();

                            for (int i = 0; i < allEvents.length; i++)
                            {
                                if (allEvents[i] != null)
                                {
                                    if (!tempEventDate.toShortDate().equals(allEvents[i].eventDate.toShortDate()))
                                    {
                                        i++;
                                        //System.out.println("\t\t" + tempEventDate.toShortDate());
                                        //System.out.println("\t\t" + allEvents[i]);
                                        //break;
                                    }
                                    else
                                    {
                                        //System.out.println("\t\tNo Events On This Date - " + tempEventDate.toShortDate());
                                        System.out.println("\t\t" + allEvents[i]);
                                        //break;
                                        continue;
                                    }
                                }
                            }
                        }
                        break;

                        case 3:
                        {
                            System.out.println("Please enter the event category: ");
                            tempEventCat = key.nextLine();

                            for (int i = 0; i < allEvents.length; i++)
                            {
                                if (allEvents[i] != null)
                                {
                                //if (tempEventCat != allEvents[i].getCategory())
                                    if (!tempEventCat.equals(allEvents[i].getCategory()))
                                {
                                    i++;
                                    System.out.println("\t" + allEvents[i]);
                                }
                                else //if (tempEventCat.equals(allEvents[i].getCategory())}
                                {
                                    //i++;


                                    System.out.println("\t" + allEvents[i]);
                                    //break;
                                }
                                }
                            }
                        }
                        break;

                        case 4:

                        break;
                    }
                }while (opt != 4);
            }
            break;

            case 4:
            {
                Event.headersEventIncome();

                for (int i = 0; i < allEvents.length; i++)
                {
                    if (allEvents[i] != null)
                    {
                        System.out.printf("%-9s%-16d%-36s%s%-10.2f%s%.2f", "\n", allEvents[i].getEventNo(), allEvents[i].getEventName(), "\t\t£", allEvents[i].getActualIncome(), "\t\t£", allEvents[i].getPotentialIncome());
                    }
                }

            }
            break;

            case 5:
            {
                double tempPremActualIncome = 0.00, totalPremActTemp = 0.00;
                double tempPremPotentialIncome = 0.00, totalPremPotTemp = 0.00;
                double percentagePremAchieved = 0.00;
                double tempFriendlyActualIncome = 0.00, totalFriendlyActTemp = 0.00;
                double tempFriendlyPotentialIncome = 0.00, totalFriendlyPotTemp = 0.00;
                double percentageFriendlyAchieved = 0.00;
                double tempCupActualIncome = 0.00, totalCupActTemp = 0.00;
                double tempCupPotentialIncome = 0.00, totalCupPotTemp = 0.00;
                double percentageCupAchieved = 0.00;
                double tempEuroActualIncome = 0.00, totalEuroActTemp = 0.00;
                double tempEuroPotentialIncome = 0.00, totalEuroPotTemp = 0.00;
                double percentageEuroAchieved = 0.00;
                String tempCategory ="";

                System.out.println("\n\nIncome Per Category:");
                Event.headersCategoryIncome();

                for (int i = 0; i < allEvents.length; i++)
                {
                    if(allEvents[i] != null)
                    {
                        if("Premiership".equals(allEvents[i].getCategory()))
                        {
                            tempPremActualIncome = allEvents[i].getActualIncome();
                            totalPremActTemp += tempPremActualIncome;
                            tempPremPotentialIncome = allEvents[i].getPotentialIncome();
                            totalPremPotTemp += tempPremPotentialIncome;
                            percentagePremAchieved = ((totalPremActTemp / totalPremPotTemp) * 100);
                            tempCategory = "Premiership";
                        }
                    }
                }

                    System.out.printf("%s%s%s%.2f%s%.2f%s%.2f", "\n\t", tempCategory, "\t\t\t\t£", totalPremActTemp, "\t\t\t\t£", totalPremPotTemp, "\t\t\t\t", percentagePremAchieved);

                for (int i = 0; i < allEvents.length; i++)
                    {
                        if(allEvents[i] != null)
                        {
                            if ("Cup".equals(allEvents[i].getCategory()))
                            {
                                tempCupActualIncome = allEvents[i].getActualIncome();
                                totalCupActTemp += tempCupActualIncome;
                                tempCupPotentialIncome = allEvents[i].getPotentialIncome();
                                totalCupPotTemp += tempCupPotentialIncome;
                                percentageCupAchieved = ((totalCupActTemp / totalCupPotTemp) * 100);
                                tempCategory = "Cup";
                            }
                        }
                    }
                System.out.printf("%s%s%s%.2f%s%.2f%s%.2f", "\n\t", tempCategory, "\t\t\t\t£", totalCupActTemp, "\t\t\t\t£", totalCupPotTemp, "\t\t\t\t", percentageCupAchieved);

                    for (int i = 0; i < allEvents.length; i++)
                    {
                        if(allEvents[i] != null)
                            if ("Friendly".equals(allEvents[i].getCategory()))
                        {
                            tempFriendlyActualIncome = allEvents[i].getActualIncome();
                            totalFriendlyActTemp += tempFriendlyActualIncome;
                            tempFriendlyPotentialIncome = allEvents[i].getPotentialIncome();
                            totalFriendlyPotTemp += tempFriendlyPotentialIncome;
                            percentageFriendlyAchieved = ((totalFriendlyActTemp / totalFriendlyPotTemp) * 100);
                            tempCategory = "Friendly";
                        }
                    }
                    System.out.printf("%s%s%s%.2f%s%.2f%s%.2f", "\n\t", tempCategory, "\t\t\t\t£", totalFriendlyActTemp, "\t\t\t\t£", totalFriendlyPotTemp, "\t\t\t\t", percentageFriendlyAchieved);

                    for (int i = 0; i < allEvents.length; i++)
                    {
                        if(allEvents[i] != null)
                            if ("Europe".equals(allEvents[i].getCategory()))
                        {
                                tempEuroActualIncome = allEvents[i].getActualIncome();
                                totalEuroActTemp += tempEuroActualIncome;
                                tempEuroPotentialIncome = allEvents[i].getPotentialIncome();
                                totalEuroPotTemp += tempEuroPotentialIncome;
                                percentageEuroAchieved = ((totalEuroActTemp / totalEuroPotTemp) * 100);
                                tempCategory = "Europe";
                        }

                    }
                    System.out.printf("%s%s%s%.2f%s%.2f%s%.2f", "\n\t", tempCategory, "\t\t\t\t£", totalEuroActTemp, "\t\t\t\t£", totalEuroPotTemp, "\t\t\t\t", percentageEuroAchieved);
            }
            break;


            case 6:
            {
                My.p("\n\n\t\tGoodbye");
            }
            break;
        }

    }while (opt != 6);
}               

}

3 个答案:

答案 0 :(得分:0)

以上评论对你的循环行为是正确的,你对allEvents没有做任何事情,所以检查一些条件和使用循环跳转语句,

int i = 0;
looper: //Jump Statements
while(true){
     if(++i > 10)
          break looper;
     System.out.println("Value of i is : " + i);
}

答案 1 :(得分:0)

AllEvents永远不会为null(即使删除了它的所有元素也不行)。 Null表示未分配,不是空的。

删除外部do..while循环,因为它没有任何功能,并且有效地创建了您报告的无限循环:)

(从长远来看,考虑使用List而不是未完全填充的数组。列表更容易使用,也更安全。)

答案 2 :(得分:0)

我会这样做......

case 2:
    // You may not even require this check, if it's ensured to be non-null.
    if (allEvents != null) {
        Event.headersEventNo();
        for (int i = 0; i < allEvents.length; i++)
        {
            if (allEvents[i] != null)
            {
                System.out.println(allEvents[i].toString());
            }
        }
    }

    break;

您可能最好在此处实例化List,并简单地循环其内容。这样,您可以避免不必要的空检查。