Java数组不起作用

时间:2013-02-03 20:40:14

标签: java arrays

创建一个代码,用户输入他们想要的席位数量。当我只为一行完成程序时,它完美运行。我不知道为什么它现在不起作用。 喜欢0个想法。如果有人可以帮忙解决这个问题,我将不胜感激。

只有一行的代码在第一行之后。

注意:如果我之前不清楚,当程序运行时,它只会询问座位数量,而其他一切只是崩溃/不起作用。我真的不知道如何解决这个问题。

JavaApplication1

package javaapplication1;
import java.util.*;

/**
 *
 * @author ziyue
 */
public class JavaApplication1 {

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

        System.out.println("Please enter the desiered row, A-E. Enter capitals.");
        String ROW = input.nextLine();

         int[] r = {};        
         int[] RowA = {0,0,1,1,1,0,0,0,0,0};
         int[] RowB = {0,0,1,1,1,0,0,0,0,0};
         int[] RowC = {0,0,1,1,1,0,0,0,0,0};
         int[] RowD = {0,0,1,1,1,0,0,0,0,0};
         int[] RowE = {0,0,1,1,1,0,0,0,0,0};                           
         int highest = num - 1;         
         String available = "";
         String booking = " ";

         if ("A".equals(ROW)) {
            r = RowA;
         }
         if ("B".equals(ROW)) {
            r = RowB;
         }         
         if ("C".equals(ROW)) {
            r = RowC;
         }         
         if ("D".equals(ROW)) {
            r = RowD;
         }         
         if ("E".equals(ROW)) {
            r = RowE;
         }         

         for (int i = 0; i < r.length; i++) {
             if (r[i] == 0) {
                 available = available + (i + 1);

             }

             if (available.length() > booking.length()) {
                 booking = available;


             }else if (r[i] == 1) {
                 available = "";

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

}

JavaApplication2

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, 0, 0, 0, 1, 0, 0, 1};

        for (int i = 0; i < RowA.length; i++) {
            if (RowA[i] == 0) {
                available = available + (i + 1);

            }

            if (available.length() > booking.length()) {
                booking = available;

            } 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());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题是您的输入不正确。您假设数字和字母在不同的行上,但这不是您输入的内容。

System.out.println("Enter the amount of people in your group, up to 6");
int num = input.nextInt();
// discard the rest of the first line and 
// expect the next input to be on the next line.
input.nextLine(); 

System.out.println("Please enter the desiered row, A-E. Enter capitals.");
String ROW = input.nextLine();

如果你不放弃第一行的结尾,那么ROW将包含你在第一行的数字后输入的内容。

如果您在调试器中逐步执行代码,您会发现ROW不是您所期望的,从而节省了您的时间。