Java 如何循环以下代码?
我对java真的很陌生,我找不到解决这个问题的方法。我想做一个程序,告诉你一个数字是正的还是负的,不管它是int还是double。但在程序执行后,我希望它循环并再次请求用户输入,反复执行代码,只要有用户输入。我可以用java来做吗Java 如何循环以下代码?,java,loops,if-statement,Java,Loops,If Statement,我对java真的很陌生,我找不到解决这个问题的方法。我想做一个程序,告诉你一个数字是正的还是负的,不管它是int还是double。但在程序执行后,我希望它循环并再次请求用户输入,反复执行代码,只要有用户输入。我可以用java来做吗 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String userInput = "Input your number: ";
System.out.println(userInput);
if (in.hasNextInt()) {
int z = in.nextInt();
if (z > 0) {
System.out.println(z + " is positive.");
} else if (z < 0) {
System.out.println(z + " is negative.");
} else {
System.out.println(z + " is equal to 0.");
}
} else if (in.hasNextDouble()) {
double x = in.nextDouble();
if (x > 0) {
System.out.println(x + " is positive.");
} else if (x < 0) {
System.out.println(x + " is negative.");
} else {
System.out.println(x + " is equal to 0.");
}
} else {
System.out.println("Hey! Only numbers!");
}
}
}
使用这个代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Console console = new Console();
while(true) {
// Take your input
Scanner in = new Scanner(System.in);
String userInput = "Input your number: ";
System.out.println(userInput);
if (in.hasNextInt()) {
int z = in.nextInt();
if (z > 0) {
System.out.println(z + " is positive.");
} else if (z < 0) {
System.out.println(z + " is negative.");
} else {
System.out.println(z + " is equal to 0.");
}
} else if (in.hasNextDouble()) {
double x = in.nextDouble();
if (x > 0) {
System.out.println(x + " is positive.");
} else if (x < 0) {
System.out.println(x + " is negative.");
} else {
System.out.println(x + " is equal to 0.");
}
} else {
System.out.println("Hey! Only numbers!");
}
// Ask for exit
System.out.print("Want to quit? Y/N")
String input = console.readLine();
if("Y".equals(input))
{
break;
}
}
}
}
这是一个很好的开始,让您了解模式匹配在Java中可以做什么,并且可以通过对详尽的数据点进行测试来改进它 这还展示了如何使用while循环、重载方法和三元运算符,而不是嵌套的if-then-else 在学习过程中,还应该使用编辑器的调试功能,并使用system.out.println来理解代码的作用 当用户按下“仅输入空字符串”时,我正在结束程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
String userInput = "Input your number: ";
System.out.print(userInput);
String input = scanner.nextLine();
// look for integer (+ve, -ve or 0)
if (input.matches("^-?[0-9]+$")) {
int z = Integer.parseInt(input);
System.out.println(display(z));
// look for double (+ve, -ve or 0)
} else if (input.matches("^-?([0-9]+\\.[0-9]+|[0-9]+)$")) {
double z = Double.parseDouble(input);
System.out.println(display(z));
// look for end of program by user
} else if (input.equals("")) {
System.out.println("Good Bye!!");
break;
// look for bad input
} else {
System.out.println("Hey! Only numbers!");
}
}
scanner.close();
}
// handle integer and display message appropriately
private static String display(int d) {
return (d>0) ? (d + " is positive") : (d<0) ? (d + " is negative") : (d + " is equal to 0");
}
// handle double and display message appropriately
private static String display(double d) {
return (d>0) ? (d + " is positive") : (d<0) ? (d + " is negative") : (d + " is equal to 0");
}
}
最有可能的是,只要有用户输入,你的意思是什么?程序如何知道何时停止?请尝试这样做:while i=System.in.read!=使用while循环,询问用户是否想要输入一个数字。他在java和编程语言方面似乎很新@对于堆栈溢出,没有任何解释的仅RC代码答案通常不被认为是可接受的。谢谢告诉我:但他可能会理解这个密码。我能写得比这好多了。但他也在学习,在一个例子中不能再高了。即使代码好得多,我还是会对它投反对票。任何堆栈溢出的答案都需要某种解释。我在代码中做了两条注释。如果代码有效,他可能会工作并关注一些新东西。总之,我只是想帮忙。此行有多个标记-控制台无法解析为类型-控制台无法解析我在第5行遇到此错误。谢谢!它起作用了。即使代码在这一点上对我来说太复杂了,我也会一步一步来看看它为什么能工作。非常感谢。
Input your number: 0
0 is equal to 0
Input your number: 0.0
0.0 is equal to 0
Input your number: -0
0 is equal to 0
Input your number: -0.0
-0.0 is equal to 0
Input your number: 12
12 is positive
Input your number: -12
-12 is negative
Input your number: 12.0
12.0 is positive
Input your number: -12.0
-12.0 is negative
Input your number: 12-12
Hey! Only numbers!
Input your number: ---12
Hey! Only numbers!
Input your number: