java - 字符串索引超出范围异常错误
问题描述
我正在为我的作业编写代码,但我收到此异常错误:
线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:9 在 java.lang.String.charAt(未知来源)
这是代码:
import java.util.*;
public class ISBN10 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int d10=0;
System.out.print("Enter the first 9 digits of an ISBN: ");
String n = in.nextLine();
if(n.length()!=9)
{
System.out.println("You need to enter exactly 9 digits!");
}
else
for(int i = 0; i < n.length(); i++)
{
for(int j = 1; j < 10; j++)
{
d10 = ((n.charAt(i)*j)+n.charAt(i+1))%11;
}
}
if(d10 == 10)
{
System.out.println("The ISBN-10 number is " + n + "X");
}
else
{
System.out.println("The ISBN-10 number is " + n);
}
}
}
谢谢!
解决方案
推荐阅读
- machine-learning - 如何构建像 Kaggle 这样的数据科学游戏
- firebase - 尝试将颤振与firebase消息ios集成时出现白屏
- sql-server - 像 `User::filePath` 这样的变量如何传递给由 SQL 代理作业运行的 SSIS 包中的脚本?
- c# - 如何修复命名空间“WebApplication1”中不存在类型或命名空间“App_Code”
- javascript - 尽管导航到多个 html/js 文件,但当我的站点加载并获取任何传入消息时,我可以使用 javascript 连接到 MQTT 代理吗?
- rest - 不向客户端返回任何内容的 POST 方法是否需要 Accept 标头?
- spring-boot-actuator - 如何在 SpringBoot Admin UI 中启用关闭功能?
- tableau-api - 如何合并 2 个参数值以过滤表格中的字段?
- go - Golang Alloc 和 HeapAlloc vs pprof 差异较大
- dask - 如何在 DASK 中批量调度 dask_jobqueue 作业而不是并发?