你能回到代码中的特定行吗?

时间:2014-02-07 01:21:04

标签: java return

我正在尝试制作一个索引,如果有人搞砸了,我希望他们能够返回到最后一个菜单或最后一个选择屏幕,或者只是重启。这可能吗?

我正在使用JCreator。这是我的第一个程序,所以我保持它非常简单;它只是一堆if-else语句。看起来像这样的东西。

编辑:如果我在程序界面中创建了一个重启按钮或者你可以称之为什么,那会更容易吗?

public class Index
{
    public static void main(String[] args)
    {
        Scanner key = new Scanner(System.in);
        System.out.println("What kind of sandwich are you looking for?");
        System.out.println("1. Ham   2. Turkey   3. etc.");
        int meat = key.nextInt();

        if (meat == 1)
        {
            System.out.println("What type of cheese would you like"?);
            System.out.println("1. Swiss    2. Cheddar   3. etc.");
            int cheese = key.nextInt();
            if (cheese == 1)
            {
                System.out.println("Would you like lettuce and/or tomatoes?");
                System.out.println("1. Just lettuce   2. Just tomatoes   3. Both   4. Neither");
                int lOrT = key.nextInt();
            }
        }

        if (meat == 2)
        {
            System.out.println("What type of cheese would you like"?);
            System.out.println("1. Swiss    2. Cheddar   3. etc.");
            int cheese = key.nextInt();
            if (cheese == 1)
            {
                System.out.println("Would you like lettuce and/or tomatoes?");
                System.out.println("1. Just lettuce   2. Just tomatoes   3. Both   4. Neither");
                int lOrT = key.nextInt();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

这不可能像你描述的那样,但这就是方法的用武之地。

方法(在某些其他语言中的函数)是基于对象的状态在特定类中公开可重复行为的东西,该状态专门属于该对象的实例。

还有static个方法,或者更正式的类方法,它们与普通方法类似,但它们通常不依赖于对象的状态来正常运行。

在您的情况下,这意味着您的代码需要进行一些清理。将所有常见的东西移到一个方法中。


让我们从生菜和西红柿开始吧。这两个街区是完全一样的。将它们移动到一个东西,以便它们可以被引用。

public static void main(String[] args) {
    Scanner key = new Scanner(System.in);
    System.out.println("What kind of sandwich are you looking for?");
    System.out.println("1. Ham   2. Turkey   3. etc.");
    int meat = key.nextInt();

    if (meat == 1) {
        System.out.println("What type of cheese would you like?");
        System.out.println("1. Swiss    2. Cheddar   3. etc.");
        int cheese = key.nextInt();
        int lOrT = extractToppings(key, cheese);
    }

    if (meat == 2) {
        System.out.println("What type of cheese would you like?");
        System.out.println("1. Swiss    2. Cheddar   3. etc.");
        int cheese = key.nextInt();
        int lOrT = extractToppings(key, cheese);
    }
}

private static int extractToppings(Scanner key, int cheese) {
    if (cheese == 1) {
        System.out.println("Would you like lettuce and/or tomatoes?");
        System.out.println("1. Just lettuce   2. Just tomatoes   3. Both   4. Neither");
        return key.nextInt();
    }
    return -1; // sentinel return value - what to return when the if fails
}

直截了当,不是吗?让我们把它弄清楚 - 让我们做一些更多的工作来重构你的代码。

接下来就是您的if语句 - 无论是meat == 1还是meat == 2,它们都会做同样的事情。将它们分开是没有意义的。我将在这里删除if语句,然后我们到达......

public static void main(String[] args) {
    Scanner key = new Scanner(System.in);
    System.out.println("What kind of sandwich are you looking for?");
    System.out.println("1. Ham   2. Turkey   3. etc.");
    int meat = key.nextInt();

    if (meat == 1 || meat == 2) {
        System.out.println("What type of cheese would you like?");
        System.out.println("1. Swiss    2. Cheddar   3. etc.");
        int cheese = key.nextInt();
        int lOrT = extractToppings(key, cheese);
    }
}

private static int extractToppings(Scanner key, int cheese) {
    if (cheese == 1) {
        System.out.println("Would you like lettuce and/or tomatoes?");
        System.out.println("1. Just lettuce   2. Just tomatoes   3. Both   4. Neither");
        return key.nextInt();
    }
    return -1;
}

现在我们可以轻松地将其转移到自己的方法中 - 例如extractCheese。我还会在这里做一些一般性的清理工作,让你感觉更直观。

public static void main(String[] args) {
    Scanner key = new Scanner(System.in);
    System.out.println("What kind of sandwich are you looking for?");
    System.out.println("1. Ham   2. Turkey   3. etc.");
    int meat = key.nextInt();
    int cheese = extractCheese(key, meat);
    int toppings = extractToppings(key, cheese);
}

private static int extractCheese(Scanner key, int meat) {
    if (meat == 1 || meat == 2) {
        System.out.println("What type of cheese would you like?");
        System.out.println("1. Swiss    2. Cheddar   3. etc.");
        return key.nextInt();
    }
    return -1;
}

private static int extractToppings(Scanner key, int cheese) {
    if (cheese == 1) {
        System.out.println("Would you like lettuce and/or tomatoes?");
        System.out.println("1. Just lettuce   2. Just tomatoes   3. Both   4. Neither");
        return key.nextInt();
    }
    return -1;
}

你可以合法地将关于肉类的问题转移到他们自己的方法中......

public static void main(String[] args) {
    Scanner key = new Scanner(System.in);
    int meat = extractMeat(key);
    int cheese = extractCheese(key, meat);
    int toppings = extractToppings(key, cheese);
}

private static int extractMeat(Scanner key) {
    System.out.println("What kind of sandwich are you looking for?");
    System.out.println("1. Ham   2. Turkey   3. etc.");
    return key.nextInt();
}

private static int extractCheese(Scanner key, int meat) {
    if (meat == 1 || meat == 2) {
        System.out.println("What type of cheese would you like?");
        System.out.println("1. Swiss    2. Cheddar   3. etc.");
        return key.nextInt();
    }
    return -1;
}

private static int extractToppings(Scanner key, int cheese) {
    if (cheese == 1) {
        System.out.println("Would you like lettuce and/or tomatoes?");
        System.out.println("1. Just lettuce   2. Just tomatoes   3. Both   4. Neither");
        return key.nextInt();
    }
    return -1;
}

信不信由你,这很容易。

编写能够返回可重复结果的东西是直截了当的。

现在变得更加棘手 - 使用while循环来检索有效信息。我将把其他人留给你实施,但这是一般的要点

  • 获取用户的输入
  • 检查其界限
  • 告诉他们这是无效的
  • 循环并继续执行程序。

这是一个片段。

int meat ;
do{
    meat = extractMeat(key);
    if(meat < 1 || meat > 3) {
        System.out.println("Please enter valid meat.");
    }
} while (meat < 1 || meat > 3);

一旦meat在其范围内,您的程序将继续执行。我把剩下的实现作为练习留给读者。

答案 1 :(得分:0)

我会更改为循环我将您的逻辑更改为Sudo Code的方法,这在一层运行,您需要n层吗?如果是这样的话,你可以用嵌套循环来扩展这个逻辑,这样它们就可以回到最后一个选择,就像你在这里看到的另一个生菜循环一样。下面的Sudo代码:

int meatType = -1;
int cheeseType = -1;
bool canLeave = false;

do{
      meatType = MeatMethod();
      if(meatType == 1 || meatType ||2)
      {
           cheeseType = CheeseMethod();
           if(cheeseType == 1 || cheeseType== 2)
           {
               canLeave = true;
            }
      }
}while(canLeave);

答案 2 :(得分:0)

我相信你正在寻找一个goto语句,但Java没有这样的东西 - 尽管它是一个保留字。

但是,如果我理解正确,你可以通过使用循环和休息来完成你想做的事情。例如:

while (true) {
    //ask the menu questions
    System.out.println("Did you mess up? Y/N");
    // get input
    if (answer=='Y')//they did mess up, so run loop again
    {
        continue; //goes back to loop's header
    }
    else //they didn't mess up
    {
       //break out of the loop 
       break;
    }
}