gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:49:18 25 4
gpt4 key购买 nike

我正在制作座位表程序。除了出现在不同线路上的第一个座位和另一个主要问题之外,它主要在工作。主要问题是,当我尝试在座位表中输入多个名字时,它只保留一个。

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

我的代码:

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

最佳答案

也许您可以使用字符串数组,而不是使用 boolean 数组来表示座位表。如果座位是空的,则将该字符串保留为空。如果座位已满,则将该索引处的字符串设置为等于坐在其中的人的姓名。

关于java - 如何为我的java程序中的每次运行更改变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56656083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com