作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作座位表程序。除了出现在不同线路上的第一个座位和另一个主要问题之外,它主要在工作。主要问题是,当我尝试在座位表中输入多个名字时,它只保留一个。
我知道这与我的变量“名称”有关。 “名称”不断被输入的下一个名称覆盖。
我的代码:
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/
我是一名优秀的程序员,十分优秀!