编辑:由于我的程序仍然无法正常工作,我发布了整个方法以防万一有其他问题。
如果用户输入' n'我想退出此程序。提示时:
char again = 'y';
while (again == 'y' || again == 'Y')
{
String ans = IBIO.inputString ("Unscramble: OBORSLW (Hint: Shellder latches onto its tail.) ");
int tries = 0;
while (!ans.toLowerCase ().equals ("slowbro"))
{
System.out.println ("Incorrect. Wrong answer. Try again.");
tries++;
ans = IBIO.inputString ("\nUnscramble: OBORSLW (Hint: Shellder latches onto its tail.) ");
if (tries > 3)
{
System.out.println ("The correct answer was SLOWBRO.");
again = IBIO.inputChar ("Play again? (y/n) ");
break;
}
}
System.out.println ("Correct.");
System.out.println ("\nQuestion #2 - ");
String ans2 = IBIO.inputString ("\nUnscramble: RVLEGERA (Hint: It rolls down slopes without slowing down.) ");
int tries2 = 0;
while (!ans2.toLowerCase ().equals ("graveler"))
{
System.out.println ("Incorrect. Wrong answer. Try again.");
tries2++;
ans2 = IBIO.inputString ("\nUnscramble: RVLEGERA (Hint: It rolls down slopes without slowing down.) ");
if (tries2 > 3)
{
System.out.println ("The correct answer was GRAVELER.");
again = IBIO.inputChar ("Play again? (y/n) ");
if (again != 'y' || again != 'Y')
break;
}
}
System.out.println ("Correct.");
System.out.println ("\nQuestion #3 -");
String ans3 = IBIO.inputString ("\nUnscramble: TYSGLA (Hint: It's almost invisible and is gaseous.) ");
int tries3 = 0;
while (!ans3.toLowerCase ().equals ("gastly"))
{
System.out.println ("Incorrect. Wrong answer. Try again.");
tries3++;
ans3 = IBIO.inputString ("\nUnscramble: TYSGLA (Hint: It's almost invisible and is gaseous.) ");
if (tries3 > 3)
{
System.out.println ("The correct answer was GASTLY.");
again = IBIO.inputChar ("Play again? (y/n) ");
if (again != 'y' || again != 'Y')
break;
}
}
printSlow ("Correct.");
printSlow ("\nWell Done, " +name+ "! You have passed the test! I'm so happy for you!!");
break;
}
}
每当我故意多次输入错误值时,我会得到正确答案,如果我愿意,可以再试一次。这部分有效。但是,如果我不想继续,该程序将自行继续并继续下一个问题。我怎样才能停止整个程序?
答案 0 :(得分:0)
char again = 'y';
while (again == 'y' || again == 'Y')
{
String ans = IBIO.inputString ("Unscramble: OBORSLW (Hint: Shelder latches onto its tail.) ");
int tries = 0;
boolean flag= false;
while (!ans.toLowerCase ().equals ("slowbro"))
{
System.out.println ("Incorrect. Wrong answer. Try again.");
tries++;
ans = IBIO.inputString ("\nUnscramble: OBORSLW (Hint: Shelder latches onto its tail.) ");
if (tries > 3)
{
System.out.println ("The correct answer was SLOWBRO.");
again = IBIO.inputChar ("Play again? (y/n) ");
flag = true;
break;
}
}
if(flag==true){
break;
}
你的意思是这个?
答案 1 :(得分:0)
编辑:我修改了代码以满足您的要求。请检查一下。
原文:我想首先你必须在这里写出你的IBIO
课程,以便人们能够帮助你调试。
我已在网上找到IBIO
课并测试了你的课程,但它已正确执行。
以下是我的代码。
Main.java:
public class Main {
private static boolean verifyAnswer(String candidate, String answer) {
if (candidate.toLowerCase().equals(answer))
return true;
else {
System.out.println("Incorrect. Wrong answer. Try again.");
return false;
}
}
private static boolean playGame(int questionNumber, String answer, String question) {
String candidate;
System.out.println("Question #" + questionNumber + " - ");
int tries = 0;
do {
tries++;
if (tries > 3) {
System.out.println("The correct answer was " + answer + ".");
char again = IBIO.inputChar("Play again? (y/n) ");
if (again != 'y' && again != 'Y')
System.exit(0);
else
return false;
}
candidate = IBIO.inputString(question);
} while (!verifyAnswer(candidate, answer));
System.out.println("Correct.");
return true;
}
public static void main(String[] args) {
while (!playGame(1, "slowbro", "Unscramble: OBORSLW (Hint: Shellder latches onto its tail.) ")) ;
while (!playGame(2, "graveler", "Unscramble: RVLEGERA (Hint: It rolls down slopes without slowing down.) ")) ;
while (!playGame(3, "gastly", "Unscramble: TYSGLA (Hint: It's almost invisible and is gaseous.) ")) ;
}
}
IBIO.java:
//---- IBIO - Below are simplified input and output methods ----
//===========================================================
// IBIO Standard Input and Output
// These methods must be copied into your program(s).
//===========================================================
public class IBIO {
static void output(String info) {
System.out.println(info);
}
static void output(char info) {
System.out.println(info);
}
static void output(byte info) {
System.out.println(info);
}
static void output(int info) {
System.out.println(info);
}
static void output(long info) {
System.out.println(info);
}
static void output(double info) {
System.out.println(info);
}
static void output(boolean info) {
System.out.println(info);
}
static String input(String prompt) {
String inputLine = "";
System.out.print(prompt);
try {
inputLine = (new java.io.BufferedReader(
new java.io.InputStreamReader(System.in))).readLine();
} catch (Exception e) {
String err = e.toString();
System.out.println(err);
inputLine = "";
}
return inputLine;
}
static String inputString(String prompt) {
return input(prompt);
}
static String input() {
return input("");
}
static int inputInt() {
return inputInt("");
}
static double inputDouble() {
return inputDouble("");
}
static char inputChar(String prompt) {
char result = (char) 0;
try {
result = input(prompt).charAt(0);
} catch (Exception e) {
result = (char) 0;
}
return result;
}
static byte inputByte(String prompt) {
byte result = 0;
try {
result = Byte.valueOf(input(prompt).trim()).byteValue();
} catch (Exception e) {
result = 0;
}
return result;
}
static int inputInt(String prompt) {
int result = 0;
try {
result = Integer.valueOf(
input(prompt).trim()).intValue();
} catch (Exception e) {
result = 0;
}
return result;
}
static long inputLong(String prompt) {
long result = 0;
try {
result = Long.valueOf(input(prompt).trim()).longValue();
} catch (Exception e) {
result = 0;
}
return result;
}
static double inputDouble(String prompt) {
double result = 0;
try {
result = Double.valueOf(
input(prompt).trim()).doubleValue();
} catch (Exception e) {
result = 0;
}
return result;
}
static boolean inputBoolean(String prompt) {
boolean result = false;
try {
result = Boolean.valueOf(
input(prompt).trim()).booleanValue();
} catch (Exception e) {
result = false;
}
return result;
}
//=========== end IBIO ===========================================//
}
我的环境:Intellij社区2016.1.1 + jdk 8 + Windows 7 x64