-1

ユーザーが必要なシート数、つまり行を入力するコードを作成します。私がプログラムを1行だけ実行したとき、それは完全に機能します。なぜ今機能しないのかわかりません。0のアイデアのように。誰かがこれを修正するのを手伝ってくれるなら、私は感謝するでしょう。

たった1行のコードは最初の行の後にあります。

注:以前にはっきりしていなかった場合、プログラムを実行すると、シートの数を要求するだけで、他のすべてがクラッシュ/機能しません。私はこれを修正する方法を真剣に知りません。

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

1 に答える 1

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が期待したものではないことがわかり、時間を節約できます。

于 2013-02-03T20:57:31.483 に答える