Как сохранить пользовательский ввод в операторе do while - PullRequest
0 голосов
/ 29 сентября 2019

так что я создаю бронирование авиабилетов, но после повторной попытки места, которые я впервые выбрал, не могут прочитать в результатах

пример

  ABC DEF 
1 xxx xXx
2 xxx xxx

я выбираю место 1E и после использования tryснова и выберите 2А, ​​это станет таким

  ABC DEF 
1 xxx xxx
2 Xxx xxx

не

  ABC DEF 
1 xxx xXx
2 Xxx xxx

Может ли кто-нибудь помочь мне, мой профессор не учил так много, я просто исследую сам, если у меня есть свободное время

код ниже:

    import java.util.*;
    import javax.swing.JOptionPane;
    public class Exer2M{

    static Scanner in = new Scanner(System.in);
    static char array[][] = new char[14][7];
    static int num=1;
    static int k=1;
    static int row=0,col=0,O=0;
    static int i,x;
    static int seat;
    static void intro(){    
    System.out.println("pick your desinated seat");
    row=1;  
    System.out.println("    A B C D E F");
        for(i=0; i<13; i++){
            for(x=0; x<6; x++){

                array[i][x]='*';
                if(x==0){
                    if(num<=9)
                    System.out.print(num+"  ");
                    else
                        System.out.print(num+" ");    

                }

                System.out.print(" "+array[i][x]);
            }
            System.out.print("");
            System.out.println();
            num++;


        }
        System.out.println("");
        System.out.println("* = seat is not taken");
        System.out.println("X = seat is taken");

        System.out.println("");
    }
        static void process(){
        do {
        System.out.print("Select a ticket type [1]First Class [2]Business Class [3]Economy Class : ");
        seat = in.nextInt();
        if (seat == 1 || seat == 2 || seat == 3) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid ticket type number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);


    switch(seat){
        case 1: do {

                  System.out.print("Enter Row [1-2]:");
                      row= in.nextInt();

            if (row == 1 || row == 2) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);

            do {
                  System.out.print("Enter Column [1=A|2=B|3=C|4=D|5=E|6=F]:");
                      col = in.nextInt();
             if (col == 1 || col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);         


        break;
        case 2: do {

                  System.out.print("Enter Row [3-7]:");
                      row= in.nextInt();

            if (row == 3 || row == 4 || row == 5 || row == 6 || row == 7) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);

            do {
                  System.out.print("Enter Column [1=A|2=B|3=C|4=D|5=E|6=F]:");
                      col = in.nextInt();
             if (col == 1 || col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true); 

        break;
        case 3: do {

                  System.out.print("Enter Row [8-13]:");
                      row= in.nextInt();

            if (row == 8 || row == 9 || row == 10 || row == 11 || row == 12 || row == 13) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);

            do {
                  System.out.print("Enter Column [1=A|2=B|3=C|4=D|5=E|6=F]:");
                      col = in.nextInt();
             if (col ==1 || col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true); 

        break;                
        default: 
        break;
        }
    }
    static void results(){
    System.out.println("");    
    System.out.println("    A B C D E F");
    num=1;
    row=row-1;
    col=col-1;
    for(i=0;i<13;i++){
    for(x=0; x<6; x++){
    if(row==i && col==x ){
        array[i][x]='X';
        }
    else{

    array[i][x]='*';}
        if(x==0){
    if(num<=9)
    System.out.print(num+"  ");
    else
    System.out.print(num+" ");    

    }

    System.out.print(" "+array[i][x]);
    }

    System.out.print("");
    System.out.println();
    num++;

        }
    }

    static char getResponse1(String prompt) {
    char ty;
    System.out.print (prompt);
    ty = in.next().toUpperCase().charAt(0);
    return ty;
}


      public static void main(String[]args){
          char ans;
          i = 0;
          x = 0;
          intro();
          do{
          process();
          results();
          ans = getResponse1(" Do you want to get another seat?(Y/N)?");
            } while (ans == 'Y'|| ans == 'y');
    } 
}

1 Ответ

0 голосов
/ 29 сентября 2019

Создайте метод сброса и логический флаг = false.

Когда пользователь скажет «да» для повторного выбора места, установите для флага значение «истина» и вызовите метод сброса для массива.

import java.util.*;
    import javax.swing.JOptionPane;
    public class Exer2M{

    static Scanner in = new Scanner(System.in);
    static char array[][] = new char[14][7];
    static int num=1;
    static int k=1;
    static int row=0,col=0,O=0;
    static int i,x;
    static int seat;
    static boolean flag = false;

    static void intro(){    
    System.out.println("pick your desinated seat");
    row=1;  
    System.out.println("    A B C D E F");
        for(i=0; i<13; i++){
            for(x=0; x<6; x++){

                array[i][x]='*';
                if(x==0){
                    if(num<=9)
                    System.out.print(num+"  ");
                    else
                        System.out.print(num+" ");    

                }

                System.out.print(" "+array[i][x]);
            }
            System.out.print("");
            System.out.println();
            num++;


        }
        System.out.println("");
        System.out.println("* = seat is not taken");
        System.out.println("X = seat is taken");

        System.out.println("");
    }
        static void process(){
        do {
        System.out.print("Select a ticket type [1]First Class [2]Business Class [3]Economy Class : ");
        seat = in.nextInt();
        if (seat == 1 || seat == 2 || seat == 3) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid ticket type number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);


    switch(seat){
        case 1: do {

                  System.out.print("Enter Row [1-2]:");
                      row= in.nextInt();

            if (row == 1 || row == 2) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);

            do {
                  System.out.print("Enter Column [1=A|2=B|3=C|4=D|5=E|6=F]:");
                      col = in.nextInt();
             if (col == 1 || col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);         


        break;
        case 2: do {

                  System.out.print("Enter Row [3-7]:");
                      row= in.nextInt();

            if (row == 3 || row == 4 || row == 5 || row == 6 || row == 7) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);

            do {
                  System.out.print("Enter Column [1=A|2=B|3=C|4=D|5=E|6=F]:");
                      col = in.nextInt();
             if (col == 1 || col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true); 

        break;
        case 3: do {

                  System.out.print("Enter Row [8-13]:");
                      row= in.nextInt();

            if (row == 8 || row == 9 || row == 10 || row == 11 || row == 12 || row == 13) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true);

            do {
                  System.out.print("Enter Column [1=A|2=B|3=C|4=D|5=E|6=F]:");
                      col = in.nextInt();
             if (col ==1 || col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7) break; 
            else 
            JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
            } while (true); 

        break;                
        default: 
        break;
        }
    }
    static void results(){
    System.out.println("");    
    System.out.println("    A B C D E F");
    num=1;
    row=row-1;
    col=col-1;
    for(i=0;i<13;i++){
    for(x=0; x<6; x++){
    if(row==i && col==x ){
        array[i][x]='X';
        }
    else{

    array[i][x]='*';}
        if(x==0){
    if(num<=9)
    System.out.print(num+"  ");
    else
    System.out.print(num+" ");    

    }

    System.out.print(" "+array[i][x]);
    }

    System.out.print("");
    System.out.println();
    num++;

        }
    }

    static char getResponse1(String prompt) {
    char ty;
    System.out.print (prompt);
    ty = in.next().toUpperCase().charAt(0);
    flag = true;
    return ty;
}


      public static void main(String[]args){
          char ans;
          i = 0;
          x = 0;
          intro();
          do{
          if(flag = true){
             reset();
          }
          process();
          results();
          ans = getResponse1(" Do you want to get another seat?(Y/N)?");
            } while (ans == 'Y'|| ans == 'y');
     }

  public static void reset(){

    for(i=0;i<13;i++){
        for(x=0; x<6; x++){
              array[i][x]='x';
        }    
    }
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...