gpt4 book ai didi

java - 字符串数组

转载 作者:行者123 更新时间:2023-12-02 04:16:19 25 4
gpt4 key购买 nike

这是上一篇文章的延续。我想知道如何为字符串“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

最佳答案

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

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

关于java - 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657859/

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