Java 如何询问用户是否希望使用do while重复计算
这是我用java编写的一个基本计算器。一旦显示输出,我想询问用户是否要继续,如果他说是,然后重复这个过程。我已经检查了许多关于堆栈溢出的主题,但我仍然不知道如何通过do while循环实现我的目标,有人可以帮助吗Java 如何询问用户是否希望使用do while重复计算,java,do-while,Java,Do While,这是我用java编写的一个基本计算器。一旦显示输出,我想询问用户是否要继续,如果他说是,然后重复这个过程。我已经检查了许多关于堆栈溢出的主题,但我仍然不知道如何通过do while循环实现我的目标,有人可以帮助吗 import java.util.Scanner; public class SecondQuestion { public static void main(String[] args) { // TODO Auto-generated method st
import java.util.Scanner;
public class SecondQuestion {
public static void main(String[] args) {
// TODO Auto-generated method stub
double firstN;
double secondN;
char operator;
Scanner readInput = new Scanner(System.in);
System.out.printf("Type a number, operator, number --" + "separated by a space: ");
firstN = readInput.nextDouble();
operator = readInput.next().charAt(0);
secondN = readInput.nextDouble();
if (operator == '+')
System.out.printf("%f + %f = %f", firstN, secondN, firstN + secondN);
else if (operator == '-')
System.out.printf("%f - %f = %f", firstN, secondN, firstN - secondN);
else if (operator == '*')
System.out.printf("%f * %f = %f", firstN, secondN, firstN * secondN);
else if (operator == '/')
System.out.printf("%f / %f = %f", firstN, secondN, firstN / secondN);
else if (operator == '%')
System.out.printf("%f %% %f = %f", firstN, secondN,firstN % secondN);
else
System.out.printf("Unknown operator");
System.out.printf("\n\n");
int loopCount = 0;
char charResponse='y';
Scanner readInput1 = new Scanner (System.in);
while (charResponse !='y')
{
System.out.println("press y");
charResponse = readInput1.next().charAt(0);
}
}
}
使用
do while
:
public static void main(String[] args) {
// TODO Auto-generated method stub
double firstN;
double secondN;
char operator;
Scanner readInput = new Scanner(System.in);
char charResponse = 'y';
do {
System.out.printf("Type a number, operator, number --" + "separated by a space: ");
firstN = readInput.nextDouble();
operator = readInput.next().charAt(0);
secondN = readInput.nextDouble();
if (operator == '+')
System.out.printf("%f + %f = %f", firstN, secondN, firstN + secondN);
else if (operator == '-')
System.out.printf("%f - %f = %f", firstN, secondN, firstN - secondN);
else if (operator == '*')
System.out.printf("%f * %f = %f", firstN, secondN, firstN * secondN);
else if (operator == '/')
System.out.printf("%f / %f = %f", firstN, secondN, firstN / secondN);
else if (operator == '%')
System.out.printf("%f %% %f = %f", firstN, secondN, firstN % secondN);
else
System.out.printf("Unknown operator");
System.out.printf("\n\n");
int loopCount = 0;
Scanner readInput1 = new Scanner(System.in);
System.out.println("press y to continue");
charResponse = readInput1.next().charAt(0);
} while (charResponse == 'y');
}
do while
循环是一个增量后循环,因此它将首先执行,然后检查while中的条件。问题说明如何明确使用do while
。非常感谢您的回答。然而,你现在使用的方法超出了我的知识范围。我希望通过进一步的学习,我能理解你的答案。非常感谢你的帮助,我是Java新手,这真的很有帮助。我试过了,真的很管用。再次感谢!!!请考虑通过回答旁边的绿色刻痕来接受我的答案,我真的很感激。
public static void main(String[] args) {
// TODO Auto-generated method stub
double firstN;
double secondN;
char operator;
Scanner readInput = new Scanner(System.in);
char charResponse = 'y';
do {
System.out.printf("Type a number, operator, number --" + "separated by a space: ");
firstN = readInput.nextDouble();
operator = readInput.next().charAt(0);
secondN = readInput.nextDouble();
if (operator == '+')
System.out.printf("%f + %f = %f", firstN, secondN, firstN + secondN);
else if (operator == '-')
System.out.printf("%f - %f = %f", firstN, secondN, firstN - secondN);
else if (operator == '*')
System.out.printf("%f * %f = %f", firstN, secondN, firstN * secondN);
else if (operator == '/')
System.out.printf("%f / %f = %f", firstN, secondN, firstN / secondN);
else if (operator == '%')
System.out.printf("%f %% %f = %f", firstN, secondN, firstN % secondN);
else
System.out.printf("Unknown operator");
System.out.printf("\n\n");
int loopCount = 0;
Scanner readInput1 = new Scanner(System.in);
System.out.println("press y to continue");
charResponse = readInput1.next().charAt(0);
} while (charResponse == 'y');
}