[英]How do I make it so my code continuously asks parts 1., 2., and 3. and Choice:? After a third attempt the scanner just closes
import java.lang.Math;
import java.util.Scanner;
public class Temperature {
static int fahrenheit;
static int celsius;
public static int Celsius(int fahrenheit) {
fahrenheit = (int) (5.0 / 9.0 * (fahrenheit - 32));
return fahrenheit;
}
public static int Fahrenheit(int fahrenheit) {
fahrenheit = (int) (9.0 / 5.0 * celsius + 32);
return fahrenheit;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("1. Fahrenheit to Celsius");
System.out.println("2. Celsius to Fahrenheit");
System.out.println("3. Exit");
System.out.println("Choice:");
int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter temperature: ");
fahrenheit = scan.nextInt();
System.out.println(fahrenheit + " Fahrenheit is " + Celsius(fahrenheit) + " Celsius");
System.out.println("1. Fahrenheit to Celsius");
System.out.println("2. Celsius to Fahrenheit");
System.out.println("3. Exit");
System.out.println("Choice:");
choice = scan.nextInt();
case 2:
System.out.println("Enter temperature: ");
celsius = scan.nextInt();
System.out.println(celsius + " Celsius is " + Fahrenheit(celsius) + " Fahrenheit");
System.out.println("1. Fahrenheit to Celsius");
System.out.println("2. Celsius to Fahrenheit");
System.out.println("3. Exit");
System.out.println("Choice:");
choice = scan.nextInt();
case 3:
break;
}
After the third attempt, the code doesn't run anymore no matter which choice I input. 第三次尝试后,无论我输入哪种选择,代码都不再运行。 I want it so it continues to ask as long as the input is either 1 or 2.
我想要它,只要输入是1或2,它就会继续询问。
Enclose it in a do-while loop 将其封装在do-while循环中
do {
switch(choice) {
...
}
}while(choice != 3);
You should have a menu method, for example: 您应该有一个菜单方法,例如:
public static void showMenu() {
System.out.println("1. Fahrenheit to Celsius");
System.out.println("2. Celsius to Fahrenheit");
System.out.println("3. Exit");
System.out.println("Choice:");
}
On your main method have a call to this method inside a do-while
loop: 在您的main方法上,在
do-while
循环中调用此方法:
public static void main(String args[]) {
//Add your Scanner code here, your variables, etc
do {
showMenu();
choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter temperature: ");
fahrenheit = scan.nextInt();
System.out.println(fahrenheit + " Fahrenheit is " + Celsius(fahrenheit) + " Celsius");
break;
case 2:
//Do the same for method 1 but for Celsius
} while (choice != 3);
}
And, you're done! 并且,您完成了!
Just put while-loop
or do-while
a around your switch-case as likewise, 同样,只要在开关盒周围放置
while-loop
或do-while
,
while(choice!= 3) {
switch (choice) {
.....
}
}
OR 要么
do {
switch(choice) {
......
}
} while (choice != 3)
This will lets you to continuously iterate
as many time as you want unless
you enter 3
. unless
输入3
unless
您将连续不断地iterate
时间。
you can even put the following condition
in order to make more secure, 您甚至可以设置以下
condition
以提高安全性,
while ( choice >= 3 ) //not recommended but you can.
If you do not want the program to end put it in a while loop: 如果您不希望程序结束,则将其放入while循环中:
Scanner scan = new Scanner(System.in);
System.out.println("1. Fahrenheit to Celsius");
System.out.println("2. Celsius to Fahrenheit");
System.out.println("3. Exit");
System.out.println("Choice:");
int choice = scan.nextInt();
do {
switch (choice) {
...
}
} while (choice!= 3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.