【问题标题】:Using 'goto' to move from if to else使用 'goto' 从 if 移动到 else
【发布时间】:2017-04-01 14:47:15
【问题描述】:

我知道java中没有goto,但我问一般是否有从if到else的goto的方法 示例:

public void decToAsci(String decimal) {
    String nb = "";
    int number;
    for (int i = 0; i < decimal.length(); i++) {
        Character c = decimal.charAt(i);
        if (!c.equals(' ')) {
            nb += decimal.charAt(i);
            if (i == decimal.length() - 1)
                //here i want to jump to else
                // i can do it like this :
                // System.out.print( (char) Integer.parseInt(nb) + " ");
                // but i repeat code in else here ..i just want to jump to else 
        } else {
            number = Integer.parseInt(nb);
            System.out.print((char) number + " ");
            nb = "";
            number = 0;
        }
    }
}

我试试这个:

if (!c.equals(' ')) {
    nb += decimal.charAt(i);
    if (i == decimal.length() - 1) continue label;
} else {
    label: the code in else
}

但没有工作,也没有使用中断标签。我明白继续,休息。像这样在儿子和父母之间使用吗? :

label:
for(int i=0 ; i<n ; i++)
    if (cond) break label;

我问一个可以从if跳转到else的方法(2个儿子)

(不是儿子和父母,而是在两个儿子之间跳跃)

我认为即使在 c 和 c++ 4 示例中使用 goto,我们也做不到

【问题讨论】:

  • 使用方法。从 if 块和 else 块中调用相同的方法。这就是避免代码重复的方法。您编写一个包含该代码的方法并从多个位置调用它。
  • 这是唯一的解决方案吗?
  • if(i==decimal.length()-1) continue label;更改为if(i!=decimal.length()-1) {/*do what you need*/};
  • 没用,我想你不明白我的问题。无论如何谢谢..
  • @FakhRi 如果您想编写可维护、可读的代码,那么是的,这是唯一的解决方案。如果你想写意大利面条、不可读和难以维护的代码,那么 goto 和 switch fallthrough 就是你需要的。

标签: java string break goto continue


【解决方案1】:

任何使用“goto like”行为的都是一个坏主意。人们普遍认为,类似于 goto 的控制结构会导致代码比坚持“结构化编程”控制结构更难阅读/维护。

以下是(IMO)解决问题的两种“正确方法”:

方法 #1 - 重新排列逻辑

public void decToAsci(String decimal) {
    String nb = "";
    for (int i = 0; i < decimal.length(); i++) {
        Character c = decimal.charAt(i);
        if (!c.equals(' ')) {
            nb += decimal.charAt(i);
        }
        if (c.equals(' ') || i == decimal.length() - 1) {
            int number = Integer.parseInt(nb);
            System.out.print((char) number + " ");
            nb = "";
        }
    }
}

(如果 c.equals(' ') 很昂贵,您可以使用临时变量来避免重新计算它。在这种情况下,它很便宜,并且可能会被 JIT 编译器内联并进一步优化。)

方法 #2 - 将代码重构为方法

public void decToAsci(String decimal) {
    String nb = "";
    for (int i = 0; i < decimal.length(); i++) {
        Character c = decimal.charAt(i);
        if (!c.equals(' ')) {
            nb += decimal.charAt(i);
            if (i == decimal.length() - 1)
                nb = processAsInt(nb);
        } else {
            nb = processAsInt(nb);
        }
    }
}

public String processInt(String nb) {
    int number = Integer.parseInt(nb);
    System.out.print((char) number + " ");
    return "";
}

在这种情况下,第一种方法提供了更好的代码。 (海事组织)

【讨论】:

    【解决方案2】:

    Java 没有goto。但是,如果我理解你,你可能会使用 switch,默认行为是 fall-through 所以类似于

    int number;
    for(int i=0 ; i<decimal.length() ; i++ )
    {
        Character c = decimal.charAt(i);
        switch (c) {
        default: // <-- c is not ' '
             nb += decimal.charAt(i);
             if (i != decimal.length() - 1) { 
                 break; 
             }
             // i == decimal.length() - 1, fall-through  
        case ' ': // <-- c is ' ', or it fell through
            number = Integer.parseInt(nb);
            System.out.print( (char) number + " ");
            nb = "";
            number = 0;
        }
    }
    

    【讨论】:

    • 我喜欢你的解决方案.. 非常有趣
    • 我讨厌它!如果我不得不维护它,我会更讨厌它。
    • 这至少是一个解决方案......但我们需要一个真正的解决方案来从“if”跳转到“else”
    • @FakhRi,不,你不需要那个。相信我。你需要学习使用方法。 所有有经验的程序员都同意 goto 是一团糟。这就是为什么 Java 和所有现代语言都没有 goto 的原因。
    • @FakhRi 老实说,如果你的代码依赖于一个不符合逻辑流程的奇怪跳转,那么就会出现问题
    【解决方案3】:

    我认为您没有正确考虑这一点。 if-else 语句的设计方式是不能从if 分支跳转到else 分支。 if 语句的全部意义在于明确表示“如果这是真的,那么做这个,否则就做那个”。试图通过使用肮脏的技巧来违反这一点只会导致维护问题。您的代码将变得非常难以理解。

    与其考虑如何打破 if 语句,为什么不找到解决方法呢?您似乎在这里所做的只是读取由空格分隔的整数。如果这就是您正在做的事情,为什么不在循环之前将' ' 字符附加到字符串的末尾?这将实现与i == decimal.length() - 1条件相同的事情

    decimal += " ";
    String nb = "";
    int number;
    for (int i = 0; i < decimal.length(); i++) {
        ...
    

    或者你可以使用Scanner:

    Scanner s = new Scanner(decimal);
    while (s.hasNextInt())
        System.out.print(((char)s.nextInt()) + " ");
    

    【讨论】:

    • 嗨,伙计,回答你的问题:[为什么不在循环前的字符串末尾附加一个 '' 字符?] ==> 起初,在我决定绕过它之后,我这样做了因为我忘记了很多,我会给它 4 一些朋友.. \n 扫描仪也很好的解决方案。谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    相关资源
    最近更新 更多