为什么我的代码永无休止?我删除了for循环

时间:2019-03-24 01:40:02

标签: java time infinite

在我运行一次代码期间,它陷入了无限循环。我正在使用repl.it,所以当我单击“运行”时,什么都没有出现。

我之前有一个for循环,但是即使删除了它,问题仍然存在。到目前为止,我已经输入了名称,并创建了bo_bfo_fmo_m变量。

import java.util.Scanner;

public class Main {
    static String input;

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What is your name? ");
        String input = keyboard.nextLine();
        input = input.toLowerCase();
        System.out.println("Input: " + input);
        String name = Song(input);
        System.out.println(name);
    }

    public static String Song(String str) {
        String bo_b = "";
        String fo_f = "";
        String mo_m = "";
        int a = 0;
        /* for (int i = 0; i < str.length(); i++) {
            a += i;
            i = 0;
            int i = 0; */
            System.out.println(str);
            System.out.println("a: " + a + "; i: " + i);
            if (!(Character.isLetter(str.charAt(i)))) {
                System.out.println("Only letters are alowed");
                System.exit(0);
            } else if (((str.substring(0, 1)).equals("a") || (str.substring(0, 1)).equals("e") || (str.substring(0, 1)).equals("i") || (str.substring(0, 1)).equals("o") || (str.substring(0, 1)).equals("u"))) {
                break;
            } else {
                str = str.substring(a + 1);
                System.out.println("AFTER: " + str);
                a -= 1;
            }
            bo_b = "Bo-B" + str;
            fo_f = "Fo-F" + str;
            mo_m = "Mo-M" + str;
            str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
            String result = new String(str + ", " + str + " " + bo_b + ", " + "Bonana Fanna " + fo_f + ", " + "Fe fi "
                    + mo_m + ", " + str);
            return result;
        }
    }
}

很抱歉冒充我的所有代码,因为我不知道错误来自哪里。

我希望代码检查字符串开头的字符。请注意,我还没有完成这个项目。该代码的目的是接受名称输入并将其插入Shirley Ellis的“名称游戏”(Bonana,fanna等)中。

1 个答案:

答案 0 :(得分:-1)

public class NameGame {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is your name? ");
    String input = keyboard.nextLine();
    input = input.toLowerCase();
    System.out.println("Input: " + input);
    String name = Song(input);
    System.out.println(name);
}

static String Song(String str) {
    String bo_b = "";
    String fo_f = "";
    String mo_m = "";
    int a = 0;
    for(int i =0;i<str.length();i++) {
        a += i;
        System.out.println(str);
        System.out.println("a: " + a + "; i: " + i);
        if (!(Character.isLetter(str.charAt(i)))) {
            System.out.println("Only letters are allowed");
            System.exit(0);
        } else if ((str.substring(0, 1)).equals("a") ||
                (str.substring(0, 1)).equals("e") ||
                (str.substring(0, 1)).equals("i") ||
                (str.substring(0, 1)).equals("o") ||
                (str.substring(0, 1)).equals("u")) {
            break;
        } else{
            str = str.substring(a + 1);
            System.out.println("AFTER: " + str);
            a -= 1;
        }
        bo_b = "Bo-B" + str;
        fo_f = "Fo-F" + str;
        mo_m = "Mo-M" + str;
        str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }
    return str + ", " + str + " " + bo_b + ", " + "Bonana Fanna " + fo_f + ", " + "Fe fi " + mo_m + ", " + str;
}

}

一件事是您在for循环中执行此操作

i = 0;
int i = 0;

这只会将循环重置为i = 0,从而导致无限循环。我已经改变了。更改后的代码有效。但是,我不确定您要达到什么目标。