这是上一篇文章的延续。 我想知道如何为字符串“名称”创建字符串数组? 基本上,我希望用户能够在程序中输入多个名称(程序会不断循环播放,直到他们退出-要求输入人名,然后再输入座位号,然后用户可以打印座位表或退出)。我需要编辑的代码的主要部分在下面...
//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
答案 0 :(得分:1)
最简单的方法是将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();
}
}
}
答案 1 :(得分:-1)
您去哪里,问我是否不懂,如果对您有帮助,还可以捏绿色的东西:)
请注意,您不需要静态变量名,还请检查方法clearSeats,我不确定这是否就是您要使用的变量,反正希望这对您有帮助
import java.util.*;
public class ProblemSolved {
java.util.Scanner scan = new java.util.Scanner(System.in);
boolean seating[] = new boolean[24];
String seatings[] = new String[24];
boolean runAgain = true;
int input;
//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(seatings);
break;
case 3:
clearSeats(seatings);
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?");
String name = scan.nextLine();
System.out.print("Which seat would you like (1-24)\n");
int seat = scan.nextInt();
if (seat > 0 && seat <= 24) {
if (seatings[seat-1] != null) {
System.out.print("That seat is taken.\n");
} else {
seatings[seat - 1] = name;
System.out.print("Seat number " + seat + " was assigned.\n");
}
}
}
//replace an empty seat with a person in the seating chart
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+1) % 8 == 0) {
System.out.println();
}
}
}
//clears all the seats
public void clearSeats(String seat[])
{
for(int i = 0; i < seat.length; i++) {
if(seat[i] == null) {
System.out.print("empty seat ");
} else {
System.out.print("not empty seat ");
}
if((i+1) % 8 == 0) {
System.out.println();
}
}
}
public static void main(String[] args)
{
ProblemSolved prog = new ProblemSolved();
prog.runProgram();
}
}