-3

//我的问题是在方法 cancelledOrder 中没有看到字符串 [] waitingList[] 至少我是这么认为的。

public static String[] canceledOrder(String[] waitingList,String[] waitingList1,String []waitingList2,String[] waitingList3){//I've decided to pass these string [] hoping the string from the other methods will now be seen in canceledOrder();
            Scanner in = new Scanner (System.in);
            int option;
            System.out.println("Select the event you want to cancel :\n");
            events();
            option= in.nextInt();
            in.nextLine();
            System.out.println("Person on wait list is " + waitingList[name] );
            switch (option){

            case 1:
                System.out.println("Please enter your name:\n");
                canceledname = in.nextLine();
                System.out.println("name:" + canceledname);



                for (String s : myStringList) {
                    if(s.equals(canceledname)){

                        s = waitingList[name];

                        System.out.println("The new name is\n" + s);

                        name++;
                    }
                    return s; // I want it to now return waitingList[name]
                }

                break;
4

3 回答 3

0

我猜你想知道是否canceledname存在于 中myStringList,因为你必须遍历myStringList它 - 没有办法绕过它。

for(String name : myStringList){
    if(name.equals(canceledname){
          // continue...
          k = waitingList[name];
          System.out.println("The new name is" + k);
          i++;
          name++;
    }
}

对于这种比较,最好将名称保存在Set例如:

HashSet<String> names = new HashSet<String>();
// here you'll add the names to the hash-set
// using: names.add("whatever");
// ...

//and now:
if(names.get(canceledname) != null){
      // continue ...
}
于 2013-10-20T07:08:19.960 回答
0

我可能是错的,但是您是否想遍历 arraylist 以查看列表中是否有任何值相等cancelledName

如果是这样,你可能想要这样的东西

for (String s : myStringList) {
    if(s.equals(canceledname)){

        k = waitingList[name];

        System.out.println("The new name is" + k);

    name++;
    }
}

编辑:可能解决问题

// declare array of Attendees, capacity is 20
// I would rather use ArrayList, but it seems you're trying to use array
String attendees[] = new String[20];

// delcare wait list
String waitList[] = new String[2];

// declare int for tickets sold
int ticketsSold = 0;
// declare waitlist
int waitlistCount = 0;

// prompt buyers for purchase of tickets
// ticketsSold = ticketsSold +  1 or 2 depending on tickets
// if ticketsSold is 20 do not sell tickets and send to waiting list
// Do this until full

// if ticketsSold = 20
// prompt user to go on wait list
// if yes, ask how many tickets
// if tickets requested for waitlist exceeds current waitlist + tickets requested
       // System.out.println("Sorry, waitlist is full");
// else prompt for user name
String name = in.nextLine();
waitlist[waitListCount] = name;

// here's the part I think you're having problems with
// if person is deleted from attendees
// get name of person not attending
int i = 0;
while (!cancelledName != attendees[i]) {
    i++
}

// replace attendees index with waitlist Name
attendees[i] = waitlist[0];

// move the waitlist forward
waitlist[0] = waitlist[1];
waitlist[1] = null;

这是我能想到的最好的逻辑。如果您的逻辑合理并且看起来与我的相似,请关注底部,我认为您遇到的问题最多

于 2013-10-20T07:09:00.937 回答
0

您不能将 ArrayList 与 String 进行比较,而是使用contains方法来查找 ArrayList 中是否存在元素。

boolean isElementExists = myStringList.contains(canceledname);

if(isElementExists) {
   // other codes here
}

请参阅 API:ArrayList#contains()

于 2013-10-20T07:09:17.833 回答