Вертикальная печать с java массивов - PullRequest
0 голосов
/ 05 августа 2020

У меня проблемы с кодом именно с массивами. У меня проблемы с вертикальной печатью массивов. Единственный способ, который я мог придумать, - это использовать один полный массив, но у вас не может быть значений string и int в одном массиве.

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

import java.util.Scanner;
//imports scanner object 
import java.util.Arrays;
//imports scanner object
public class Assignment8_jkaur1240026 {

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
int club_seat = 0;
//creates and initializes club_seat variable
int coach_seat = 5;
//creates and initializies coach_seat variable
int counting_line1 = 0;
//creates and initializes counting1 variable
int counting_line2 = 0;
//creates and initializes counting2 variable
int counting_line3 = 0;
//creates and initializes counting3 variable
boolean reservation = true; 
//boolean used to keep the loop going
Scanner input = new Scanner( System.in );
// create a Scanner object 
do{
    System.out.print( "Please specify service class  " );
    System.out.println( "(1 = club 2 = coach): ");
    //prompt the user to say club or coach seating 
    int seating_class = input.nextInt();
    //stores the input in seating_class variable 
    System.out.println();
    //line break
    if(seating_class == 1){
        //if seating_class var is 1
        if(club_seat < 5){
            //if club seat is less than 5
            club_seat = club(club_seat);
            System.out.println("Club Class: Seat # "+ club_seat);
            //call the club method and prints it
        }
        else{
            //if club seats are greater than 5
            System.out.println("Sorry we are out of club seats");
        }
    }
    else if(seating_class == 2){
        //if seating_class var is 1
        if(coach_seat < 20){
            //if club seat is less than or equal to 20
            coach_seat = coach(coach_seat);
            System.out.print("Coach Class: Seat # "+ coach_seat);
            //call the club method and prints it
        }
        else{
            //if coach seats are greater than 20
            System.out.println("Sorry we are out of coach seats");
        }

    }
    else{
        System.out.println("Sorry we don't have that service class");
        //Say we don't have that class if seating class is not 1 or 2
    }
    System.out.println();
    //line break 
    System.out.println("Another reservation (1 = yes 2 = no)? ");
    //ask user if they want another reservation 
    int another_reservation = input.nextInt();
    //store the value inside another reservation int
    if(another_reservation == 1){
        //if anther reservation is 1
        reservation = true;
        //keep the boolean reservation as true
    }
    else if(another_reservation == 2){
        //if another reservation is 2
        reservation = false;
        //change the boolean reservation to false
        System.out.println("Have a good day!");
        //say bye bye
    }
    else{
        //if another reservation int is not 1 or 2
        System.out.println("Sorry we don't understand that command");
        //tell the user that they are invalid response
    }
    System.out.println();
    //line break
}
while(reservation == true);
//keep going if reservation is true
String reservation_array_line1[][] = new String [20][1];
while(counting_line1 < 20){
    reservation_array_line1[counting_line1][0] = "Seat";
    counting_line1 = counting_line1 + 1;
    System.out.println(reservation_array_line1[counting_line1][0]);
counting_line1 = counting_line1 + 1;
}
int reservation_array_line2[][] = new int [20][1];
while(counting_line2 < 20){
    reservation_array_line2[counting_line2][0] = counting_line2;
    counting_line2 = counting_line2 + 1;
    System.out.println(reservation_array_line2[counting_line2][0]);
counting_line2 = counting_line2 + 1;
}
String reservation_array_line3[][] = new String [20][1];
while(counting_line3 < 20){
    reservation_array_line3[counting_line3][0] = "E";
    counting_line3 = counting_line3 + 1;
    System.out.println(reservation_array_line3[counting_line3][0]);
ounting_line3 = counting_line3 + 1;
}


}
public static int club( int club_seat ){
//method to assign user club seats
club_seat = club_seat + 1;
//increase club_seat by one
return(club_seat);
//return that value
}
public static int coach( int coach_seat ){
//method to assign user coach seats 
coach_seat = coach_seat + 1;
//increase coach_seat by one
return(coach_seat);
//return that value
}

}**

1 Ответ

0 голосов
/ 06 августа 2020
  1. Вы можете использовать Object-Arrays (Object [] []) для хранения в нем любых данных, которые также включают строки, целые числа, числа с плавающей запятой и т. Д. c.

  2. Вы можете распечатать эти массивы с помощью циклов for, где вам не нужны отдельные счетчики

  3. Реализация:

publi c class Test {

// in this array you can input any Number/String/Objects
public static Object list[][] = new Object[5][20]; // <-- just change dimensions here

public static void main(String[] args) {
    //
    // this part is just here to randomly fill the array
    for (int i = 0; i < list.length; i++) {
        for (int j = 0; j < list[i].length; j++) {

            int rand = (int) (Math.random() * 2);
            switch (rand) {
            case 0:
                list[i][j] = "A"; // example
                break;
            case 1:
                list[i][j] = 1; // example
                break;
            }
        }
    }
    //
    
    print_Y_X();
    System.out.println(); // free line
    print_X_Y();
}

// for printing the array, where every line represents the first dimension of the array
public static void print_X_Y() {
    
    String spacing = " "; // spacing between the arrayobjects
    
    for(int i=0;i<list.length;i++) {
        System.out.print("[ "); // Line-start
        for(int j=0;j<list[i].length;j++) {
            System.out.print(list[i][j]+(j != list[i].length-1 ? spacing : ""));
        }
        System.out.println(" ]"); // Line-end
    }
}

// for printing the array, where every line represents the second dimension of the array
public static void print_Y_X() {
    
    String spacing = " "; // spacing between the arrayobjects

    for(int j=0;j<list[0].length;j++) {
        System.out.print("[ "); // Line-start
        for(int i=0;i<list.length;i++) {
            System.out.print(list[i][j]+(i != list.length-1 ? spacing : ""));
        }
        System.out.println(" ]"); // Line-end
    }
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...