gpt4 book ai didi

java - 将 c++ 代码转换为 java 代码时出现 java.lang.StringIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-01 19:04:27 25 4
gpt4 key购买 nike

我正在努力解决这个问题

https://vjudge.net/problem/UVALive-6805

我找到了解决方案,但是在 C++ 中,任何人都可以帮我将其转换为 java 代码。我是编程新手

我尝试了很多解决方案,但没有一个有效。

如果可能的话,我需要帮助

我不知道 Java 中 C++ 中的 .erase 函数的等价物是什么C++ 中的 sbstr 是否提供与 java 不同的结果?

#include <iostream>
#include <string>

using namespace std;

int syllable(string word)
{
int L = word.size();
int syllable;

if (L>=7)
{
syllable = 3;
}
else if (L==6)
{
int indicator = 0;

for (int k=0; k<=L-2; k++)
{
string subword = word.substr(k, 2);
if (subword == "ng" || subword == "ny")
{
indicator++;
}
}

if (indicator == 0)
{
syllable = 3;
}
else
{
syllable = 2;
}
}
else if (L == 4 || L == 5)
{
syllable = 2;
}
else if (L == 3)
{
char Char = word[0];

if (Char=='a' || Char=='A' || Char=='e' || Char=='E' || Char=='i' || Char=='I' || Char=='o' || Char=='O' || Char=='u' || Char=='U')
{
syllable = 2;
}
else
{
syllable = 1;
}
}
else
{
syllable = 1;
}

return syllable;
}

int main()
{
string word;

int T;

cin >> T;

for (int i=1; i<=T; i++)
{

int syl[] = {0, -1, -2, -3};
string rhy[] = {"a", "b", "c", "d"};

int verse = 0;

int stop = 0;

while (stop == 0)
{
cin >> word;

int L = word.size();

char end = word[L-1];

if (end == '.')
{
stop = 1;
}

if (word[L-1] == ',' || word[L-1] == '.')
{
word = word.erase(L-1, 1);
L = word.size();
}

if (verse<=3)
{
syl[verse] = syl[verse] + syllable(word);
}

if (end == ',' || end == '.')
{
if (verse<=3)
{
rhy[verse] = word.substr(L-2, 2);
}

verse++;

if (verse<=3)
{
syl[verse] = 0;
}
}
}

int A = 0, B = 0, C = 0, D = 0;

for (int k=0; k<4; k++)
{
if (syl[k] >= 8 && syl[k] <= 12)
{
A = A + 10;
}
}

for (int k=0; k<2; k++)
{
if (rhy[k] == rhy[k+2])
{
B = B + 20;
}
}

for (int k=0; k<2; k++)
{
if (syl[k] == syl[k+2])
{
C = C + 10;
}
}

if (verse > 4)
{
D = (verse - 4) * 10;
}

int E = A + B + C - D;

cout << "Case #" << i << ": " << A << " " << B << " " << C << " " << D << " " << E << endl;
}
}

这是我的尝试

import java.util.*;

public class First {

public static int syllable(String word) {
int L = word.length();
int syllable;

if (L >= 7) {
syllable = 3;
} else if (L == 6) {
int indicator = 0;

for (int k = 0; k < L - 3; k++) {
String subword = word.substring(k, 2);
if (subword == "ng" || subword == "ny") {
indicator++;
}
}

if (indicator == 0) {
syllable = 3;
} else {
syllable = 2;
}
} else if (L == 4 || L == 5) {
syllable = 2;
} else if (L == 3) {
char Char = word.charAt(0);

if (Char == 'a' || Char == 'A' || Char == 'e' || Char == 'E' || Char == 'i' || Char == 'I' || Char == 'o'
|| Char == 'O' || Char == 'u' || Char == 'U') {
syllable = 2;
} else {
syllable = 1;
}
} else {
syllable = 1;
}

return syllable;
}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String word;

int T;

T = sc.nextInt();

for (int i = 1; i <= T; i++) {

int syl[] = { 0, -1, -2, -3 };
String rhy[] = { "a", "b", "c", "d" };

int verse = 0;

int stop = 0;

while (stop == 0) {

word = sc.next();

int L = word.length();

char end = word.charAt(L-1);

if (end == '.') {
stop = 1;
}

if (word.charAt(L-1) == ',' || word.charAt(L-1) == '.') {

word.substring(L-1, 1);
L = word.length();
}

if (verse <= 3) {
syl[verse] = syl[verse] + syllable(word);
}

if (end == ',' || end == '.') {
if (verse <= 3) {

rhy[verse] = word.substring(L - 2, 2);
}

verse++;

if (verse <= 3) {
syl[verse] = 0;
}
}
}

int A = 0, B = 0, C = 0, D = 0;

for (int k = 0; k < 4; k++) {
if (syl[k] >= 8 && syl[k] <= 12) {
A = A + 10;
}
}

for (int k = 0; k < 2; k++) {
if (rhy[k] == rhy[k + 2]) {
B = B + 20;
}
}

for (int k = 0; k < 2; k++) {
if (syl[k] == syl[k + 2]) {
C = C + 10;
}
}

if (verse > 4) {
D = (verse - 4) * 10;
}

int E = A + B + C - D;

System.out.println("Case #" + i + ": " + A + " " + B + " " + C + " " + D + " " + E);
}
}

}

最佳答案

第二次和第三次调用 String 子字符串方法时抛出异常。您的 beginIndex 高于您的 endIndex。正如您在这里看到的 https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int) beginIndex 始终必须低于 endIndex。

关于java - 将 c++ 代码转换为 java 代码时出现 java.lang.StringIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59585699/

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