用户要求两次,第二次用户得到不同的答案?

时间:2017-04-02 12:04:17

标签: java xcode java.util.scanner computer-science

嗯,我的代码有点长,所以当你看到x == 1部分时,我只是放了第一部分,实际上它有七个这样的部分。我的目标就像你选择1A并且它给你桌子(那部分我做了。)我想要的是如果用户再次写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);

            }


        }

3 个答案:

答案 0 :(得分:0)

你可以做很多清理代码的事情,但是......特别针对你的问题,你从来没有检查seat[x][y]之前是否已经有"X"写一个"X"给它。

如果对数组的值执行简单的if-check,并且它已经等于"X",则可以打印消息而不是写入数组(并可能覆盖先前选择的席位选择)。

答案 1 :(得分:0)

您需要Map数据结构(将数据保存为键值对)以存储已预订的座位。

您可以使用Map

参考以下步骤解决问题

(1)当用户输入座位号时,检查Map对象中是否存在

(2)如果存在,即已经预订,则显示错误消息

(3)如果它不存在,则允许用户预订并将其存储在Map

我在下面提供了使用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命名约定类名以大写

开头

(2)您的代码很难遵循,尝试重构为更小的方法

(3)有效地使用现有的String /其他API方法,就像您可以使用equalsIgnoreCase而不是写d.equals("A")|| d.equals("a")

答案 2 :(得分:0)

在分配&#34; X&#34;座位[0] [1],你可以检查它是否已经是&#34; X&#34;。然后,您可以打印&#34;抱歉&#34;

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