Seeing as this is for a university assignment, I'm not sure how much detail I can give. However I'll share what I believe I can.

In my main method I have this code;

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);

and this is my private static boolean '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;
    }

So what I want to know is how to get the return value and use it in my main method. If return value is true it should move on to 'private static void displayQuestions()'

and if the return value is false it should loop back to asking the year and number of questions.

有帮助吗?

解决方案

try something like this:

bool myVariable = confirmSessionDetails(yearMenu, questionMenu);

That will assign the returned value of the function confirmSessionDetails(yearMenu, questionMenu) and then you can use myVariable later on in your code.

其他提示

I would do like : Main

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();

Hope it helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top