2013-04-09 42 views
0

這裏是我的元胞自動機的代碼我的工作:Java:指標正在發生什麼?

public class Life1D { 

    private Rule rule; 
    private int stepCount; 

    public static void main (String [ ] args) { 
      Life1D simulation = new Life1D (); 
      simulation.processArgs (args); 
      simulation.producePBM (); 
    } 

    // Print, in Portable Bitmap format, the image corresponding to the rule and step count 
    // specified on the command line. 
    public void producePBM () { 
      int width = (stepCount*2+1); 
      System.out.println("P1 " + width + " " + (stepCount+1)); 
      String prev_string = ""; 
      // constructs dummy first line of rule 
      for (int i = 0; i < width; i++){ 
        if (i == stepCount+1){ 
          prev_string += "1"; 
        } else { 
          prev_string += "0"; 
        } 
      } 
      // contructs and prints out all lines prescribed by the rule, including the first 
      for (int i = 0; i < stepCount; i++) { 
        String next_string = ""; 
        for (int j = 0; j < width; j++) { 
         // prints next line, one character at a time 

          System.out.print(prev_string.charAt(j) + " "); 
          // specifies cases for the edges as well as for normal inputs to Rule 
          if (j == 0) { 
            next_string += rule.output(0, prev_string.charAt(0), prev_string.charAt(1)); 
          } else if (j == width-1) { 
            next_string += rule.output(prev_string.charAt(width-2), prev_string.charAt(width-1), 0); 
          } else { 
            String rule_input = prev_string.substring(j-1, j+2); 
            int first = rule_input.charAt(0); 
            int second = rule_input.charAt(1); 
            int third = rule_input.charAt(2); 
            next_string += rule.output(first, second, third); 
          } 
        } 
        // sets prev_string to next_string so that string will be the next string in line to be printed 
        prev_string = next_string; 
        System.out.println(); 
      } 
    } 


    // Retrieve the command-line arguments, and convert them to values for the rule number 
    // and the timestep count. 
    private void processArgs (String [ ] args) { 
      if (args.length != 2) { 
        System.err.println ("Usage: java Life1D rule# rowcount"); 
        System.exit (1); 
      } 
      try { 
        rule = new Rule (Integer.parseInt (args[0])); 
      } catch (Exception ex) { 
        System.err.println ("The first argument must specify a rule number."); 
        System.exit (1); 
      } 
      try { 
        stepCount = Integer.parseInt (args[1]); 
      } catch (Exception ex) { 
        System.err.println ("The second argument must specify the number of lines in the output."); 
        System.exit (1); 
      } 
      if (stepCount < 1) { 
        System.err.println ("The number of output lines must be a positive number."); 
        System.exit (1); 
      } 
    } 
} 

class Rule { 

    private int a, b, c; 
    private String rulebin; 

    public Rule (int ruleNum) { 
      rulebin = Integer.toBinaryString(ruleNum); 
    } 

    // Return the output that this rule prescribes for the given input. 
    // a, b, and c are each either 1 or 0; 4*a+2*b+c is the input for the rule. 
    public int output (int a, int b, int c) { 
     return rulebin.charAt(7 - 4*a + 2*b + c); 
    } 
} 

以下是錯誤消息當我運行它:

P1 7 4 
0 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index  out of range: 151 
    at java.lang.String.charAt(String.java:686) 
    at Rule.output(Life1D.java:90) 
    at Life1D.producePBM(Life1D.java:35) 
    at Life1D.main(Life1D.java:9) 

什麼鬼?爲什麼我得到這個錯誤,我該如何解決它?我一直在努力尋找幾個小時的錯誤,如果我能得到幫助,這將是一件幸事。

+0

什麼是線35和90 life1D?因爲我不會計算代碼行.. :) – Elior 2013-04-09 08:01:56

+0

以及你對這個項目有什麼論點? – Elior 2013-04-09 08:30:20

+0

我更新了我的答案,看看並閱讀我的評論 – Elior 2013-04-09 16:44:06

回答

0

在要轉換爲二進制字符串整數這個特殊的部分:

rulebin = Integer.toBinaryString(ruleNum); 

現在讓我們假設你的參數是:
第一個參數= 12
第二個參數=任意數量的

現在,當這個代碼將其轉換成數字二進制字符串,那麼你將得到:
rulebin =「1100」(長度4)

現在,在這個功能:

public int output (int a, int b, int c) { 
    return rulebin.charAt(7 - 4*a + 2*b + c); 
} 

