如何循环此WHOLE程序?
public class Experiments {
public static void main(String[] args) {
int n;
System.out.print("Please enter a number:");
n = TextIO.getlnInt();
while (n<3) {
System.out.print("Please enter another number:");
n = TextIO.getlnInt();
}
while (n<=5) {
System.out.println("Number entered should be more than 5.");
System.out.println("Please re-enter:");
n=TextIO.getlnInt();
}
if (n%2==0) {
n=n*2+1;
}
else {
n=n*2;
}
System.out.println("The result is "+n +".");
}
}
打印出结果时:
结果是14。
我想要它说:
有乐趣吗?让我们再做一次。
请输入一个数字:
或
有乐趣吗?想再做一次。按y / n。
请输入一个数字:
答案 0 :(得分:1)
你必须做这样的事情:
Scanner s = new Scanner(System.in);
char c;
do {
your_code
c = s.next().charAt(0);
} while(c == 'y');
由于这个原因,如果你想继续,当你进入&#39; y&#39;然后它会再次循环。否则它将退出循环。
答案 1 :(得分:1)
将您的代码与wawek中的示例结合使用:
public class Experiments {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
do {
play();
System.out.println("Again ( press y ) else press Anykey");
c = s.next().charAt(0);
} while(c == 'y');
}
public static play(){
int n;
System.out.print("Please enter a number:");
n = TextIO.getlnInt();
while (n<3) {
System.out.print("Please enter another number:");
n = TextIO.getlnInt();
}
while (n<=5) {
System.out.println("Number entered should be more than 5.");
System.out.println("Please re-enter:");
n=TextIO.getlnInt();
}
if (n%2==0) {
n=n*2+1;
}
else {
n=n*2;
}
System.out.println("The result is "+n +".");
}
}
答案 2 :(得分:1)
public class Experiments {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char c;
int n;
do{
System.out.print("Please enter a number:");
n = TextIO.getlnInt();
while (n<3) {
System.out.print("Please enter another number:");
n = TextIO.getlnInt();
}
while (n<=5) {
System.out.println("Number entered should be more than 5.");
System.out.println("Please re-enter:");
n=TextIO.getlnInt();
}
if (n%2==0) {
n=n*2+1;
}
else {
n=n*2;
}
System.out.println("The result is "+n +".");
System.out.println("Had fun? press y do it again n to exit");
c = s.next().charAt(0);
} while(c == 'y');
}
归功于@wawek
答案 3 :(得分:0)
您可以使用递归。样本如下:
public class Experiments {
public static void main(String[] args) {
guess();
}
public static void guess() {
int n;
System.out.print("Please enter a number:");
n = TextIO.getlnInt();
while (n<3) {
System.out.print("Please enter another number:");
n = TextIO.getlnInt();
}
while (n<=5) {
System.out.println("Number entered should be more than 5.");
System.out.println("Please re-enter:");
n=TextIO.getlnInt();
}
if (n%2==0) {
n=n*2+1;
}
else {
n=n*2;
}
System.out.println("The result is "+n +".");
System.out.println("Had fun? Lets do it again.");
guess();
}
}
答案 4 :(得分:0)
只需读取用户的下一行字符为yes,然后将整个序列作为shwon运行。
public class Experiments {
public static void main(String[] args) {
int n;
while(againFlag=='y'){
System.out.print("Please enter a number:");
n = TextIO.getlnInt();
while (n<3) {
System.out.print("Please enter another number:");
n = TextIO.getlnInt();
}
while (n<=5) {
System.out.println("Number entered should be more than 5.");
System.out.println("Please re-enter:");
n=TextIO.getlnInt();
}
if (n%2==0) {
n=n*2+1;
}
else {
n=n*2;
}
System.out.println("The result is "+n +".");
System.out.println("Had fun. Want do it again?(Press 'y')");
char againFlag = TextIO.getlnChar();
}
}