首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用子字符串时的Java StringIndexOutOfBounds

使用子字符串时的Java StringIndexOutOfBounds
EN

Stack Overflow用户
提问于 2014-08-31 18:40:17
回答 2查看 201关注 0票数 0

我得到了一个StringIndexOutOfBounds错误与这个Java程序行:

代码语言:javascript
复制
String num3 = lottoString.substring(2,2);

告诉我,2超出了范围,但是这个代码应该随机选择一个三位数的彩票号码,从000到999不等。我的错误是什么?

代码语言:javascript
复制
import java.util.Scanner;
public class Lottery
{
    public static void main(String[] args)
    {
        //Declare and initialize variables and objects
        Scanner input = new Scanner(System.in);
        String lottoString = "";

        //Generate a 3-digit "lottery" number composed of random numbers
        //Simulate a lottery by drawing one number at a time and 
        //concatenating it to the string
        //Identify the repeated steps and use a for loop structure
        for(int randomGen=0; randomGen < 3; randomGen++){
            int lotNums = (int)(Math.random()*10);
            lottoString = Integer.toString(lotNums);
        }

        String num1 = lottoString.substring(0,0);
        String num2 = lottoString.substring(1,1);
        String num3 = lottoString.substring(2,2);

        String num12 = num1 + num2;
        String num23 = num2 + num3;
        String num123 = num1 + num2 + num3;

        //Input: Ask user to guess 3 digit number
        System.out.println("Please enter your three numbers (e.g. 123): ");
        String userGuess = input.next();

        //Compare the user's guess to the lottery number and report results
        if(userGuess.equals(num123)){
            System.out.println("Winner: " + num123);
            System.out.println("Congratulations, both pairs matched!");
        }else if(userGuess.substring(0,2).equals(num12)){
            System.out.println("Winner: " + num123);
            System.out.println("Congratulations, the front pair matched!");
        }else if(userGuess.substring(1,3).equals(num23)){
            System.out.println("Winner: " + num123);
            System.out.println("Congratulations, the end pair matched!");
        }else{
            System.out.println("Winner: " + num123);
            System.out.println("Sorry, no matches! You only had one chance out of 100 to win anyway.");
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-31 18:53:44

正如在另一个答案中提到的,每次迭代循环时,都会将lottoString的值重置为一位数。你需要加在上面,就像这样:

代码语言:javascript
复制
lottoString += Integer.toString(lotNums);

另一个问题是使用substring方法。如果两个索引位置相同,如0,0,则返回一个空字符串。你想要的是:

代码语言:javascript
复制
String num1 = lottoString.substring(0,1);
String num2 = lottoString.substring(1,2);
String num3 = lottoString.substring(2,3);
票数 1
EN

Stack Overflow用户

发布于 2014-08-31 18:45:28

代码语言:javascript
复制
for(int randomGen=0; randomGen < 3; randomGen++){
    int lotNums = (int)(Math.random()*10);
    lottoString = Integer.toString(lotNums);
}

您将Integer.toString()的结果分配给lottoString。lotNums是介于0到9之间的一个数字。

我想你想

代码语言:javascript
复制
lottoString += Integer.toString(lotNums);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25595097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档