为什么此代码中存在运行时错误?

时间:2016-04-15 02:07:45

标签: java string runtime substring

我对40个字符串的penney游戏以运行时错误结束,有人可以帮我理解原因吗?代码只需要告诉字符串中的组合数。

以下是一个例子:

输入:

Sub AddLineToSQL(sLine As String)

    frmSQL.txtSQL.Value = frmSQL.txtSQL.Value & sLine & vbCr

End Sub

输出:

4                                 1                        
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH                       2      
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT                              
3                              
HHTTTHHTTTHTHHTHHTTHTTTHHHTHTTHTTHTTTHTH                             
4                  HTHTHHHTHHHTHTHHHHTTTHTTTTTHHTTTTHTHHHHT

这是我的代码:

1 0 0 0 0 0 0 0 38                  2 38 0 0 0 0 0 0 0        
3 4 7 6 4 7 4 5 1                   4 6 3 4 5 3 6 5 6

错误:

  

运行时错误时间:0.1内存:320576信号:-1

1 个答案:

答案 0 :(得分:0)

以下是您的计划的简化版本。现在显示正在输入的字符串前面的迭代编号:

import java.util.*;
import java.lang.*;
import java.io.*;

public class HelloWorld{

     public static void main(String []args) throws java.lang.Exception{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Please enter number of iterations : ");
        int t = Integer.parseInt(br.readLine());
        for(int counter=1; counter<= t;  counter++)
        {
            int c []={0,0,0,0,0,0,0,0};
            String arr [] = {"TTT","TTH","THT","THH","HTT","HTH","HHT","HHH"};
            System.out.print(counter + " ");
            String str = br.readLine();

            for(int i=0; i<str.length()-3; i++)
            {
                String str1 = str.substring(i,i+3);

                for(int j=0; j<8; j++)
                {
                    if(str1.equals(arr[j]))
                    {
                        c[j]++;
                        break;
                    }   
                }
            }

            for(int i=0;i<8;i++){
                System.out.print(" "+c[i]);
            }
            System.out.println();
        }
     }
}

它正在发挥作用。