在其体内调用该方法

时间:2013-10-24 21:24:07

标签: java if-statement methods while-loop java.util.scanner

我编写了一个使用Scanner类来读取用户输入的类,它首先提供了2个选项,用户选择其中一个重复某些内容或者通过与这些选项对应的单个字符终止,然后选择一个这些选项再次询问用户是否想再次显示菜单,如果用户选择是,我必须再次调用方法才能显示选项,是否可以在其体内调用方法或者有更好的方法

public void userInput()
{
    printMessage();
    Scanner scanner = new Scanner(System.in);
    String readString = scanner.nextLine();

    while(readString != null)
    {
        if(readString.equals("A"))
        {
            System.out.println("Enter the word you want to repeate it");
            readString = scanner.nextLine();
            System.out.println(readString);
            System.out.println("Would you like to display the Options Again type yes or no");
            readString = scanner.nextLine();
            if(readString.equals("YES"))
            {
                readString =null;
                userInput();
            }
            else if(readString.equals("NO"))
            {
                readString =null;
            }
        }
        else if(readString.equals("T"))
        {
            readString =null;
        }

        else 
        {
            readString =null;
        }
    }

}

private void printMessage()
{
    System.out.println("Welcome user please select one of the following opetions:");
    System.out.println("Type A to reapeate your code");
    System.out.println("Type T to end your program");

}

0 个答案:

没有答案