用户问两次,第二次用户得到不同的答案?
User asks twice and the second time the user gets different answer?
好吧,我的代码有点长,所以当您看到 x ==1 部分时,我只放了第一部分,实际上它有七个这样的部分。我的目标就像你选择了 1A 并且它给了你 table (那部分是我做的。)我想要的是如果用户再次写 1A 系统必须说对不起。如何在不使用任何软件包的情况下执行此操作?
import java.util.Scanner;
class deneme{
public void printTable(String [ ][ ] seat){
for(int r=0; r<seat.length; r++){
for(int c = 0; c< seat[0].length; c++){
System.out.print(seat[r][c]);
}
System.out.println();
}
}
public static void main(String[] args) {
deneme k = new deneme();
String [][] seat = {{"1","A","B","C","D"},{"2","A","B","C","D"},{"3","A","B","C","D"},{"4","A","B","C","D"},{"5","A","B","C","D"},{"6","A","B","C","D"},{"7","A","B","C","D"}};
Scanner read = new Scanner(System.in);
int x = 6;
String s;
String d;
while(x <= 7){
System.out.println("Please choose your seat number (1 to 7).But if you want to terminate the program please write 0");
s = read.nextLine();
x = Integer.parseInt(s);
System.out.println("Please choose your seat row (A to D)");
d = read.nextLine();
if(x == 1){
if((d.equals("A")|| d.equals("a"))){
seat[0][1] = "X";
k.printTable(seat);
}
else if((d.equals("B")|| d.equals("b")) ){
seat[0][2] = "X";
System.out.println("Your seat is chosen ");
k.printTable(seat);
}
else if((d.equals("C")|| d.equals("c")) ){
seat[0][3] = "X";
System.out.println(" Your seat is chosen ");
k.printTable(seat);
}
else if((d.equals("D")|| d.equals("d")) ){
seat[0][4] = "X";
System.out.println(" Your seat is chosen " );
k.printTable(seat);
}
}
你可以做很多事情来清理你的代码,但是......特别是对于你的问题,你从来没有检查过 seat[x][y]
之前是否已经有一个 "X"
向其写入 "X"
。
如果您对数组的值执行简单的 if-check,并且它已经等于 "X"
,那么您可以打印消息而不是写入数组(并且可能会覆盖之前选择的选座)。
您需要一个 Map
数据结构(将数据保存为键值对)来存储已经预订的座位。
您可以参考以下步骤使用Map
解决您的问题:
(1)当用户输入seat-number时,检查Map
对象中是否存在
(2) 如果存在,即已经预订则显示错误信息
(3)如果不存在,则允许用户预订并存储在地图中
我在下面提供了使用 Map
:
的伪代码
public class Deneme {
private static Map<String, String> seatsUsed = new HashMap<>();
public static void main(String[] args) {
//add other code here
while(x <= 7){
//add other code here
if(seatsUsed.get(x+d) != null) {
System.out.println(" Sorry, seat alrady booked");
}
if(x == 1){
if((d.equals("A")|| d.equals("a"))){
seat[0][1] = "X";
k.printTable(seat);
seatsUsed.put(x+d, "Y");//Y indicates that booked
}
//add other code
}
}
此外,作为旁注,您的代码存在很多问题,如下所述:
(1) 遵循 Java 命名约定 Class 名称以大写开头
(2) 你的代码很难理解,尝试重构为更小的方法
(3) 有效地使用现有的 String
/其他 API 方法,就像您可以使用 equalsIgnoreCase
而不是编写 d.equals("A")|| d.equals("a")
在将"X"分配给座位[0][1]之前,您可以检查它是否已经是"X"。然后,你可以打印 "Sorry"
if(x == 1)
{
if((d.equals("A")|| d.equals("a")))
{
if(seat[0][1].equals("X"))
{
//Print Sorry
}
else
{
seat[0][1] = "X";
k.printTable(seat);
}
}
}
好吧,我的代码有点长,所以当您看到 x ==1 部分时,我只放了第一部分,实际上它有七个这样的部分。我的目标就像你选择了 1A 并且它给了你 table (那部分是我做的。)我想要的是如果用户再次写 1A 系统必须说对不起。如何在不使用任何软件包的情况下执行此操作?
import java.util.Scanner;
class deneme{
public void printTable(String [ ][ ] seat){
for(int r=0; r<seat.length; r++){
for(int c = 0; c< seat[0].length; c++){
System.out.print(seat[r][c]);
}
System.out.println();
}
}
public static void main(String[] args) {
deneme k = new deneme();
String [][] seat = {{"1","A","B","C","D"},{"2","A","B","C","D"},{"3","A","B","C","D"},{"4","A","B","C","D"},{"5","A","B","C","D"},{"6","A","B","C","D"},{"7","A","B","C","D"}};
Scanner read = new Scanner(System.in);
int x = 6;
String s;
String d;
while(x <= 7){
System.out.println("Please choose your seat number (1 to 7).But if you want to terminate the program please write 0");
s = read.nextLine();
x = Integer.parseInt(s);
System.out.println("Please choose your seat row (A to D)");
d = read.nextLine();
if(x == 1){
if((d.equals("A")|| d.equals("a"))){
seat[0][1] = "X";
k.printTable(seat);
}
else if((d.equals("B")|| d.equals("b")) ){
seat[0][2] = "X";
System.out.println("Your seat is chosen ");
k.printTable(seat);
}
else if((d.equals("C")|| d.equals("c")) ){
seat[0][3] = "X";
System.out.println(" Your seat is chosen ");
k.printTable(seat);
}
else if((d.equals("D")|| d.equals("d")) ){
seat[0][4] = "X";
System.out.println(" Your seat is chosen " );
k.printTable(seat);
}
}
你可以做很多事情来清理你的代码,但是......特别是对于你的问题,你从来没有检查过 seat[x][y]
之前是否已经有一个 "X"
向其写入 "X"
。
如果您对数组的值执行简单的 if-check,并且它已经等于 "X"
,那么您可以打印消息而不是写入数组(并且可能会覆盖之前选择的选座)。
您需要一个 Map
数据结构(将数据保存为键值对)来存储已经预订的座位。
您可以参考以下步骤使用Map
解决您的问题:
(1)当用户输入seat-number时,检查Map
对象中是否存在
(2) 如果存在,即已经预订则显示错误信息
(3)如果不存在,则允许用户预订并存储在地图中
我在下面提供了使用 Map
:
public class Deneme {
private static Map<String, String> seatsUsed = new HashMap<>();
public static void main(String[] args) {
//add other code here
while(x <= 7){
//add other code here
if(seatsUsed.get(x+d) != null) {
System.out.println(" Sorry, seat alrady booked");
}
if(x == 1){
if((d.equals("A")|| d.equals("a"))){
seat[0][1] = "X";
k.printTable(seat);
seatsUsed.put(x+d, "Y");//Y indicates that booked
}
//add other code
}
}
此外,作为旁注,您的代码存在很多问题,如下所述:
(1) 遵循 Java 命名约定 Class 名称以大写开头
(2) 你的代码很难理解,尝试重构为更小的方法
(3) 有效地使用现有的 String
/其他 API 方法,就像您可以使用 equalsIgnoreCase
而不是编写 d.equals("A")|| d.equals("a")
在将"X"分配给座位[0][1]之前,您可以检查它是否已经是"X"。然后,你可以打印 "Sorry"
if(x == 1)
{
if((d.equals("A")|| d.equals("a")))
{
if(seat[0][1].equals("X"))
{
//Print Sorry
}
else
{
seat[0][1] = "X";
k.printTable(seat);
}
}
}