重定向错误消息的代码流
Redirecting the flow of the code for an Error Message
我想将代码流重定向到 else
语句之后的“1”部分。
我试图通过创建循环和不同的方法来实现它,但我做不到。
我想要的是,当给出无效答案时它会报错并重新提问。
import java.util.Scanner;
public class OddsAndEvens {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let’s play a game called “Odds and Evens”");
System.out.println("What is your name?");
String name = input.next();
// 1 <------------------------
System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");
String oe = input.next();
if (oe.equalsIgnoreCase("e")) {
System.out.println(name + " has picked evens! The computer will be odds.");
}
if (oe.equalsIgnoreCase("o")) {
System.out.println(name + " has picked odds! The computer will be evens.");
}
else {
System.out.println("You have typed an invalid answer.");
}
}
}
检查更新后的代码并尝试阅读有关 while 循环的内容
import java.util.Scanner;
public class OddsAndEvens {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let’s play a game called “Odds and Evens”");
System.out.println("What is your name?");
String name = input.next();
do {
System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");
String oe = input.next();
if (oe.equalsIgnoreCase("e")) {
System.out.println(name + " has picked evens! The computer will be odds.");
break; // after any valid input we will break it
} else if (oe.equalsIgnoreCase("o")) {
System.out.println(name + " has picked odds! The computer will be evens.");
break;// after any valid input we will break it
} else {
System.out.println("You have typed an invalid answer.");
}
} while (true); // this OO loop
}
}
修复很简单,见下文
import java.util.Scanner;
public class OddsAndEvens {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let’s play a game called “Odds and Evens”");
System.out.println("What is your name?");
String name = input.next();
while(true) {
// 1 <------------------------
System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");
String oe = input.next();
if (oe.equalsIgnoreCase("e")) {
System.out.println(name + " has picked evens! The computer will be odds.");
//do even logic here
break;
}
if (oe.equalsIgnoreCase("o")) {
System.out.println(name + " has picked odds! The computer will be evens.");
//do odd logic here
break;
}
else {
System.out.println("You have typed an invalid answer, lets try again");
}
}
}
}
我想将代码流重定向到 else
语句之后的“1”部分。
我试图通过创建循环和不同的方法来实现它,但我做不到。
我想要的是,当给出无效答案时它会报错并重新提问。
import java.util.Scanner;
public class OddsAndEvens {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let’s play a game called “Odds and Evens”");
System.out.println("What is your name?");
String name = input.next();
// 1 <------------------------
System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");
String oe = input.next();
if (oe.equalsIgnoreCase("e")) {
System.out.println(name + " has picked evens! The computer will be odds.");
}
if (oe.equalsIgnoreCase("o")) {
System.out.println(name + " has picked odds! The computer will be evens.");
}
else {
System.out.println("You have typed an invalid answer.");
}
}
}
检查更新后的代码并尝试阅读有关 while 循环的内容
import java.util.Scanner;
public class OddsAndEvens {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let’s play a game called “Odds and Evens”");
System.out.println("What is your name?");
String name = input.next();
do {
System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");
String oe = input.next();
if (oe.equalsIgnoreCase("e")) {
System.out.println(name + " has picked evens! The computer will be odds.");
break; // after any valid input we will break it
} else if (oe.equalsIgnoreCase("o")) {
System.out.println(name + " has picked odds! The computer will be evens.");
break;// after any valid input we will break it
} else {
System.out.println("You have typed an invalid answer.");
}
} while (true); // this OO loop
}
}
修复很简单,见下文
import java.util.Scanner;
public class OddsAndEvens {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let’s play a game called “Odds and Evens”");
System.out.println("What is your name?");
String name = input.next();
while(true) {
// 1 <------------------------
System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");
String oe = input.next();
if (oe.equalsIgnoreCase("e")) {
System.out.println(name + " has picked evens! The computer will be odds.");
//do even logic here
break;
}
if (oe.equalsIgnoreCase("o")) {
System.out.println(name + " has picked odds! The computer will be evens.");
//do odd logic here
break;
}
else {
System.out.println("You have typed an invalid answer, lets try again");
}
}
}
}