我无法让开关不断循环,直到用户告诉程序停止。 当提示用户放入 N 循环时,应将其发送回开关顶部
char stop;
while(stop == 'N'){
switch(choice){
case 1:
System.out.println("Enter the time in seconds:");
time = input.nextInt();
displacement = (Math.pow(time,4)) + 16;
System.out.println("Felix's displacement is equal to " + displacement + " meters");
System.out.println("Stop the application(Y/N)");
stop = input.findWithinHorizon(".",0).charAt(0);
break;
case 2:
System.out.println("Enter the time in seconds:");
time = input.nextInt();
velocity = 4*(Math.pow(time,3));
System.out.println("Felix's velocity is equal to " + velocity + " m/s");
break;
case 3:
System.out.println("Enter the time in seconds:");
time = input.nextInt();
acceleration = 12*(Math.pow(time,2));
System.out.println("Felix's acceleration is equal to " + acceleration + " m/(s*s)");
break;
default:
System.out.println("Please select a choice");
}
}
}
最佳答案
- 粗略地说:初始化
stop
关于声明:
char stop = 'N';
更好:替换您的
while
与do
while
循环:做{
} while (stop == 'N')
关于java - 带 Switch 语句的 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29372691/