首页 > 解决方案 > 如何将数组内的数据更新为假而不是真?

问题描述

这个程序是为三个不同的游戏预留时间。到目前为止,在程序中,我已经打印了两个显示游戏的不同菜单,另一个显示可用时间槽。该程序设置为无限循环,因此其他用户也有机会选择游戏并保留时间。这就是我的问题所在,程序第二次运行它仍然显示之前选择的时间。

import java.util.Scanner;

public class GameReservationSystem {

    public static Scanner input = new Scanner(System.in);
    static String[] timeSlots = {"1-2pm", "2-3pm", "3-4pm", "4-5pm"};
    static String[] Games = {"Backganmon", "Chess", "Dominoes"};
    static boolean avaliableTime[][] = new boolean[Games.length][timeSlots.length];
    static int userChoice;

    public static void main(String[] args) {
        for (int i = 0; i < Games.length; i++) {
            for (int j = 0; j < timeSlots.length; j++) {
                avaliableTime[i][j] = true;
            }
        }

        mainMenu();

    }

    public static void mainMenu() {

        while (true) {
            characterPrint('-');
            for (int i = 0; i < Games.length; i++) {
                System.out.print(i + ". ");
                System.out.println(Games[i]);
            }
            characterPrint('-');
            System.out.println("input a number to choose your game!");
            userChoice = input.nextInt();

            reserveMenu(userChoice);
        }

    }

    public static void reserveMenu(int userChoice) {
        int reserveTime = 0;
        if (userChoice >= 0 && userChoice < Games.length) {

            characterPrint('-');
            System.out.println("This are the avaliable times to play" + Games[userChoice]); 
            for (int i = 0; i < timeSlots.length; i++) {
                System.out.print(i + ". ");
                System.out.println(timeSlots[i]);
            }
            characterPrint('-');
            System.out.println("Input a number to reserve a time: ");
            reserveTime = input.nextInt();

        }
        if (reserveTime >= 0 && reserveTime < timeSlots.length) {

            avaliableTime[userChoice][reserveTime] = false;
            System.out.println("You have succesfully reserve a time to play " + Games[userChoice] + " at" + timeSlots[reserveTime]);
        }

    }

    public static void characterPrint(char c) {
        for (int i = 0; i < 30; i++) {
            System.out.print(c);
        }
        System.out.println();
    }
}

标签: javabooleanupdates

解决方案


实际上,您的问题不在于您没有将表中的布尔值从 true 更改为 false。相反,您的问题是您在预订之前没有使用表格中的信息。您所做的是始终将该值设置为 false 并打印“成功保留”,而不检查该时间是否已被其他人保留。

试试下面的代码。我还添加了一些代码来处理无效的输入号码。顺便说一句,“avaliableTime”看起来像一个错字,所以我把它改成了“availableTime”。

import java.util.Scanner;

public class GameReservationSystem {
    public static Scanner input = new Scanner(System.in);
    static String[] timeSlots = {"1-2pm", "2-3pm", "3-4pm", "4-5pm"};
    static String[] Games = {"Backganmon", "Chess", "Dominoes"};
    static boolean availableTime[][] = new boolean[Games.length][timeSlots.length];
    static int userChoice;

    public static void main(String[] args) {
        for (int i = 0; i < Games.length; i++) {
            for (int j = 0; j < timeSlots.length; j++) {
                availableTime[i][j] = true;
            }
        }

        mainMenu();

    }

    public static void mainMenu() {

        while (true) {
            characterPrint('-');
            for (int i = 0; i < Games.length; i++) {
                System.out.print(i + ". ");
                System.out.println(Games[i]);
            }
            characterPrint('-');
            System.out.println("input a number to choose your game!");
            userChoice = input.nextInt();

            reserveMenu(userChoice);
        }

    }

    public static void reserveMenu(int userChoice) {
        int reserveTime = 0;
        if (userChoice >= 0 && userChoice < Games.length) {

            characterPrint('-');
            System.out.println("This are the avaliable times to play" + Games[userChoice]); 
            for (int i = 0; i < timeSlots.length; i++) {
                if (availableTime[userChoice][i]) {
                    //if false, this time is not available and should not be displayed.
                    System.out.print(i + ". ");
                    System.out.println(timeSlots[i]);
                }
            }
            characterPrint('-');
            System.out.println("Input a number to reserve a time: ");
            reserveTime = input.nextInt();

        }else {
            //handle unexpected game number.
            System.out.println("Invalid game number, please try again."); 
        }

        if (reserveTime >= 0 && reserveTime < timeSlots.length) {
            //what if someone typed 0 although 0 wasn't displayed? (0 wasn't displayed, for example, because it has been reserved by someone else.)
            //So you should check whether it is available by the following line.
            if (availableTime[userChoice][reserveTime]) {
                //if true, this time hasn't been reserved, so it is a valid input.
                availableTime[userChoice][reserveTime] = false;
                System.out.println("You have succesfully reserve a time to play " + Games[userChoice] + " at" + timeSlots[reserveTime]);
            }else {
                //if already false, this time has already been reserved by someone else,
                //thus it is an invalid input.
                System.out.println("This time has already been reserved, please try again.");
            }
        }else {
            System.out.println("Invalid time number, please try again."); 
        }

    }

    public static void characterPrint(char c) {
        for (int i = 0; i < 30; i++) {
            System.out.print(c);
        }
        System.out.println();
    }
}

推荐阅读