编程新手,遇到运行时错误不知道为什么

帕加苏尔

基本上概括在标题中。https://ideone.com/E2BMS8 <--这是代码的链接。我理解如果你不想点击它,所以我也会把它粘贴在这里。只会杂乱无章。该代码应该翻转字母但将单词保持在相同的位置。不过,我想自己弄清楚那部分。只需要有关运行时错误的帮助。

import java.util.*;

class Ideone {

  public static void main (String[] args) throws java.lang.Exception {

    Scanner input = new Scanner(System.in);
    String sent, accum = "";
    char check, get;
    int len, count = 0;

    System.out.print("Please enter the sentance you want reversed: ");
    sent = input.nextLine();

    len = sent.length();

    for (int i = 0; i < len; i++) {

      check = sent.charAt(len - i);
      count += 1;

      if (check == ' ') {

        for (int p = 0; p < count; p++) {

          while (p < count) {

            get = sent.charAt(len - p);
            accum += (get + ' ');

          }
        }
      }


    }

    System.out.println("Reversed: " + accum);
  }
}
乔纳森·加涅

错误String index out of range是由于 len 比索引范围大 1 造成的。删除索引上的一个,如下所示:

import java.util.*;

public class Ideone {
    public static void main (String[] args) throws java.lang.Exception {
        Scanner input = new Scanner(System.in);
        String sent, accum = "";
        char check, get;
        int len, count = 0;
        System.out.print("Please enter the sentance you want reversed: ");
        sent = input.nextLine();
        len = sent.length();
        for (int i = 0; i < len; i++) {
            check = sent.charAt(len - i - 1);
            count += 1;
            if (check == ' ') {
                for (int p = 0; p < count; p++) {
                    get = sent.charAt(len - p - 1);
                    accum += (get + ' ');
                }
            }
        }
        System.out.println("Reversed: " + accum);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Fortran 运行时错误:列表输入中项目 1 的错误整数?不知道为什么

我是 React 的新手,我不知道为什么这不起作用

编码新手,我不知道为什么我的javascript按钮无法正常工作?

我不断遇到细分错误,不知道为什么

不知道为什么我在这里遇到细分错误

我使用 Flask 在 python 中遇到语法错误,我不知道为什么

ASP.NET的新手,我不知道为什么此@RenderPage无法正常工作

SMTP 错误,我不知道为什么

Dockerfile 错误的目录。我不知道为什么

不知道为什么我收到 StopIteration 错误

Octave 索引越界错误。不知道为什么

带有boto3的AWS Lambda Python S3,不知道为什么我遇到错误

我在 tkinter 'bad window path name .!button2' 中遇到错误,我不知道为什么?

我是python新手,使用print函数时遇到语法错误,有人知道为什么吗?

我使用 Python 制作了一个非常基本的脚本(我是新手),但我不知道为什么它不工作,因为它应该工作

我遇到内存泄漏,但我不知道为什么

运行此程序时出现“无法到达的代码错误”,但我不知道为什么

我不知道为什么我会收到 400 bad request 错误来发送不和谐的 webhook。一切似乎都很好,但我是新手

代码不起作用,我不知道为什么。制作刽子手游戏

我不知道它什么时候退出我的 for 循环,我收到运行时错误 28

我不知道为什么我收到以下错误“错误:元素类型无效:”

我不断得到细分错误:11错误,我不知道为什么... c ++

得到错误:ORA-01843: not a valid month] 错误,不知道为什么

我不知道为什么我的代码是错误的?那是什么错呢?

不知道发生了什么-没有错误(Java的新手)

Unban command JDA 4.1.1_101,无法运行,不知道为什么

Shell脚本返回“正在运行:找不到命令”。不知道为什么

运行应用程序时,应用程序崩溃。我不知道为什么?

循环不止一次运行,不知道为什么