我在使用String Arrays编写代码时遇到了问题

时间:2013-10-21 22:02:30

标签: java

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

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 个答案:

答案 0 :(得分: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,但如果赋值的目的是使用数组,那么你可以这样做。

答案 1 :(得分:0)

不要使用String []使用List

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

同时定义一个int [](这次数组就足够了),它保存每个事件的最大插槽,当客户要求在已经达到最大的事件上找到一个点时,你可以给他一个坏消息。< / p>

答案 2 :(得分:0)

  

我的问题是我不知道如何使用循环继续向String数组添加值而不删除以前的值。

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

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"

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

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
}

答案 3 :(得分: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约定,可以帮助其他人阅读您的代码。
  4. 以某种方式解决所有这些问题会更好。为此我会提出一个数组数组(虽然对初学者来说可能更复杂,但会使代码更简单):

    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();
        }
    }