如何在Java中返回类的开头?

时间:2015-03-23 12:45:33

标签: java

这就是我正在做的事情:我一直在用Java编写一个程序,我还有最后通to,我不能做一件事,回到开头代码 - 我总是在这里或那里结束语法错误。

我不是专业人士,如果这是一个简单的问题,请不要讨厌我!

这是我的代码:

import java.util.Scanner;

public class Name {
    public static void main(String args[])  {
        Scanner keyboard = new Scanner(System.in);
        int Name;
        int Confirmation = 5; 


    System.out.print("What Game Would You Like To Buy? Type '9' for Help. Type '0' To View The Selection Of Games Available: ");
    System.out.println("");
    Name = keyboard.nextInt();

    if (Name == 0) {
        System.out.println("Fantasy World (0.99). Type '1' to Purchase.");
        System.out.println("Sir Wags A Lot (0.99). Type '2' to Purchase.");
        System.out.println("Take a Path (1.99). Type '3' to Purchase.");
        // (plus a few more calls to println)
            } 
    if (Name == 9) {
        System.out.println("Help:"
                + "All games are corresponded to a single integer ranging from 1 to 8. Type 0 in the main bar too see the range of games."
                + "Too see this help message, type 9."
                + "For more information on each game, type the number followed by that number again. For example, I would type 22 too see more information on Sir Wags A Lot.");
    }

    if (Name == 1) {
        System.out.println("Fantasy World is £0.99, are you sure you want to pay? Type '1' for Yes or '0' for No: ");
        Confirmation = keyboard.nextInt();
    }
        if (Confirmation == 1) {
            System.out.print("Thank You For Purchasing!");

        }
        if (Confirmation == 0) {
            System.out.print("Ok, Come Again!");
        }
    if (Name == 11) {
        System.out.println("Fantasy World:");
        System.out.println("Title: Fantasy World");
        System.out.println("Genre: RPG");
        System.out.println("Description: Where you can live your dreams");
        System.out.println("Price: £0.99");

        }
    if (Name == 22) {
        System.out.println("Sir Wags A Lot:");
        // (plus more println calls)
        }
    if (Name == 33) {
        System.out.println("Take a Path:");
         // (plus more println calls)
        }
    if (Name == 44) {
        System.out.println("River Clear Up:");
        // (plus more println calls)
        }
    if (Name == 55) {
        System.out.println("PinBall:");
        // (plus more println calls)
        }
    if (Name == 66) {
        System.out.println("Ghost Girl:");
        // (plus more println calls)
        }
    if (Name == 77) {
        System.out.println("Dress Up:");
        //(plus more println calls)
        }
    if (Name == 88) {
        System.out.println("Where Is My Hat?:");
        //(plus more println calls)
        }
    }

}

所以基本上,变量是用户输入一个双位整数来激活底部子类。说我想了解更多有关幻想世界':

的信息

我在输入控制台中键入11,它显示文本 - 也许我想购买游戏但很明显,我不想重新启动整个程序 - 我可以让它更容易在每个子类的底部添加一些代码,将其再次返回到顶部的整数输入?

我尽可能地尝试解释我的情况。

3 个答案:

答案 0 :(得分:1)

您基本上只需要将代码放在do-while循环中。你可以阅读它here。程序将继续运行,直到while语句的条件为真。

答案 1 :(得分:0)

你可以添加一个循环 - 一个do-while或while-将永远运行(while(true))。在条件中添加另一个输入类型,即退出代码。如果退出代码为99,那么当用户输入99时,您可以return;,这将结束您的程序。

答案 2 :(得分:0)

这是最快的解决方法:

  • 将主要功能重命名为programLoop
  • 创建一个新的主要功能:

代码:

public static void main(String args[])  {
  while(true) {
    programLoop(args);
  }
}

正确的解决方法是阅读循环的工作原理,以便了解程序的流程。