需要帮助通过测试岩石纸剪刀游戏

时间:2017-05-16 03:46:41

标签: java testing assertions

char getInput(String prompt, char[] options, Scanner sc) {
   // getInput method
   // prompts user for an input that matches one of the given characters
   // if its not one of those, repeat. (use contains (above))
   char ch;
   boolean con = false;
   String printStr = prompt + " (";
   for (int i = 0; i < options.length; i++) {
       printStr += " " + options[i] + ",";
   }
   printStr = printStr.substring(0, printStr.length() - 1) + " ):";

   do {
       ch = sc.next().charAt(0);
       System.out.println(printStr);
       if (contains(ch, options)) {
           con = true;
       }
   }
   while (!con);
   return ch;
}

我已经制作了上面的代码......但它显示了这个错误...我必须通过测试而不更改测试代码

Exception in thread "main"` `java.lang.Asse`rtionError: 26 13
Doug ( v ):
Doug ( v ):
Doug ( v ):

at RPSTester.assertOutput(RPSTester.java:227)
at RPSTester.testGetInput(RPSTester.java:88)
at RPSTester.main(RPSTester.java:16)

测试代码:

void testGetInput() {
      OutputStream out;

      out = resetSystemOut();
      assert 'y' == RPS.getInput("Choose", new char[] {'y','n','q'}, new Scanner("y\n"));
      assertOutput("Choose ( y, n, q ):\n", out);
      out = resetSystemOut();
      assert 'n' == RPS.getInput("Alice", new char[] {'y','n','q'}, new Scanner("n\n"));
      assertOutput("Alice ( y, n, q ):\n", out);
      out = resetSystemOut();
      assert 'q' == RPS.getInput("Bob", new char[] {'y','n','q'}, new Scanner("q\n"));
      assertOutput("Bob ( y, n, q ):\n", out);
      out = resetSystemOut();
      assert 'q' == RPS.getInput("Cloe", new char[] {'y','n','q'}, new Scanner("x\nw\nq\n"));
      assertOutput("Cloe ( y, n, q ):\n" +
         "Cloe ( y, n, q ):\n" +
         "Cloe ( y, n, q ):\n", out);
      out = resetSystemOut();
      assert 'v' == RPS.getInput("Doug", new char[] {'v'}, new Scanner("vvvv\nv\n"));
      assertOutput("Doug ( v ):\n" +
         "Doug ( v ):\n", out);
   }

0 个答案:

没有答案