Java 中的“类、接口或枚举预期错误”[重复]

问题描述 投票:0回答:1

这是我遇到问题的代码:

public class StringTest1
{
    
    {
            
        // change these assignment statements to the test case values
        
        String str = "12xy34";
        String word = "xy";


        System.out.println(plusOut(str,word));
    }

}

public static String plusOut(String str, String word)
{
    //create a new string variable
    String output  = "";
    
    // write your code here
    str.indexOf(word);
    String len = word.length();
    for (int i = 0; i < str; i++){
        if (int i != word){
            output.i += "+";
            i++;
        }
        else (int i == str.indexOf(word, i)){
            output.i += word;
            i = i + len;
        }
        str.indexOf(word);
    }
    // return the new string
    return output;
}

继续:代码基本上应该用“+”替换所有非单词字母, 例如:如果 str = "12xy34" 且 word = "xy",则输出应为 "++xy++"。

上面的代码是我迄今为止尝试过的,但我什至无法测试它,因为我收到多个错误,提示“类、接口或枚举预期错误”。我检查了我的花括号,但找不到错误。如果有人可以帮助解决这个问题或告诉我我的主要代码是否有问题,我将不胜感激...... 谢谢

java methods while-loop indexof
1个回答
0
投票

你需要main方法。你把它纳入你的班级了吗? main 函数是类的入口点。这是将运行的第一个函数。您的 { } 位置也错误。我对它进行了一些修复,以便您继续解决您的问题。

public class StringTest1
{

public static void main(String[] args) {
    // change these assignment statements to the test case values
    String str = "12xy34";
    String word = "xy";
    
    System.out.println(plusOut(str,word));
 }    

 private static String plusOut(String str, String word)
 {
    //create a new string variable
    String output  = "";

    // write your code here
    int index = str.indexOf(word);
    int length = word.length();
    
    for (int i = 0; i < length; i ++) {
        
    }
    return output;
 }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.