这是我用Java编写的基本计算器。显示输出后,我想问用户是否要继续,如果他回答“是”,则重复该过程。我已经检查了许多有关堆栈溢出的主题,但我仍然不知道如何通过 do while循环实现我的目标,任何人都可以帮忙吗?
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);
}
}
}
答案 0 :(得分:0)
def path_mas_largo (labyrinth, idlaberinto):
for i in range (len (labyrinth [idlaberinto]) - 1):
if len (labyrinth [idlaberinto] [i] ['ensenanza' + str (i)]) > len (labyrinth [idlaberinto] [i + 1] ['ensenanza' + str (i + 1)]):
j = len (labyrinth [idlaberinto] [i] ['ensenanza' + str (i)])
if j > len (labyrinth [idlaberinto] [i + 1] ['ensenanza' + str (i + 1)]):
camino_largo = laberinto [idlaberinto] [i] ['ensenanza' + str (i)]
print (long_path)
emular_recorrido (camino_largo, ventana, blanco, negro, celeste, aumento)
答案 1 :(得分: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
循环是一个后递增循环,因此它将首先执行,然后在一段时间内检查条件。