在Java中更新数组

huangapple go评论52阅读模式
英文:

Updating arrays in Java

问题

我非常新于Java感到迷茫我正在制作一个程序用于检查数组以查看哪些座位是可用的如果可用它将更新它们使其不再可用

它正在更新它们但一旦我使用一个单独的方法打印出所有的数组它只会打印默认设置的变量

我知道我的代码可能有点混乱我还是新手希望能得到如何让它正常工作的任何帮助

英文:

I am very new to Java and am getting lost! I am making a programme that checks the arrays to see what seats are available and if they are it will update them so they are no longer available.

it is updating them but once I print out all of my arrays using a separate method it just prints the default set variable.

I know my code may be a bit messy I am still new and would appreciate any help on how to get it working right.

import java.util.Scanner;

public class Theatre {
    
    // creating the rows of seats

    public int[] rowOne = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    public int[] rowTwo = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    public int[] rowThree = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    static void buy_ticket(){

        //Allows you to use variables set in the class
        Theatre myObjTheatre = new Theatre();

        System.out.println("Please select a row (1-3):");
        Scanner row = new Scanner(System.in);
            int selectRow = row.nextInt();
        
        if(selectRow == 1){
            System.out.println("You have selected row 1.");
            System.out.println("Please select a seat (1-12):");
            Scanner seat = new Scanner(System.in);
            int selectSeat = seat.nextInt();
            if(selectSeat <= 12){
                if(myObjTheatre.rowOne[selectSeat-1] == 0){
                    System.out.println("You bought a ticket"); 
                    myObjTheatre.rowOne[selectSeat-1] = 1;
                    for(int i: myObjTheatre.rowOne){
                        System.out.print(i);
                    }
                }
                else{
                    System.out.println("Seat is occupied");
                }
            }
            else if(selectSeat >= 13){
                    System.out.println("The chosen seat does not exist.");
                }
        }

        else if(selectRow == 2){
            System.out.println("You have selected row 2.");
            System.out.println("Please select a seat (1-16):");
            Scanner seat2 = new Scanner(System.in);
            int selectSeat2 = seat2.nextInt();
            if(selectSeat2 <= 16){
                if(myObjTheatre.rowTwo[selectSeat2-1] == 0){
                    System.out.println("You bought a ticket"); 
                    myObjTheatre.rowTwo[selectSeat2-1] = 1;
                }
                else{
                    System.out.println("Seat is occupied");
                }
            }
            else if(selectSeat2 >= 17){
                    System.out.println("The chosen seat does not exist.");
                }
        }
        else if(selectRow == 3){
            System.out.println("You have selected row 3.");
            System.out.println("Please select a seat (1-20):");
            Scanner seat3 = new Scanner(System.in);
            int selectSeat3 = seat3.nextInt();
            if(selectSeat3 <= 20){
                if(myObjTheatre.rowThree[selectSeat3-1] == 0){
                    System.out.println("You bought a ticket"); 
                    myObjTheatre.rowThree[selectSeat3-1] = 1;
                }
                else{
                    System.out.println("Seat is occupied");
                }
            }
            else if(selectSeat3 >= 21){
                    System.out.println("The chosen seat does not exist.");
                }
        }   
    }
    
    public static void print_seating_area() {
        Theatre myObjTheatre = new Theatre();
        // prints the rows
        for (int i = 0; i < myObjTheatre.rowOne.length; i++) {
            System.out.print(myObjTheatre.rowOne[i]);
        }
        System.out.println();
        for (int i = 0; i < myObjTheatre.rowTwo.length; i++) {
            System.out.print(myObjTheatre.rowTwo[i]);

        }
        System.out.println();
        for (int i = 0; i < myObjTheatre.rowThree.length; i++) {
            System.out.print(myObjTheatre.rowThree[i]);

        }
    }

    public static void main(String[] args) {
       System.out.println("Welcome to the New Theatre");

        while(true){
        System.out.println();
        System.out.println("------------------------------------");
        System.out.println("Please select an option:");
        System.out.println("    1) Buy a ticket");
        System.out.println("    2) Print seating area");
        System.out.println("    3) Cancel a ticket");
        System.out.println("    4) List available seats");
        System.out.println("    5) Save to file");
        System.out.println("    6) Load from file");
        System.out.println("    7) Print ticket information and total price");
        System.out.println("    8) Sort tickets by price");
        System.out.println("    0) Quit");
        System.out.println("------------------------------------");
        System.out.println("Enter option:");

        //takes the user input to select an option
        Scanner menuOption = new Scanner(System.in);
        int option = menuOption.nextInt();

        if(option == 1){
            buy_ticket();
        }
        else if(option == 2){
            print_seating_area();
        }
    }
}   


}

答案1

得分: 1

这打印出刚刚创建的新剧院对象的座位布局。当然,座位都是空的。

public static void print_seating_area() {
    Theatre myObjTheatre = new Theatre();

要修复这个问题,你真的应该使buyTicket()printSeating()不是静态的,这样它们可以直接在剧院对象上操作。然后在main上调用这些方法,使用你创建的对象一次。

public static void main(String[] args) {
   Theatre myObjTheatre = new Theatre();
   System.out.println("欢迎来到新剧院");

    while(true){
        // 等等。
    
    if(option == 1){
        myObjTheatre.buy_ticket();
    }
    else if(option == 2){
        myObjTheatre.print_seating_area();
    }
}
英文:

This prints out the seating from a new theater object that you just created. Of course the seats are all empty.

public static void print_seating_area() {
    Theatre myObjTheatre = new Theatre();

To fix this, you should really make buyTicket() and printSeating() non-static, so they operate on the Theater object directly. Then call those methods from main on an object you create once.

public static void main(String[] args) {
   Theatre myObjTheatre = new Theatre();
   System.out.println("Welcome to the New Theatre");

    while(true){
        // etc.
    
    if(option == 1){
        myObjTheatre.buy_ticket();
    }
    else if(option == 2){
        myObjTheatre.print_seating_area();
    }
}

}

huangapple
  • 本文由 发表于 2023年2月10日 05:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定