0

所以我必须写一段代码来管理一个事件。有4个活动座位有限。我应该为每个编写字符串数组。然后程序会将名称添加到相关字符串中。我的问题是我不知道如何在不删除先前值的情况下使用循环继续向字符串数组添加值。任何帮助将不胜感激。

import java.util.Scanner;


public class Assignment_1 {

public static void main(String[] args) {


    String [] Hockey_Game;
    Hockey_Game = new String[10];

    String [] Turner_Concert;
    Turner_Concert = new String [5];

    String [] Cats_Play; 
    Cats_Play = new String [3];


    String [] StarTrek_Convention;
    StarTrek_Convention = new String [3];

    System.out.println("Which Event would you like to purchase a ticket for?");
    System.out.println("1. Hockey Game 2. Tina Turner Concert \n"
                        + "3. Cats Play 4. Star Trek Convention");
    Scanner keyboard = new Scanner(System.in);
    int input = keyboard.nextInt();
    System.out.println("Please enter your first and last name");
    Scanner scan = new Scanner(System.in);
    String name = scan.nextLine();




    for (int i = 0; i < Hockey_Game.length; i++){
        Hockey_Game[i] = name; 
    } 


        for (String x: Hockey_Game ){
         System.out.print(x +",");
    }
4

4 回答 4

1

这应该做你正在寻找的......

import java.util.Scanner;

public class Assignment_1 {

public static void main(String[] args) {

    String[] Hockey_Game;
    int numHockey = 0;
    Hockey_Game = new String[10];

    String[] Turner_Concert;
    int numConcert = 0;
    Turner_Concert = new String[5];

    String[] Cats_Play;
    int numPlay = 0;
    Cats_Play = new String[3];

    String[] StarTrek_Convention;
    int numCon = 0;
    StarTrek_Convention = new String[3];
    for (int user = 0; user < 1; user++) {
        System.out
                .println("Which Event would you like to purchase a ticket for?");
        System.out.println("1. Hockey Game 2. Tina Turner Concert \n"
                + "3. Cats Play 4. Star Trek Convention");
        Scanner keyboard = new Scanner(System.in);
        int input = keyboard.nextInt();
        System.out.println("Please enter your first and last name");
        Scanner scan = new Scanner(System.in);
        String name = scan.nextLine();

        switch (input) {
        case 1:
            if (numHockey < Hockey_Game.length) {
                Hockey_Game[numHockey] = name;
            }
            numHockey++;
            for (int j = 0; j < numHockey; j++) {
                System.out.print(Hockey_Game[j] + ",");
            }
            break;
        case 2:
            if (numConcert < Turner_Concert.length) {
                Turner_Concert[numConcert] = name;
            }
            numConcert++;
            for (int j = 0; j < numConcert; j++) {
                System.out.print(Turner_Concert[j] + ",");
            }
            break;
        // ... continue for last two ...
        }
    }
}
}

我同意其他回答者的观点,即您应该为此使用 List/ArrayList,但如果分配的目的是使用数组,那么您可以这样做。

于 2013-10-21T22:25:31.980 回答
0

不要使用 String[] 使用 List

List<String> event1 = new List<String>();
. . .
event1.add(name);

还要定义一个 int[] (这一次一个数组就足够了),它为每个事件保存最大的插槽,这样当客户要求在一个已经满员的事件上找到一个位置时,你可以给他一个坏消息。

于 2013-10-21T22:15:49.727 回答
0

我的问题是我不知道如何在不删除先前值的情况下使用循环继续向字符串数组添加值。

给定以下字符串数组定义:

String[] sa = new String[3];

您可以替换现有元素的值...

sa[0] = "test";      // index 0 = "test"
sa[1] = "another";   // index 1 = "test", index 1 = "another"
sa[1] = "different"; // index 1 = "test", index 1 = "different"

您可以像普通字符串一样附加到字符串数组元素...

sa[2] = "123";  // index 2 = "123"
sa[2] += "456"; // index 2 = "123456"

现在,您可以按照自己认为合适的方式循环执行其中任何一项操作...

for (int i = 0; i < sa.length; i++) {
    String userInputVar = getUserInput();
    sa[i] = userInputVar; // replace value
    sa[i] += "foo";       // append to value
}
于 2013-10-21T22:17:26.993 回答
0

由于您需要为 4 个用户循环(根据您的评论),因此您应该围绕整个输入过程进行循环:

for (int i = 0; i < 4; i++) {
    // read the input
    // add name to correct array
}

您应该为每种事件类型保留计数器:

// here come all your variable declarations 

// declare the counters
int hockeyCounter = 0;
int tinaCounter = 0;
int catsCounter = 0;
int startrekCounter = 0;

// no need to redeclare the keyboard all the time, just once is enough
Scanner keyboard = new Scanner(System.in);

for (int i = 0; i < 4; i++) {
    System.out.println("Which Event would you like to purchase a ticket for?");
    System.out.println("1. Hockey Game 2. Tina Turner Concert \n"
                    + "3. Cats Play 4. Star Trek Convention");
    int input = keyboard.nextInt();
    System.out.println("Please enter your first and last name");
    String name = scan.nextLine();

    switch (input) {
      case 1: Hockey_Game[hockeyCounter++] = name; break;
      case 2: Turner_Concert[tinaCounter++] = name; break;
      case 3: Cats_Play[catsCounter++] = name; break;
      case 4: StarTrek_Convention[startrekCounter++] = name; break;
      default: System.out.println(input + " is not a valid input");
    }
}

现在完美了吗?不,不完全是。还是有一些问题:

  1. 我没有考虑可用门票的最大数量。如果所有客户都想要一张猫游戏的门票,此代码将崩溃。
  2. 有很多代码重复。忽略前面提到的问题已经让我很恼火了。
  3. 变量名应始终以小写字母开头(最好使用 camelCase)。这是一个 Java 约定,可以帮助其他人阅读您的代码。

以某种方式一次解决所有这些问题会更好。为此,我将提出一个数组数组(虽然这对于初学者来说可能更复杂,但会使代码更简单):

final int EVENT_COUNT = 4;
final String[] EVENTS = { "Hockey Game", "Tina Turner Concert", 
                          "Cats Play", "Star Trek Convention" };
final int[] LIMITS = { 10, 5, 3, 3 };

String[][] buyers = new String[EVENT_COUNT][];
int[] counters = new int[EVENT_COUNT];

for (int i = 0; i < EVENT_COUNT; i++) {
    buyers[i] = new String[LIMITS[i]];
}

final int CUSTOMER_COUNT = 4;
Scanner keyboard = new Scanner(System.in);

for (int i = 0; i < CUSTOMER_COUNT; i++) {
    System.out.println("Which Event would you like to purchase a ticket for?");
    for (int j = 0; j < EVENT_COUNT; j++) {
        System.out.print((j+1) + ". " + EVENTS[j] + " ");
    }
    System.out.println();
    int input = keyboard.nextInt();

    if (input < 1 || input > EVENT_COUNT) {
        System.out.println(input + " is not a valid choice");
        i--;
    } else if (counters[input-1] >= LIMITS[input-1]) {
        System.out.println(EVENTS[input-1] + " is sold out!");
        i--;
    } else {                
        System.out.println("Please enter your first and last name");
        buyers[input-1][counters[input-1]++] = scan.nextLine();
    }
}
于 2013-10-21T22:39:25.507 回答