2013-02-03 146 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

相关问题