【问题标题】:Working with private static boolean - java使用私有静态布尔值 - java
【发布时间】:2014-06-08 08:08:07
【问题描述】:

鉴于这是大学作业,我不确定我能提供多少细节。 不过我会分享我认为我能做到的。

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

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 方法中使用它。 如果返回值为真,它应该转到'private static void displayQuestions()'

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

【问题讨论】:

  • 您使用的是什么特定语言?
  • 您好。我正在使用 Java
  • 好的,你现在有几个答案要看看,看看它们是否有帮助

标签: static boolean private void


【解决方案1】:

试试这样的:

bool myVariable = confirmSessionDetails(yearMenu, questionMenu);

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

【讨论】:

  • 如果这有帮助,请考虑标记为答案
【解决方案2】:

我想: 主要

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

希望对你有帮助。

【讨论】:

  • 谢谢山姆。也很有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 2020-11-12
  • 2013-12-17
  • 2017-10-13
  • 2015-12-15
  • 1970-01-01
  • 2022-11-20
相关资源
最近更新 更多