首页 > 解决方案 > 字符串数组

问题描述

这是上一篇文章的延续。我想知道如何为字符串“name”创建一个字符串数组?基本上我希望用户能够在程序中输入多个名称(程序一直循环直到他们退出 - 询问人的姓名然后座位#然后用户可以打印座位表或退出)。我需要编辑的代码的主要部分如下......

    //changes an empty seat to a student's name at any location
    public void emptySeat()
    {
        System.out.println("Who will be taking this seat?");
        name = scan.nextLine();
        System.out.print("Which seat would you like (1-24)\n");
        int seat = scan.nextInt();
        if (seat > 0 && seat <= 24) {
            if (seating[seat - 1]) {
                System.out.print("That seat is taken.\n");
            } else {
                seating[seat - 1] = true;
                System.out.print("Seat number " + seat + " was assigned.\n");
            }
        }

    }

    //replace an empty seat with a person in the seating chart
    public static void seatingChart(boolean seat[]) {
        for(int i = 0; i < seat.length; i++) {  
            if(seat[i]) {
                System.out.print(name + " ");
            } else {
                System.out.print("o ");
            }    

            if(i % 8 == 0) {
                System.out.println();
            }
        }
    }

所有代码供参考:

package programs;

import java.util.*;

public class SeatingChart {

    java.util.Scanner scan = new java.util.Scanner(System.in);
    boolean seating[] = new boolean[24];

    boolean runAgain = true;
    int input;
    static String name;

    //runs the program with four options
    public void runProgram()
    {
        do{
            System.out.println("");
            System.out.println("Press 1 to change a seat, 2 to print the seating chart,"
                    + " 3 to clear all the seats or 4 to exit the program");
            input = scan.nextInt();
            scan.nextLine();
            switch(input)
            {
            case 1:
                emptySeat();
                break;
            case 2:
                seatingChart(seating);
                break;
            case 3:
                clearSeats(seating);
                break;
            case 4:
                runAgain = false;
                break;
            default:
                System.out.println("That is not an option, please try again!");
            }
        }while(runAgain);
    }

    //changes an empty seat to a student's name at any location
    public void emptySeat()
    {
        System.out.println("Who will be taking this seat?");
        name = scan.nextLine();
        System.out.print("Which seat would you like (1-24)\n");
        int seat = scan.nextInt();
        if (seat > 0 && seat <= 24) {
            if (seating[seat - 1]) {
                System.out.print("That seat is taken.\n");
            } else {
                seating[seat - 1] = true;
                System.out.print("Seat number " + seat + " was assigned.\n");
            }
        }

    }

    //replace an empty seat with a person in the seating chart
    public static void seatingChart(boolean seat[]) {
        for(int i = 0; i < seat.length; i++) {  
            if(seat[i]) {
                System.out.print(name + " ");
            } else {
                System.out.print("o ");
            }    

            if(i % 8 == 0) {
                System.out.println();
            }
        }
    }

    //clears all the seats
    public void clearSeats(boolean seat[])
    {
            for(int i = 0; i < seat.length; i++) {  
                if(seat[i]) {
                    System.out.print("empty seat  ");
                } else {
                    System.out.print("empty seat  ");
                }    

                if(i % 8 == 0) {
                    System.out.println();
                }
            }
    }

    public static void main(String[] args) 
    {
        SeatingChart prog = new SeatingChart();
        prog.runProgram();
    }

}

我是java初学者,不胜感激,谢谢。


编辑:好的,让这一点更清楚:当前错误输出:

Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
1
Who will be taking this seat?
Bob
Which seat would you like (1-24)
3
Seat number 3 was assigned.

Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
2
o 
o Bob o o o o o o 
o o o o o o o o 
o o o o o o o 
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
1
Who will be taking this seat?
Joe
Which seat would you like (1-24)
5
Seat number 5 was assigned.

Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
2
o 
o Joe o Joe o o o o 
o o o o o o o o 
o o o o o o o 

我想要的输出是:

Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
1
Who will be taking this seat?
Bob
Which seat would you like (1-24)
3
Seat number 3 was assigned.

Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
2

o o Bob o o o o o
o o o o o o o o 
o o o o o o o o
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
1
Who will be taking this seat?
Joe
Which seat would you like (1-24)
5
Seat number 5 was assigned.

Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
2
o o Bob o Joe o o o
o o o o o o o o 
o o o o o o o o

标签: java

解决方案


最简单的就是改成boolean seating[]. String seating[]每次保存新座位时,而不是将其保存为布尔值,而是将人名保存到数组中的该项目中:

String seating[] = new String[24];
// ...
if (seat > 0 && seat <= 24) {
        if (seating[seat - 1] != null) {
            System.out.print("That seat is taken.\n");
        } else {
            seating[seat - 1] = name;
            System.out.print("Seat number " + seat + " was assigned.\n");
        }
    }

进而

public static void seatingChart(String seat[]) {
    for(int i = 0; i < seat.length; i++) {  
        if(seat[i] != null) {
            System.out.print(seat[i] + " ");
        } else {
            System.out.print("o ");
        }    

        if(i % 8 == 0) {
            System.out.println();
        }
    }
}

推荐阅读