我想在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.");
}
}
}
语句之后将代码流重定向到“ 1”部分。
我试图通过创建循环和其他方法来实现它,但是我做不到。
我想要的是,当给出无效答案时,它将给出一个错误,并再次询问问题。
$(function() {
$("#sortable1, #sortable2").sortable({
connectWith: ".connectedSortable",
placeholder: "sortable-placeholder",
out: function(e, ui) {
$(".sortable-placeholder").remove();
}
}).disableSelection();
});
答案 0 :(得分:0)
检查更新的代码,然后尝试阅读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
}
}
答案 1 :(得分:0)
修复很简单,请参见下文
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");
}
}
}
}