Java计算器中的连续性问题
我是Java新手,我正在做一个计算器应用程序 我试图让我的计算器在“无效输入”后的else语句之后不断提示用户输入正确答案(键入Y或N)。Java计算器中的连续性问题,java,switch-statement,calculator,Java,Switch Statement,Calculator,我是Java新手,我正在做一个计算器应用程序 我试图让我的计算器在“无效输入”后的else语句之后不断提示用户输入正确答案(键入Y或N)。 我希望程序在最终输入正确的输入后继续计算 我曾经玩过一个嵌入的while循环,但最终得到了一个无限循环或一个没有解析的循环。代码如下 import java.util.Scanner; 类计算{ 公共静态void.main(字符串[]args){ System.out.println(“欢迎使用计算器!”); System.out.println(“****
我希望程序在最终输入正确的输入后继续计算 我曾经玩过一个嵌入的
while
循环,但最终得到了一个无限循环或一个没有解析的循环。代码如下
import java.util.Scanner;
类计算{
公共静态void.main(字符串[]args){
System.out.println(“欢迎使用计算器!”);
System.out.println(“****************************************************************************************************************************”);
扫描仪用户输入=新扫描仪(System.in);
双num1,num2;
字符串选择;
布尔值=真;
while(youdecision==true){
System.out.println(“请输入一个数字:”);
num1=userinput.nextDouble();
System.out.println(“请输入可用的运算符(+、-、*、/):”;
char operator=userinput.next().charAt(0);
System.out.println(“请输入另一个号码:”);
num2=userinput.nextDouble();
双输出;
开关(操作员)
{
格“+”:
输出=num1+num2;
打破
案例'-':
输出=num1-num2;
打破
案例“*”:
输出=num1*num2;
打破
案例“/”:
输出=num1/num2;
如果(num2==0)
System.out.println(“数学错误!数字不能被零除”);
打破
违约:
System.out.println(“输入无效。请输入一个可用的运算符,即(+、-、*、/):”;
返回;
}
System.out.println(“****************************************************************************************************************************”);
System.out.println(“答案是:“+”\n“+输出”);
System.out.println(“****************************************************************************************************************************”);
System.out.println(“您想再次计算吗?”);
System.out.println(“请输入Y表示是,或输入N表示否”);
choice=userinput.next();
if(选择相等信号情况(“Y”)){
System.out.println(“好的,让我们继续吧!”);
System.out.println(“****************************************************************************************************************************”);
Youdecise=正确;
}
else if(选择.相等信号情况(“N”)){
System.out.println(“****************************************************************************************************************************”);
System.out.println(“好的,谢谢你使用计算器,再见!”);
系统出口(0);
}
否则{
System.out.println(“输入无效,请重试…”);
System.out.println(“****************************************************************************************************************************”);
Youdecise=false;
}
}
}
}
您需要一个while
循环来确保值是正确的
由于用户键入字符时需要在代码中加入逻辑,否则必须更改许多行代码,或者再次请求所有值
我认为对于乞丐来说,最简单的方法就是使用这个循环:
char算子;
while(true){
operator=userinput.next().charAt(0);
if(运算符='+'| |运算符='-'| |运算符=='*'| |运算符=='/')){
打破
}否则{
System.out.println(“请输入有效的运算符:”);
}
}
有很多更优雅的方法可以做到这一点。但我认为对于乞丐来说,这是理解和实现代码最简单的方法
此循环仅用于确保用户键入有效字符。虽然字符不是其中之一,但循环将进行迭代
您必须将此代码正好放在此行下方,在此处您需要一个有效的运算符。您需要一个
while
循环,以确保值是正确的
由于用户键入字符时需要在代码中加入逻辑,否则必须更改许多行代码,或者再次请求所有值
我认为对于乞丐来说,最简单的方法就是使用这个循环:
char算子;
while(true){
operator=userinput.next().charAt(0);
if(运算符='+'| |运算符='-'| |运算符=='*'| |运算符=='/')){
打破
}否则{
System.out.println(“请输入有效的运算符:”);
}
}
有很多更优雅的方法可以做到这一点。但我认为对于乞丐来说,这是理解和实现代码最简单的方法
此循环仅用于确保用户键入有效字符。虽然字符不是其中之一,但循环将进行迭代
您必须将此代码放在要求有效操作员的此行下方。我对您的代码做了一些更改和注释
import java.util.Scanner;
class Calculator {
public static void main(String[] args) {
System.out.println("Welcome To Calculator!");
System.out.println("*************************************************************************");
Scanner userinput = new Scanner(System.in);
double num1, num2;
String choice;
//the "youDecide" variable is not needed at all
while (true) {
System.out.println("Please enter a number: ");
num1 = userinput.nextDouble();
System.out.println("Please enter an available operator (+, -, *, /): ");
char operator = userinput.next().charAt(0);
System.out.println("Please enter another number: ");
num2 = userinput.nextDouble();
double output;
switch (operator) {
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
output = num1 / num2;
if (num2 == 0) {
System.out.println("Math error! A number cannot be divided by zero.");
}
break;
default:
System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
continue; //changed from "return" you don't want to exit, just to skip to the next execution of the loop
}
System.out.println("*************************************************************************");
System.out.println("The answer is: " + "\n" + output);
System.out.println("*************************************************************************");
System.out.println("Would you like to calculate again?");
System.out.println("Please enter Y for yes, or N for no");
choice = userinput.next();
if (choice.equalsIgnoreCase("Y")) {
System.out.println("Okay. Let's continue!");
System.out.println("*************************************************************************");
} else if (choice.equalsIgnoreCase("N")) {
System.out.println("*************************************************************************");
System.out.println("Okay. Thanks for using Calculator. Goodbye!");
System.exit(0);
} else {
System.out.println("Invalid input. Please try again...");
System.out.println("*************************************************************************");
}
}
}
}
我对您的代码做了一些更改和注释
import java.util.Scanner;
class Calculator {
public static void main(String[] args) {
System.out.println("Welcome To Calculator!");
System.out.println("*************************************************************************");
Scanner userinput = new Scanner(System.in);
double num1, num2;
String choice;
//the "youDecide" variable is not needed at all
while (true) {
System.out.println("Please enter a number: ");
num1 = userinput.nextDouble();
System.out.println("Please enter an available operator (+, -, *, /): ");
char operator = userinput.next().charAt(0);
System.out.println("Please enter another number: ");
num2 = userinput.nextDouble();
double output;
switch (operator) {
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
output = num1 / num2;
if (num2 == 0) {
System.out.println("Math error! A number cannot be divided by zero.");
}
break;
default:
System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
continue; //changed from "return" you don't want to exit, just to skip to the next execution of the loop
}
System.out.println("*************************************************************************");
System.out.println("The answer is: " + "\n" + output);
System.out.println("*************************************************************************");
System.out.println("Would you like to calculate again?");
System.out.println("Please enter Y for yes, or N for no");
choice = userinput.next();
if (choice.equalsIgnoreCase("Y")) {
System.out.println("Okay. Let's continue!");
System.out.println("*************************************************************************");
} else if (choice.equalsIgnoreCase("N")) {
System.out.println("*************************************************************************");
System.out.println("Okay. Thanks for using Calculator. Goodbye!");
System.exit(0);
} else {
System.out.println("Invalid input. Please try again...");
System.out.println("*************************************************************************");
}
}
}
}
在添加J.F.和Javaman建议的编辑以及一些研究之后,我能够通过在代码中添加以下行来解决问题:
import java.util.InputMismatchException;
import java.util.Scanner;
class Calculator {
public static void main(String[] args) {
System.out.println("Welcome To Calculator!");
System.out.println("*************************************************************************");
Scanner userinput = new Scanner(System.in);
char operator;
double num1, num2;
String choice;
while(true) {
System.out.println("Please enter a number: ");
num1 = userinput.nextDouble();
System.out.println("Please enter an available operator (+, -, *, /): ");
while(true) {
operator = userinput.next().charAt(0);
if(operator == '+' || operator == '-' || operator == '*' || operator == '/') {
break;
}else {
System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
}
}
System.out.println("Please enter another number: ");
num2 = userinput.nextDouble();
double output;
switch(operator) {
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
output = num1 / num2;
break;
default:
System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
continue;
}
System.out.println("*************************************************************************");
if(num2 == 0) {
System.out.println("Math error! A number cannot be divided by zero.");
}else {
System.out.println("The answer is: " + "\n" + output);
}
System.out.println("*************************************************************************");
System.out.println("Would you like to calculate again?");
System.out.println("Please enter Y for yes, or N for no");
choice = userinput.next();
if(choice.equalsIgnoreCase("Y")) {
System.out.println("Okay. Let's continue!");
System.out.println("*************************************************************************");
}else if(choice.equalsIgnoreCase("N")) {
System.out.println("*************************************************************************");
System.out.println("Okay. Thanks for using Calculator. Goodbye!");
System.exit(0);
}else {
System.out.println("Invalid input. Please try again...");
System.out.println("*************************************************************************");
**while (!("Y").equalsIgnoreCase(choice) && !("N").equalsIgnoreCase(choice)) {
System.out.println("Invalid input. Please try again...");
System.out.println("*************************************************************************");
System.out.println("Please enter Y for yes, or N for no");
choice = userinput.next();
if(choice.equalsIgnoreCase("Y")) {
System.out.println("Okay. Let's continue!");
System.out.println("*************************************************************************");
}else if(choice.equalsIgnoreCase("N")) {
System.out.println("*************************************************************************");
System.out.println("Okay. Thanks for using Calculator. Goodbye!");
System.exit(0);**
}
}
}
}
}
}
在添加J.F.和Javaman建议的编辑以及一些研究之后,我能够通过在代码中添加以下行来解决问题:
import java.util.InputMismatchException;
import java.util.Scanner;
class Calculator {
public static void main(String[] args) {
System.out.println("Welcome To Calculator!");
System.out.println("*************************************************************************");
Scanner userinput = new Scanner(System.in);
char operator;
double num1, num2;
String choice;
while(true) {
System.out.println("Please enter a number: ");
num1 = userinput.nextDouble();
System.out.println("Please enter an available operator (+, -, *, /): ");
while(true) {
operator = userinput.next().charAt(0);
if(operator == '+' || operator == '-' || operator == '*' || operator == '/') {
break;
}else {
System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
}
}
System.out.println("Please enter another number: ");
num2 = userinput.nextDouble();
double output;
switch(operator) {
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
output = num1 / num2;
break;
default:
System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
continue;
}
System.out.println("*************************************************************************");
if(num2 == 0) {
System.out.println("Math error! A number cannot be divided by zero.");
}else {
System.out.println("The answer is: " + "\n" + output);
}
System.out.println("*************************************************************************");
System.out.println("Would you like to calculate again?");
System.out.println("Please enter Y for yes, or N for no");
choice = userinput.next();
if(choice.equalsIgnoreCase("Y")) {
System.out.println("Okay. Let's continue!");
System.out.println("*************************************************************************");
}else if(choice.equalsIgnoreCase("N")) {
System.out.println("*************************************************************************");
System.out.println("Okay. Thanks for using Calculator. Goodbye!");
System.exit(0);
}else {
System.out.println("Invalid input. Please try again...");
System.out.println("*************************************************************************");
**while (!("Y").equalsIgnoreCase(choice) && !("N").equalsIgnoreCase(choice)) {
System.out.println("Invalid input. Please try again...");
System.out.println("*************************************************************************");
System.out.println("Please enter Y for yes, or N for no");
choice = userinput.next();
if(choice.equalsIgnoreCase("Y")) {
System.out.println("Okay. Let's continue!");
System.out.println("*************************************************************************");
}else if(choice.equalsIgnoreCase("N")) {
System.out.println("*************************************************************************");
System.out.println("Okay. Thanks for using Calculator. Goodbye!");
System.exit(0);**
}
}
}
}
}
}
publicstaticvoid.main()
它是如何编译的?始终复制/粘贴代码,切勿在StackOverf中重新键入