使用私有静态布尔值 - java

时间:2014-04-23 13:04:01

标签: static boolean private void

看到这是一个大学任务,我不确定我能给出多少细节。 但是,我会分享我认为我能做到的事情。

在我的主要方法中,我有这段代码;

System.out.println ("Please enter a year?");
int yearMenu = scan.nextInt();
System.out.println ("Please enter number of questions?");
int questionMenu = scan.nextInt();
confirmSessionDetails(yearMenu, questionMenu);

这是我的私有静态布尔'confirmSessionDetails'

private static boolean confirmSessionDetails(int year, int questions)
{
String yearName = " ";
switch (year) {
    case 1: yearName = "Year 1"; break; 
    case 2: yearName = "Year 2"; break;  
    default: yearName = "error"; break;
}

String questionNumber = " ";
    switch (questions) {
    case 1: questionNumber = "10"; break; 
    case 2: questionNumber = "20"; break; 
    default: questionNumber = "error"; break;
    }

System.out.print ("You are a " + yearName + " student and want to do " + questionNumber + " questions. Is this correct (Y/N)?");
             correctDetail = scan.next();

        if (correctDetail.equalsIgnoreCase ("y"))
    {
        return true;
    }
    else
    {
        return false;
    }

所以我想知道的是如何获取返回值并在我的main方法中使用它。 如果返回值为true,则应转到'private static void displayQuestions()'

如果返回值为false,则应循环回询问年份和问题数量。

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

bool myVariable = confirmSessionDetails(yearMenu, questionMenu);

这将分配函数confirmSessionDetails(yearMenu,questionMenu)的返回值,然后您可以在代码中使用myVariable。

答案 1 :(得分:0)

我愿意: 主

int yearMenu, questionMenu;

do{
   System.out.println ("Please enter a year?");
   yearMenu = scan.nextInt();
   System.out.println ("Please enter number of questions?");
   questionMenu = scan.nextInt();
} while (confirmSessionDetails(yearMenu, questionMenu);

displayQuestions();

希望它有所帮助。