當A = B = C = 0,那麼這一功能將試圖訪問你的「rulebin的八字」,但你rulebin的長度爲4。這就是爲什麼你得到字符串索引出界的例外。

注:我不知道你是否已經把任何限制你的輸入參數,但是這可能是一個潛在的問題。

0

不!問題是,你傳遞字符不是int來

public int output (int a, int b, int c) { 
    return rulebin.charAt(7 - 4*a + 2*b + c); 
} 

我嘗試過了,當prevString.charAt(0)和prevString.charAt(1)的0將其發送到輸出方法的參數( 0,48,48)(嘗試調試它,你會) 這會導致索引超出範圍!

,也是皈依到二進制字符串不返回7位數字格式..

UPDATE:

public class Lif1ID { 

private Rule rule; 
private int stepCount; 

public static void main (String [ ] args) { 
     Lif1ID simulation = new Lif1ID (); 
     simulation.processArgs (args); 
     simulation.producePBM (); 
} 

// Print, in Portable Bitmap format, the image corresponding to the rule and step count 
// specified on the command line. 
public void producePBM () { 
     int width = (stepCount*2+1); 
     System.out.println("P1 " + width + " " + (stepCount+1)); 
     String prev_string = ""; 
     // constructs dummy first line of rule 
     for (int i = 0; i < width; i++){ 
       if (i == stepCount+1){ 
         prev_string += "1"; 
       } else { 
         prev_string += "0"; 
       } 
     } 
     // contructs and prints out all lines prescribed by the rule, including the first 
     for (int i = 0; i < stepCount; i++) { 
       String next_string = ""; 
       for (int j = 0; j < width; j++) { 
        // prints next line, one character at a time 

         System.out.print(prev_string.charAt(j) + " "); 
         // specifies cases for the edges as well as for normal inputs to Rule 
         if (j == 0) { 
// take a look at the 'getNumericValue' Method.. in your version it didn't pass 0 or 1, now it does.. 
           next_string += rule.output(0, Character.getNumericValue(prev_string.charAt(0)), Character.getNumericValue(prev_string.charAt(1))); 
         } else if (j == width-1) { 
           next_string += rule.output(prev_string.charAt(width-2), prev_string.charAt(width-1), 0); 
         } else { 
           String rule_input = prev_string.substring(j-1, j+2); 
           int first = Character.getNumericValue(rule_input.charAt(0)); 
           int second = Character.getNumericValue(rule_input.charAt(1)); 
           int third = Character.getNumericValue(rule_input.charAt(2)); 
           next_string += rule.output(first, second, third); 
         } 
       } 
       // sets prev_string to next_string so that string will be the next string in line to be printed 
       prev_string = next_string; 
       System.out.println(); 
     } 
} 


// Retrieve the command-line arguments, and convert them to values for the rule number 
// and the timestep count. 
private void processArgs (String [ ] args) { 
     if (args.length != 2) { 
       System.err.println ("Usage: java Life1D rule# rowcount"); 
       System.exit (1); 
     } 
     try { 
       rule = new Rule (Integer.parseInt(args[0])); 
     } catch (Exception ex) { 
       System.err.println ("The first argument must specify a rule number."); 
       System.exit (1); 
     } 
     try { 
       stepCount = Integer.parseInt (args[1]); 
     } catch (Exception ex) { 
       System.err.println ("The second argument must specify the number of lines in the output."); 
       System.exit (1); 
     } 
     if (stepCount < 1) { 
       System.err.println ("The number of output lines must be a positive number."); 
       System.exit (1); 
     } 
    } 
} 

class Rule { 

private int a, b, c; 
private String rulebin; 

public Rule (int ruleNum) { 
     rulebin = convertToBinary(ruleNum); 
} 

private String convertToBinary(int input) // get the binary presentation as you want 
{           // if the input is 2 you'll get "00000010" 
    String binary = ""; 
    for (int i = 0; i < 8; i++){ 
     if ((1 << i & input) != 0) 
     binary += "1"; 
     else 
      binary+= "0"; 
    } 
    binary = new StringBuffer(binary).reverse().toString(); 
    return binary; 
} 

// Return the output that this rule prescribes for the given input. 
// a, b, and c are each either 1 or 0; 4*a+2*b+c is the input for the rule. 
public char output (int a, int b, int c) { // here you want to return a char, no? 
    return rulebin.charAt(7 - 4*a + 2*b + c); // there is a problem with your formula 
} 

}

+0

@John Cheung我更新了我的答案.. – Elior 2013-04-09 16:45:08