Java I';我在用字符串数组编写代码时遇到问题

Java I';我在用字符串数组编写代码时遇到问题,java,Java,所以我必须写一段代码来管理一个事件。共有4个活动,座位有限。我应该为每一个写字符串数组。然后,程序会将名称添加到相关字符串中。我的问题是,我不知道如何使用循环不断地向字符串数组添加值,而不删除以前的值。任何帮助都将不胜感激 import java.util.Scanner; public class Assignment_1 { public static void main(String[] args) { String [] Hockey_Game; Hockey_

所以我必须写一段代码来管理一个事件。共有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 +",");
    }
import java.util.Scanner;
公开课作业1{
公共静态void main(字符串[]args){
曲棍球比赛;
曲棍球比赛=新弦[10];
弦乐演奏会;
特纳音乐会=新弦[5];
你玩的是弦乐;
Cats_Play=新字符串[3];
字符串[]StarTrek_约定;
StarTrek_约定=新字符串[3];
System.out.println(“您想为哪个事件购买门票?”);
System.out.println(“1.曲棍球比赛2.蒂娜·特纳音乐会\n”
+“3.猫玩4.星际迷航大会”);
扫描仪键盘=新扫描仪(System.in);
int input=keyboard.nextInt();
System.out.println(“请输入您的名字和姓氏”);
扫描仪扫描=新扫描仪(System.in);
字符串名称=scan.nextLine();
for(int i=0;i
不要使用字符串[]使用列表

List<String> event1 = new List<String>();
. . .
event1.add(name);
List event1=新列表();
. . .
事件1.添加(名称);
还要定义一个int[](这一次一个数组就足够了)来保存每个事件的最大插槽,这样当客户要求在一个已经用完的事件中找到一个位置时,您可以告诉他坏消息

我的问题是,我不知道如何使用循环不断地向字符串数组添加值,而不删除以前的值

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

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
}
for(int i=0;i
这应该是你想要的

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 ...
        }
    }
}
}
import java.util.Scanner;
公开课作业1{
公共静态void main(字符串[]args){
曲棍球比赛;
int numHockey=0;
曲棍球比赛=新弦[10];
弦乐演奏会;
int numConcert=0;
特纳音乐会=新弦[5];
你玩的是弦乐;
int numPlay=0;
Cats_Play=新字符串[3];
字符串[]StarTrek_约定;
int numCon=0;
StarTrek_约定=新字符串[3];
for(int user=0;user<1;user++){
系统输出
.println(“您想购买哪项赛事的门票?”);
System.out.println(“1.曲棍球比赛2.蒂娜·特纳音乐会\n”
+“3.猫玩4.星际迷航大会”);
扫描仪键盘=新扫描仪(System.in);
int input=keyboard.nextInt();
System.out.println(“请输入您的名字和姓氏”);
扫描仪扫描=新扫描仪(System.in);
字符串名称=scan.nextLine();
开关(输入){
案例1:
if(纽霍基<曲棍球比赛长度){
曲棍球比赛[橄榄球]=名称;
}
numHockey++;
对于(int j=0;j

我同意另一位回答者的观点,即您应该为此使用List/ArrayList,但如果作业的目的是使用数组,那么以下是您可以做到的方法。

由于您需要为4个用户循环(根据您的评论),您应该在整个输入过程中循环:

for (int i = 0; i < 4; i++) {
    // read the input
    // add name to correct array
}
for(int i=0;i<4;i++){
//读取输入
//将名称添加到正确的数组中
}
您应该为每种事件类型保留计数器:

// 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");
    }
}
//下面是您所有的变量声明
//申报柜台
int曲棍球计数器=0;
int=0;
int CATS中心=0;
int startrekCounter=0;
//不需要一直重新声明键盘,只要一次就足够了
扫描仪键盘=新扫描仪(System.in);
对于(int i=0;i<4;i++){
System.out.println(“您想为哪个事件购买门票?”);
System.out.println(“1.曲棍球比赛2.蒂娜·特纳音乐会\n”
+“3.猫玩4.星际迷航大会”);
int input=keyboard.nextInt();
System.out.println(“请输入您的名字和姓氏”);
字符串名称=scan.nextLine();
开关(输入){
案例1:曲棍球比赛[hockeyCounter++]=名称;休息;
案例2:Turner_Concert[tinaCounter++]=姓名;中断;
案例3:Cats_Play[catsconter++]=name;break;
案例4:StarTrek_约定[startrekCounter++]=name;break;
默认值:System.out.println(输入+“不是有效输入”);
}
}
这个现在完美了吗?不,不完全完美。仍然存在一些问题:

  • 我没有说明可用门票的最大数量。如果所有客户都想要一张猫戏门票,此代码将崩溃
  • 有很多代码重复。忽略前面提到的问题已经够让我恼火的了
  • 变量名应始终以小写字母开头(最好使用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();
        }
    }