如何为我的Java程序中的每次运行更改变量

时间:2019-06-18 19:48:30

标签: java arrays

我正在制作座位表程序。它主要是与第一个席位出现在不同的路线上分开工作,而另一个问题是主要问题。主要问题是,当我尝试在座位表中输入多个名称时,只会保留一个。

我知道它与我的变量“名称”有关。 “名称”会不断被输入的下一个名称覆盖。

我的代码:

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

    }

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

}

当前错误的输出:

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
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

1 个答案:

答案 0 :(得分:2)

代替使用布尔数组来表示座位表,也许可以使用字符串数组。如果座位为空,则将该字符串保留为空。如果座位已满,请在该索引处设置字符串,使其等于坐在其中的人的名字。