[英]How would I go about creating input commands to execute different part of my code? - Java
I have created a basic java program that determines the hypotenuse of a triangle. 我创建了一个基本的Java程序来确定三角形的斜边。 Originally, the program would ask for side A, then side B, and automatically calculate the hypotenuse. 最初,程序将要求A面,然后是B面,并自动计算斜边。
I want to create a input command list that will allow the user to type "a" in when giving the A side value, type "b" when giving the B side value, and then type "c" to calculate the hypotenuse, or "q" to exit the program. 我想创建一个输入命令列表,该列表将允许用户在提供A边值时键入“ a”,在提供B边值时键入“ b”,然后键入“ c”以计算斜边,或“ q”退出程序。
Instead of forcing the user to put in side A first, I want them to be able to put in either side at there will. 我希望他们能够随意摆在任一侧,而不是强迫用户先放入A侧。 However, if the user types "c" and either the A or B value is missing (or both) I would like an error message and have the user correct it. 但是,如果用户键入“ c”,而A或B值都缺失(或两者都缺失),我会收到一条错误消息,并让用户对其进行更正。
So far I have 到目前为止,我有
import java.util.InputMismatchException;
import java.util.Scanner;
public class handleExceptions1 {
public static void main(String[] args) {
Scanner initial = new Scanner(System.in);
System.out.println(" Type 'a' to enter the value for side A.\n Type 'b' to enter the value for side B.\n Type 'c' to calculate the hypotenuse.\n Or type 'q' to exit");
String inputselected = initial.next();
boolean repeat = true;
double _sideA = 0;
while (repeat) {
try {
Scanner input = new Scanner(System.in);
System.out.print("Please enter side A, this may not be 0: ");
_sideA = input.nextDouble();
if (_sideA > 0){
repeat = false;
}
} catch (InputMismatchException e) {
System.out.print("Error! Please enter a valid number!");
}
}
boolean repeat2= true;
double _sideB = 0;
while (repeat2){
try {
Scanner input = new Scanner(System.in);
System.out.print("Please enter side B, this may not be 0: ");
_sideB = input.nextDouble();
if (_sideB > 0){
repeat2= false;
}
} catch (InputMismatchException e) {
System.out.print("Error! Please enter a valid number!");
}
}
double hypotenuse = Math.sqrt((_sideA*_sideA) + (_sideB*_sideB));
System.out.print("Side C(the hypotenuse) is: "+ hypotenuse);
}
}
My logic is to put something after "String inputselected = ..." but I'm not sure what. 我的逻辑是在“ String inputselected = ...”之后加上一些内容,但我不确定。 If anyone can help me, it would be greatly appreciated ! 如果有人可以帮助我,将不胜感激!
sideA = -1;
sideB = -1;
Scanner input = new Scanner(System.in);
do
{
System.out.println("Enter your choice ( a/b/c/q ) : ");
char ch = in.nextChar();
switch(ch)
{
case 'a': sideA = in.nextDouble();
if(sideA<0)
System.out.println("Error! Please enter a valid number!");
break;
case 'b': sideB = in.nextDouble();
if(sideB<0)
System.out.println("Error! Please enter a valid number!");
break;
case 'c': if(sideA<0 || sideB<0)
System.out.println("Other two sides not yet given! please provide a and b first. ");
else
System.out.print("Side C(the hypotenuse) is: "+ Math.sqrt((_sideA*_sideA) + (_sideB*_sideB)););
break;
case 'q': break;
default : System.out.println(" Enter a valid choice! ");
}
}while(ch!='q');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.