循环序列停止
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环序列停止相关的知识,希望对你有一定的参考价值。
我将代码放入do,while循环中。它询问用户他们想要哪个菜单选项,并为其计算方程式。该代码应该一直继续下去,直到用户按下4(这是退出选项)为止,但是在他们打了一个序列之后它就停止了。我不知道我需要更改或添加什么,所以它一直在继续
import java.util.Scanner;
import java.text.DecimalFormat;
class Lab5
public static void main(String[] args) //header of the main method
Scanner in = new Scanner(System.in);
int choice;
int rem = 0;
int num;
do
//user prompt
System.out.print("Choose from the following menu\n1) Calculate the sum of integers 1 to m\n2) Factorial of a number\n3) Repeat the first number\n4) Quit\n:");
choice = in.nextInt();
switch(choice)
case 1:
int m, sum =0;
int i = 1;
System.out.print("Enter the number:");
m = in.nextInt();
while (i <= m)
sum=sum+i;
i++;
System.out.print("The sum of:" + m + ' ' + "is" + ' ' + sum);
break;
case 2:
int number, fact =1;
System.out.print("Enter the number:");
number = in.nextInt();
i=1;
for (int factor = 2; factor <= number; factor++)
fact = fact*factor;
System.out.print("The Factorial of +:" + number + ' ' + "is" + ' ' + fact);
break;
case 3:
System.out.print("Enter the number:");
num = in.nextInt();
while(num!=0)
rem = num%10;
num = num/10;
System.out.print("The leftmost digit is:" + rem);
break;
default:
break;
while (choice == '4');
System.out.print(" ");
答案
您将其写为do ... while ( choice == '4' )
,这意味着只有在用户输入4时它才会继续。
想要的声音choice != '4'
。
以上是关于循环序列停止的主要内容,如果未能解决你的问题,请参考以下文章