用Java将开关盒返回主程序
我知道这是一个简单的问题,但我对Java还是新手,所以我很迷茫。在我的程序中,剩下要做的就是输出一条消息,如输入无效,在我的程序中,如果用户没有输入是或否,请在每个案例结束时重试,并返回到要求进行另一次计算的点。我知道这是基本的,我尽可能地寻找答案,但我对java术语的了解还不够。如果你能帮助我,我将不胜感激 另外,有人向我指出,我的变量不应该以大写字母开头,我知道,将来也不会这样做用Java将开关盒返回主程序,java,Java,我知道这是一个简单的问题,但我对Java还是新手,所以我很迷茫。在我的程序中,剩下要做的就是输出一条消息,如输入无效,在我的程序中,如果用户没有输入是或否,请在每个案例结束时重试,并返回到要求进行另一次计算的点。我知道这是基本的,我尽可能地寻找答案,但我对java术语的了解还不够。如果你能帮助我,我将不胜感激 另外,有人向我指出,我的变量不应该以大写字母开头,我知道,将来也不会这样做 System.out.println(" The purpose of this program is
System.out.println(" The purpose of this program is to calculate the speed of sound through several mediums.\n The program user will input a distance in feet followed by a mediumd and the program will output the speed in feet per second and miles per hour\n");
//declare variables
Scanner keyboard = new Scanner(System.in);
final double Air = 1126.1;
final double Water = 4603.2;
final double Steel = 20013.3;
final double Earth = 22967.4;
double OneFootPerSecond = .68181818182;
double Distance;
double AirSpeed;
double WaterSpeed;
double SteelSpeed;
double EarthSpeed;
boolean shouldContinue = true;
while (shouldContinue == true){
System.out.print(" What is the distance in feet:" );
//ask the user to input variables
while (!keyboard.hasNextDouble()){
System.out.println("Please enter a valid numeric value, try again: ");
keyboard.next();
}
Distance =keyboard.nextDouble();
{
System.out.print("Input the media: Air, Water, Steel, or Earth: ");
String Input = keyboard.next();
switch(Input.toLowerCase())
{
case "air":
AirSpeed = Distance/Air;
System.out.print("\n \nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through AIR" +"\n");
System.out.printf("%.6f", AirSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond*Air);
System.out.print(" miles per hour.");
System.out.print("\n \nEnter Yes for another calculation, else No: ");
String Another = keyboard.next();
Another.toLowerCase();
if (Another.equals("no")){
shouldContinue = false;
}
if (!Another.equals("no"))
if (!Another.equals("yes"))
{System.out.print("Invalid.");
}
break;
case "water":
WaterSpeed = Distance/Water;
System.out.print("\nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through WATER" +"\n");
System.out.printf("%.6f",WaterSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond*Water);
System.out.print(" miles per hour.");
System.out.print("\n \nEnter Yes for another calculation, else No: ");
Another = keyboard.next();
Another.toLowerCase();
if (Another.equals("yes")){
shouldContinue = false;
}
break;
case "steel":
SteelSpeed = Distance/Steel;
System.out.print("\nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through STEEL" +"\n");
System.out.printf("%.6f",SteelSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond*Steel);
System.out.print(" miles per hour.");
System.out.print("\n \nEnter Yes for another calculation, else No: ");
Another = keyboard.next();
Another.toLowerCase();
if (Another.equals("yes")){
shouldContinue = false;
}
break;
case "earth":
EarthSpeed = Distance/Water;
System.out.print("\nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through EARTH" +"\n");
System.out.printf("%.6f",EarthSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond*Earth);
System.out.print(" miles per hour.");
System.out.print("\n \nEnter Yes for another calculation, else No: ");
Another = keyboard.next();
Another.toLowerCase();
if (Another.equals("yes")){
shouldContinue = false;
}
break;
default :
System.out.print("Invalid. Re-run the program. ");
break;
}
}
你有逻辑问题
System.out.print("\n \nEnter Yes for another calculation, else No: ");
Another = keyboard.next();
Another.toLowerCase();
if (Another.equals("yes")){
shouldContinue = false;
}
如果用户键入yes,则此代码将退出循环,您的if应为:
if (Another.equals("yes")){
shouldContinue = true;
}
else
if (Another.equals("no")){
shouldContinue = false;
}
else
{
System.out.print("Invalid input");
shouldContinue = true;
}
考虑到您希望为每个案例请求另一个计算,为了防止重复代码,请将额外处理的提示移出案例,并将其放在switch语句之后。然后提供一种方法继续提示用户,直到用户输入可接受的输入
public boolean promptForContinue(final Scanner keyboard) {
boolean isValid = false;
String userInput = "";
do {
userInput = keyboard.next();
isValid = userInput.matches("Yes|No");
if (!isValid) {
System.out.println("Invalid entry.");
}
} while (!isValid);
return userInput.equals("Yes") ? true : false;
}
编辑:替代实现,不需要额外的局部变量,也不需要使用正则表达式。此外,添加.toLowerCase扩展了可接受的输入,而不需要额外的case语句。对于这个简单的用例,我们可以利用case语句的fall-through效应将可接受值扩展到8
private static boolean promptForContinue(final Scanner keyboard)
{
do
{
System.out.print("Continue (Yes/No) ?");
final String userInput = keyboard.next().toLowerCase();
switch(userInput)
{
case "y":
case "yes": return true;
case "n":
case "no": return false;
default :
System.out.println("Invalid Entry.");
}
}
while (true);
}
然后shouldContinue将设置为while循环结束时该方法的返回值
shouldContinue = promptForContinue(keyboard);
将您的建议与我的建议相结合,该文件应如下所示。另外,我建议将这两个计算都存储在一个变量中,这样您就可以将重复的打印语句移出案例
public static void main(String[] args)
{
System.out.println(" The purpose of this program is to calculate the speed of sound through several mediums.\n The program user will input a distance in feet followed by a mediumd and the program will output the speed in feet per second and miles per hour\n");
//declare variables
Scanner keyboard = new Scanner(System.in);
final double Air = 1126.1;
final double Water = 4603.2;
final double Steel = 20013.3;
final double Earth = 22967.4;
double OneFootPerSecond = .68181818182;
double Distance;
double AirSpeed;
double WaterSpeed;
double SteelSpeed;
double EarthSpeed;
boolean shouldContinue = true;
while (shouldContinue == true)
{
System.out.print(" What is the distance in feet:");
//ask the user to input variables
while (!keyboard.hasNextDouble())
{
System.out.println("Please enter a valid numeric value, try again: ");
keyboard.next();
}
Distance = keyboard.nextDouble();
System.out.print("Input the media: Air, Water, Steel, or Earth: ");
String Input = keyboard.next();
switch (Input.toLowerCase())
{
case "air":
AirSpeed = Distance / Air;
System.out.print("\n \nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through AIR" + "\n");
System.out.printf("%.6f", AirSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond * Air);
System.out.println(" miles per hour.");
break;
case "water":
WaterSpeed = Distance / Water;
System.out.print("\nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through WATER" + "\n");
System.out.printf("%.6f", WaterSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond * Water);
System.out.println(" miles per hour.");
break;
case "steel":
SteelSpeed = Distance / Steel;
System.out.print("\nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through STEEL" + "\n");
System.out.printf("%.6f", SteelSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond * Steel);
System.out.println(" miles per hour.");
break;
case "earth":
EarthSpeed = Distance / Water;
System.out.print("\nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through EARTH" + "\n");
System.out.printf("%.6f", EarthSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond * Earth);
System.out.println(" miles per hour.");
break;
default:
System.out.println("Invalid. Re-run the program. ");
break;
}
shouldContinue = promptForContinue(keyboard);
}
}
private static boolean promptForContinue(final Scanner keyboard)
{
boolean isValid = false;
String userInput = "";
do
{
System.out.print("Continue (Yes/No) ?");
userInput = keyboard.next();
isValid = userInput.matches("Yes|No");
if (!isValid)
{
System.out.println("\nInvalid entry.");
}
}
while (!isValid);
return userInput.equals("Yes") ? true : false;
}
我试过你的建议,但我有很多错误。公共布尔值表达式的开头非法,暂时找不到符号!是有效的和预期的;刚好在…之后!这是有效的。我无法想象解决方案是困难的,我只需要返回到程序中要求进行另一次计算的部分,如果没有输入“是”或“否”。我快发疯了:我的道歉…由于我的多任务处理,没有测试就很快把它拼凑起来了。现在应该很好了。一切看起来都是正确的,除了在公共布尔值上,我得到了一个表达式的非法开始。我可能是不正确的,但这与publicstaticvoidmain有关吗?将其设为privatesetatic,并确保它不在mainFixed的方法定义范围内,并且功能齐全!先生或女士,我真是太感谢你了,我想弄明白这一点已经太久了!你救了我一些可能的白发;。但说真的,非常感谢你!