gpt4 book ai didi

java - 为什么我的程序不断给我一个 ArrayIndexOutOfBoundsException 即使我没有

转载 作者:行者123 更新时间:2023-12-01 09:15:11 25 4
gpt4 key购买 nike

我最近试图构建一个程序,它接受两个输入并检查它们在其他碱基中是否同等表示(碱基最多为 20)。但我在第 28 行不断遇到索引越界异常...该怎么办?

例如:12(基数 10) = 5(基数 3) [两者在各自的基数中均表示为“12”。]

import java.util.Scanner;
import java.util.Arrays;
class Bases
{

public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Two Numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Thank You for inputting the numbers!");

String basea[] = new String[20];
String baseb[] = new String[20];

int i=0 , j=0;
for( i=0;i<20;i++)
{
basea[i] = convert(a,i+1);
baseb[i] = convert(b,i+1);
}

for(i=0;i<=19;i++)
{
for(j=0;i<=19;j++)
{
if(basea[i].equals(baseb[j]))
{//this is where the exception keeps popping

break ;
}
}
}
if(i!=20){
if(i==0){
i=9;
System.out.println(a+"(base "+(i+1)+") ="+b+"(base "+(j+1)+")");
}
else
System.out.println(a+"(base "+(i+1)+") ="+b+"(base "+(j+1)+")");
}
else System.out.println("Numbers dont match at all till base 20!!");
}

private static String convert(int number,int base)
{

return Integer.toString(number,base);
}

}

最佳答案

for(j=0;i<=19;j++)

上面的循环应该是 j <= 19

for(j=0;j<=19;j++)

关于java - 为什么我的程序不断给我一个 ArrayIndexOutOfBoundsException 即使我没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40606264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com