请求嵌套的If-Else检查

时间:2017-12-15 02:42:52

标签: nested

我的任务是编写一个遵循以下模式的嵌套分支:

        * if numberCups is greater 1 AND less than or equal to MAX_CUPS:
        *   print numberCups + " is a valid number of cups!"
        *
        * else:
        *   print numberCups + " is a not valid number of cups!"
        *   print "Please enter another number of cups between 1 and 100: "
        *   numberCups = scnr.nextInt();
        *
        *   if numberCups is greater 1 AND less than or equal to MAX_CUPS:
        *       print numberCups + " is a valid number of cups!"
        *
        *   else if numberCups < 1:
        *       print numberCups + "is less than 1. Sorry you are out of" 
        *       attempts."
        * 
        *
        *   else
        *       print numberCups + "is greater than 100. Sorry you are out  of attempts."

到目前为止,这是我的代码:

if (numberCups > 1 && numberCups <= MAX_CUPS) {
           System.out.println(numberCups + " is a valid number of cups!");
       }
       else {
           System.out.println(numberCups + " is a not valid number of cups!");
       }

       System.out.println("Please enter another number of cups between 1 and 100: ");
          numberCups = scnr.nextInt();

          if (numberCups > 1 && numberCups <= MAX_CUPS) {
              System.out.println(numberCups + " is a valid number of cups!");
          }
          else if (numberCups < 1) {
              System.out.println(numberCups + "is less than 1. Sorry you are out of attempts");
          }
          else {
              System.out.println(numberCups + "is greater than 100. Sorry you are out of attempts");
          }

有人可以快速浏览一下这段代码并告诉我它是否符合要求以及我上面是否有错误的内容?我对这一切都很陌生,并希望得到推荐和检查。谢谢!

0 个答案:

没有答案