菜单选项的代码不会循环,但没有错误。我故意写它类似于我的while循环,循环很好。我只需要使用整数,我的导师确实告诉我把输入变成变量。大部分都没有完成,但我正在努力解决这一问题。
//Loop to check user input
num=-1;
while (num<0)
{
//Getting user entered interger
System.out.print("Please enter a positive number: ");
num = kb.nextInt();
//Ensuring positive interger
if (num >= 0)
{
System.out.println("You've entered the number " + num + ".");
}
else
{
System.out.println("Error! The number you've entered is not a valid interger.");
}
}
copy = num;
//Printing menu loop
copy2 = -1;
do
{
//Menu making
System.out.println(" ");
System.out.println("1. Enter a new number.");
System.out.println("2. Print the number of odd digits, even digits, and zeros in the interger.");
System.out.println("3. print if the number is light or heavy.");
System.out.println("4. Print the prime numbers between 2 and the interger (inclusive).");
System.out.println("5. Quit the program.");
System.out.println(" ");
System.out.print("Please enter your menu choice: ");
choice = kb.nextInt();
copy2 =choice;
//Checking entry
if (0<copy2 && copy2<=5)
{
System.out.println("You chose option " +copy2+".");
}
//Option one
if (copy2 == 1)
{
System.out.print("Will work on soon.");
}
//Option two
if (copy2 == 2)
{
oddNum=0;
evenNum=0;
zero=0;
while (copy>0)
{
if (copy %10==0)
{
zero++;
}
else if (copy %2==1)
{
evenNum++;
}
else
{
oddNum++;
}
}
copy = copy/10;
System.out.println("Even numbers: " + evenNum+
"\nOdd numbers: "+ oddNum +
"\nZeros: " +zero );
}
//Option three
if (copy2 == 3)
{
loh = 0;
do
{
System.out.println("To check if your number is light or heavy, we need a second interger.");
System.out.print("Please enter a second positive interger: ");
loh = kb.nextInt();
if (loh >= 0)
{
numWeight = ((loh +copy)/2);
//Test num weight
System.out.println("Check: " +numWeight);
if (numWeight > copy)
{
System.out.print("The number " + copy + " compared to the number "
+ loh + " is a heavy number.");
}
else
{
System.out.print("The number " + copy + " compared to the number "
+ loh + " is a light number.");
}
}
if (loh < 0)
{
System.out.println("Error! The number you've entered is not a valid interger.");
}
}while (loh <=0);
}
//Option four
if (copy2 ==4)
{
primeNumbers = 0;
for (int i=1; i<=copy; i++)
{
int counter = 0;
for(int prime = i; prime>=1; prime--)
{
if (i%prime==0)
{
counter = counter +1;
}
}
if (counter==2)
{
primeNumbers = primeNumbers + i;
}
}
System.out.println("Prime numbers from 2-"+copy+" are: ");
System.out.println(primeNumbers);
}
//Option five
else
{
System.out.print("Error! Please enter a valid menu option.");
}
}while (copy2 <0 && copy2 >=6);
}//End main
}//End class
答案 0 :(得分:0)
您应该使用开关盒来达到您的目的。嵌套if / else并不是菜单选项的好主意,但是切换案例可以让你更清楚地了解你要做什么。
switch (num) {
case c1:
statements // they are executed if variable == c1
System.out.println("1. Enter a new number.");
break;
case c2:
System.out.println("2. Print the number of odd digits, even digits, and zeros in the interger.");
statements // they are executed if variable == c2
break;
case c3:
case c4:
statements // they are executed if variable == any of the above c's
break;
. . .
default:
statements // they are executed if none of the above case is satisfied
break;
}