技术:程序未按预期工作(例外,数组)

时间:2014-12-01 21:07:05

标签: java exception error-handling arrays

我正在制作一个作为调度程序的程序。它工作不正常。我想要三件事:

1)程序一直询问,直到数组约会时没有任何空(null)元素。

2)如果输入的时间不在1到6 [包括]之间,则抛出相应的错误。

3)如果已经预留了一个时隙,则抛出另一个错误。

以下是代码:用于Scheduler类

    import java.util.Scanner;
    import java.lang.*;
    import java.io.*;
    import java.util.Arrays;

    public class Scheduler {

    /**
     * Writes a program that allows students to schedule appointments at either
     * 1, 2, 3, 4, 5, or 6 o’clock p. m.
     * 
     * @param args
     */
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);

        String[] appointmentTime = new String[6];

        int time;
        String name;
        boolean Continue;
        int temp;

        try {
            do {

                System.out.println("\nEnter your name");
                name = kbd.next();

                System.out.println("Which time slot would you like to chose?");
                time = kbd.nextInt();

                if (appointmentTime[time - 1] == null && time < 0 && time > 6) {
                    appointmentTime[time - 1] = name;
                    System.out.println(time + " is added to slot "
                            + appointmentTime[time]);

                } else if (appointmentTime[time - 1] != null) {
                    throw new TimeInUseException();

                } else if (time < 0 && time > 7) {
                    throw new InvalidTimeException();
                }

            } while (appointmentTime[time - 1] == null);

            // Final stage: printing
            System.out.println("Schedule:");
            for (int i = 0; i < appointmentTime.length; i++) {

                System.out.println((i + 1) + "PM: \t"
                        + appointmentTime[i].toString());

            }

        } catch (InvalidTimeException e) {
            System.out.println(e.getMessage());
            System.out.println("Enter another time");
        } catch (TimeInUseException ex) {
            System.out.println(ex.getMessage());
            System.out.println("Enter another time");
        } catch (ArrayIndexOutOfBoundsException ex1) {
            System.out.println(ex1.getMessage());
        } catch (NullPointerException ex2) {
            System.out.println(ex2.getMessage());
        }

    }

}

输出:

Enter your name
1
Which time slot would you like to chose?
1

Enter your name
2
Which time slot would you like to chose?
2

Enter your name
3
Which time slot would you like to chose?
3

Enter your name
4
Which time slot would you like to chose?
4

Enter your name
5
Which time slot would you like to chose?
5

Enter your name
6
Which time slot would you like to chose?
6

Enter your name
7
Which time slot would you like to chose?
7
6

它不会在6

停止

2 个答案:

答案 0 :(得分:1)

将您的if条件更改为

if (appointmentTime[time - 1] == null && time > 0 && time < 7)

答案 1 :(得分:0)

为了使它工作,我改变了循环的条件。检查以下代码:

package assignment9;

import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.util.Arrays;

public class Scheduler {
static Scanner kbd = new Scanner(System.in);

/**
 * Writes a program that allows students to schedule appointments at either
 * 1, 2, 3, 4, 5, or 6 o’clock p. m.
 * 
 * @param args
 */
public static void main(String[] args) {

    String[] appointmentTime = new String[7];

    int time;
    String name;
    int temp = 1;

    try {
        do {

            System.out.println("\nEnter your name");
            name = kbd.next();

            System.out.println("Which time slot would you like to chose?");
            time = kbd.nextInt();

            if (appointmentTime[time] == null && time > 0 && time < 7) {
                appointmentTime[time] = name;
                System.out.println(time + " is added to slot "
                        + appointmentTime[time]);

            } else if (appointmentTime[time] != null) {
                throw new TimeInUseException();

            } else if (time < 0 || time > 7) {
                throw new InvalidTimeException();
            }
            temp++;
        } while (temp < 7);

        // Final stage: printing
        System.out.println("Schedule:");
        for (int i = 1; i < 7; i++) {
        System.out.println((i) + "PM: \t"
        + appointmentTime[i].toString());}

    } catch (InvalidTimeException e) {
        System.out.println(e.getMessage());
        System.out.println("Enter another time");
    } catch (TimeInUseException ex) {
        System.out.println(ex.getMessage());
        System.out.println("Enter another time");
    } catch (ArrayIndexOutOfBoundsException ex1) {
        System.out.println(ex1.getMessage());
    } catch (NullPointerException ex2) {
        System.out.println(ex2.getMessage());
    }

}

}