英文:
how to repeat the loop statement by having a conditional value in java?
问题
以下是翻译后的代码部分:
System.out.println(" ****按0开始****");
int only0 = sc.nextInt(); //获取用户输入值
if (only0 == 0) {
System.out.println(" 欢迎您 :-)");
} else {
System.out.println(" 请按照要求输入0 :-)");
}
英文:
What i want to do is, taking input value in variable only0, and the value must be only 0, if the user inputs value 0, then it must show ("we welcome you") line and if not then repeat the same question
System.out.println(" ****Press 0 to start****");
int only0 = sc.nextInt(); //taking input of the user
if (only0 == 0) {
System.out.println(" We Welcome You :-)");
} else {
System.out.println(" YOU ARE REQUESTED TO PRESS ONLY 0 :-)");
}// if end
答案1
得分: 1
我喜欢假设用户并不总是输入整数,所以我将其加载为字符串,然后解析为整数以处理异常情况。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
int only0 = 0;
try {
only0 = Integer.valueOf(scan.nextLine());
} catch (NumberFormatException e) {
System.out.println("只能输入整数!请重试。");
continue;
}
if (only0 == 0) {
System.out.println("欢迎您。");
break;
} else {
System.out.println("您未输入 0!");
continue;
}
}
scan.close();
}
英文:
I like to assume the user is not always going to enter an integer so I load it as a string and then parse to an int to handle off cases.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
int only0 = 0;
try {
only0 = Integer.valueOf(scan.nextLine());
} catch (NumberFormatException e) {
System.out.println("Only enter an integer! Try again.");
continue;
}
if (only0 == 0) {
System.out.println("We welcome you.");
break;
} else {
System.out.println("You did not enter 0!");
continue;
}
}
scan.close();
}
答案2
得分: 1
很遗憾,Java 并不支持这样的操作(我想你在谈论 Goto 语句)。然而,你仍然可以通过递归模拟而不使用循环语句:
import java.util.Scanner;
public class Test{
Scanner input = new Scanner(System.in);
public static void main(String args[]){
Test run = new Test();
run.ask();
}
private void ask(){
System.out.print("Press 0 to start");
if(input.nextInt()==0){
System.out.println(" We Welcome You :-)");
}
else{
System.out.println(" YOU ARE REQUESTED TO PRESS ONLY 0 :-)");
ask();
}
}
}
英文:
Unfortunately Java doesn't do that(I suppose you are talking about Goto Statement). However, you can still simulate with recursion without looping statement:
import java.util.Scanner;
public class Test{
Scanner input = new Scanner(System.in);
public static void main(String args[]){
Test run = new Test();
run.ask();
}
private void ask(){
System.out.print("Press 0 to start");
if(input.nextInt()==0){
System.out.println(" We Welcome You :-)");
}
else{
System.out.println(" YOU ARE REQUESTED TO PRESS ONLY 0 :-)");
ask();
}
}
}
答案3
得分: 0
在输入无效时,您需要循环以提示用户重新输入值。其中一种最好的方法是使用 `do-while` 循环,如下所示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int only0;
boolean valid;
do {
// 假设输入将是有效的。如果输入无效,在有效变量中赋值为 false,这将强制循环再次运行
valid = true;
System.out.print("请输入零:");
try {
// 使用 Scanner#nextLine 来消耗换行符
only0 = Integer.parseInt(input.nextLine());
if (only0 == 0) {
System.out.println("欢迎您 :-)");
} else {
System.out.println("您只能按 0 :-)");
valid = false;
}
} catch (NumberFormatException e) {
System.out.println("这不是一个数字。");
valid = false;
}
} while (!valid);
}
}
**一个示例运行:**
请输入零:1
您只能按 0 :-)
请输入零:a
这不是一个数字。
请输入零:0
欢迎您 :-)
英文:
In case of invalid input, you need to loop back to prompt the user to enter the value again. One of the best ways to do it is by using a do-while
loop as shown below:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int only0;
boolean valid;
do {
// Start with assuming that the input will be valid. In case of invalid input,
// assign false to valid which will force the loop to run again
valid = true;
System.out.print("Enter zero: ");
try {
// Use Scanner#nextLine to consume Enter as well
only0 = Integer.parseInt(input.nextLine());
if (only0 == 0) {
System.out.println(" We Welcome You :-)");
} else {
System.out.println(" YOU ARE REQUESTED TO PRESS ONLY 0 :-)");
valid = false;
}
} catch (NumberFormatException e) {
System.out.println("This is not a number.");
valid = false;
}
} while (!valid);
}
}
A sample run:
Enter zero: 1
YOU ARE REQUESTED TO PRESS ONLY 0 :-)
Enter zero: a
This is not a number.
Enter zero: 0
We Welcome You :-)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论