0

How do I go about writing a loop that's supposed to be boolean, yet the answer can be an integer using JOptionPane?

  • boolean promptMenu( int menu )
    • This will represent the core of your code.
    • Should be in the body of a loop inside main().
    • Returns true if it should continue running.
    • Returns false if it is time to quit.
    • Notice that promptMenu takes in an int parameter: 0 - Prints the Main Menu.

So far this is what I got:

import javax.swing.JOptionPane;

public class BankSystem {

    //Fields
    static boolean question = true;
    static String q ;
    static int qt;

    //Methods
    public static void main(String[]args)
    {
        while(question = true)
        {
            promptMenu (qt) ;
        }
    }

    static int promptMenu( int qt )
    {
        q = JOptionPane.showInputDialog ("Gen's Bank" + "\n \n Print main menu? 0-> YES\n\n") ;
        qt = Integer.parseInt(q);

        if (qt != 0)
        {
            question = false;
        }

        return (qt);
    }

}

If you press anything that isn't 0 it still loops. Any Suggestions would help.

4

2 回答 2

2

仔细阅读问题。该问题要求您提供一种方法:

boolean promptMenu( int menu )

你写的是另一种方法:

int promptMenu( int menu )

您的讲师希望您编写一个返回 aboolean的方法,但您正在编写一个返回 a 的方法int。你没有回答被问到的问题。

要返回boolean您需要:

return true;

return false;

或类似的东西:

boolean boolVar;
boolVar = // Your code here.
return boolVar;
于 2011-08-27T13:30:03.677 回答
0

你有什么:

public static void main(String[]args)
{
    while(question = true)
    {
        promptMenu (qt) ;
    }
}

你需要什么:

public static void main(String[]args)
{
    while(question == true)
    {
        promptMenu (qt) ;
    }
}

如果要检查条件,则必须使用以下参数之一(<、<=、>、>=、!=、==)。一个=用于为变量赋值。

于 2011-08-27T11:40:51.273 回答