1

该程序在int数组不需要数到 10 时有效。我的问题是将charat10 读取为两个不同的字符,它们是。我怎样才能为 10 破例?例如下面的程序,当你输入 5 个人时,程序从 6-1 打印出来,因为 1 是从 10 的第一个字符读取的。当你输入 6 时,charat是读取 0,所以它打印出来6-0。

package javaapplication2;

import java.util.*;

public class JavaApplication2 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the amount of people in your group, up to 6");
        int num = input.nextInt();

        if (num > 6) {
            System.out.println("You have exceeded the maximum amount of people allowed.");
        }

        int highest = num - 1;
        String available = "";
        String booking = " ";
        int[] RowA = {0, 0, 1, 1, 1, 0, 0, 0, 0, 0};

        for (int i = 0; i < RowA.length; i++) {
            if (RowA[i] == 0) {
                available = available + (i + 1);
            }
            if (available.length() > booking.length()) {
                booking = available;
                System.out.println(booking);
            } else if (RowA[i] == 1) {
                available = "";
            }
        }


        if (num <= booking.length()) {
            char low = booking.charAt(0);
            char high = booking.charAt(highest);
            System.out.println("There are seats from " + low + " - " + high + ".");
        } else {
            System.out.println("The desired seat amount is not available. The maximum amount on Row is " + booking.length());
        }
    }
}
4

2 回答 2

1

如果您使用的是输入字符串:

由于 booking 是一个字符串,因此使用子字符串将 low 和 high 的值捕获为整数会更方便。例如像这样:

if (num <= booking.length()) {
    int lineIndex = booking.indexOf("-");
    if (lineIndex < 0)
        lineIndex = booking.indexOf("/");

    int low = Integer.parseInt(booking.substring(0, lineIndex);
    int high = Integer.parseInt(booking.substring(lineIndex+1, booking.length());
}

System.out.println() 将以它们现在的形式正确解析整数。额外的检查是为了防止您使用的分隔符号不仅仅是“-”。

于 2013-02-03T21:32:47.100 回答
0

所以你正在做的是一种叫做原始痴迷的代码味道。您正在使用字符串,而应该使用更复杂的数据类型,例如集合或数组:

import java.util.*;

public class JavaApplication2 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the amount of people in your group, up to 6");
        int num = input.nextInt();

        if (num > 6) {
            System.out.println("You have exceeded the maximum amount of people allowed.");
        }

        int highest = num - 1;
        ArrayList<Integer> available = new ArrayList<Integer>();
        Integer[] booking = new Integer[0];
        int[] RowA = {0, 0, 1, 1, 1, 0, 0, 0, 0, 0};

        for (int i = 0; i < RowA.length; i++) {
            if (RowA[i] == 0) {
                available.add(i + 1);
            }
            if (available.size() > booking.length) {
                booking = available.toArray(booking);
                System.out.println(Arrays.toString(booking));
            } else if (RowA[i] == 1) {
                available.clear();
            }
        }


        if (num <= booking.length) {
            int low = booking[0];
            int high = booking[highest];
            System.out.println("There are seats from " + low + " - " + high + ".");
        } else {
            System.out.println("The desired seat amount is not available. The maximum amount on Row is " + booking.length);
        }
    }
}

使用集合和数组意味着您不必担心charAt小数位数,因为您实际上是在操作intInteger类型 - 这将自行跟踪它们的小数。

于 2013-02-03T21:48:32.233 回答