2013-02-03 148 views
1

int數組不需要計數到10時。我的問題是charat讀數爲10,因爲它們是兩個不同的字符。我如何能夠爲10例外?例如下面的程序,當您鍵入5個人時,程序從6-1打印出來,因爲1正在從第一個字符10開始讀取。當輸入6時,charat正在讀取0,因此它會打印6-0。Java將10讀爲2個字符,如何更改此

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

變量'booking'代表什麼?這聽起來像你正在嘗試使用字符串的字符作爲數字。也許你應該使用'int []'而不是String。 –

+0

您應該重新考慮您設計代碼的方式。我不會使用String作爲集合,因爲您會遇到各種問題。我還會檢查你的「預訂」循環工作,因爲我可以看到它的一些問題。如果你不知道你想添加多少元素,你可以使用'int []',但是'List '會更簡單。 –

回答

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類型 - 它們會自己跟蹤它們的小數位數。

1

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

由於預訂是一個字符串,它是更方便地捕捉低和高值利用的子整數。例如像這樣:

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()將以現在的形式解析正確的整數。額外的檢查是爲了防止使用更多的分隔符號而不僅僅是' - '。

+0

謝謝你,你忘了一些括號壽,但謝謝youuu –

+0

等一秒netbeans說它找不到符號int –

+0

int是一個原始數據類型,它應該被netbeans識別。整數是一個以大寫字母開頭的對象。難道你不小心鍵入int大寫(詮釋)? – bas

相關問題