如何使用do-while重新打印菜单

时间:2018-06-28 04:09:40

标签: java

package mathloops3;

import java.util.*; import java.util.Random;

public class Program3 {

 public static void main(String args[])
 {
     //======================================================================
     //Input
     //======================================================================

         Scanner kb = new Scanner( System.in );

         System.out.println("Please enter a non-negative integer: ");
         int num1 = kb.nextInt();
             while ( num1 == 0 || num1 <= 0 )
             {
                 System.out.println("Please enter a non-negative integer: ");
                 num1 = kb.nextInt();
             }

     //======================================================================
     //Menu
     //======================================================================

         System.out.println("Please make a choice");
         System.out.println("0 Print the number");
         System.out.println("1 Determine if the number is odd or even");
         System.out.println("2 Find half of the number");
         System.out.println("3 Find the reciprocal of the number");
         System.out.println("4 Raise the number to the power of 5");
         System.out.println("5 Raise the number to a power of 5");
         System.out.println("6 Generate 20 random numbers between 0 and the number");
         System.out.println("7 Find the sum of 0 up to your number");
         System.out.println("8 Find the factorial of the number");
         System.out.println("9 find the square root of the number");
         System.out.println("10 Find the square root of the number");
         System.out.println("11 Determine whether the number is prime");         
         System.out.println("12 Exit the Program");

     //======================================================================
     //Menu Choice Switch
     //======================================================================

         System.out.println("Please enter choice: ");
         int menuChoice = kb.nextInt(); 

         switch (menuChoice)
         {
             case 0: System.out.println("Yor number is " + num1 ); break;

             case 1: 
                 if ( num1 %2 == 0)
                    System.out.println( num1 + " is even!");
                 else
                     System.out.println( num1 + " is odd!");
                 break;

             case 2: System.out.println("Half of " + num1 +  " is " + num1 / 2); break;

             case 3: System.out.println("The reciprocal of " + num1 + " is 1/" + num1 ); break;

             case 4: System.out.println( num1 + " to the 5th power is " 
                     + (int)java.lang.Math.pow(num1,5)); break;

             case 5:
                 double t = 1;
                 double b = num1;
                 double exponent = 5;
                 for ( int i = 1; i<=exponent; i++ )
                     t = t*b;
                 System.out.println(num1 + " to the 5th power is " + (int)t);
                 break;

             case 6: 
                 List<Integer> numbers = new ArrayList<Integer>();
                 int  numberOfNumbersYouWant = 20; // This has to be less than 11
                 Random random = new Random();

                     do
                     {
                         int next = random.nextInt(20);
                         if (!numbers.contains(next))
                         {
                             numbers.add(next);
                             System.out.println(next);
                         }
                     } while (numbers.size() < numberOfNumbersYouWant);
                 break;

             case 7: 
                 for(int i = 1; i < num1; i++) 
                 {
                     System.out.println(i);
                 }
                 break;

             case 8:
                 int i,fact=1;  
                 int number=num1;//It is the number to calculate factorial    
                 for(i=1;i<=number;i++)
                     {    
                         fact=fact*i;    
                     }    
                 System.out.println("Factorial of "+number+" is: "+fact);  
                 break;

             case 9:
                 int sqrt = num1;
                 System.out.println("The square root of " + num1 + " is " + (int)Math.sqrt(sqrt));
                 break;

             case 10: 
                 int sqrtloop=1;
                 int iterations=1;
                 int curSum = 0;
                     do 
                     {
                         sqrtloop+=2; iterations++; curSum +=sqrtloop;

                     } while (curSum + sqrtloop < num1 );
                         System.out.println("The square root of " + num1 + " is " + iterations);
                 break;

             case 11: 
                 int num = num1;
                 boolean flag = false;
                 for(int prime = 2; prime <= num/2; ++prime)
                 {
                     // condition for nonprime number
                     if(num % prime == 0)
                     {
                         flag = true;
                         break;
                     }
                 }

                 if (!flag)
                     System.out.println(num + " is a prime number.");
                 else
                     System.out.println(num + " is not a prime number.");
                 break;

             case 12: 
                 System.out.println("Thank you, Bye-Bye!");
                 System.exit(0);
                 break;
         }//end switch
         do              //details: this is where i am trying out different things with the do-while but i just cant seem to get it
right
             {    
                 if ( menuChoice != 12)
                 {
                     System.out.println("Please make a choice");
                     System.out.println("0 Print the number");
                     System.out.println("1 Determine if the number is odd or even");
                     System.out.println("2 Find half of the number");
                     System.out.println("3 Find the reciprocal of the number");
                     System.out.println("4 Raise the number to the power of 5");
                     System.out.println("5 Raise the number to a power of 5");
                     System.out.println("6 Generate 20 random numbers between 0 and the number");
                     System.out.println("7 Find the sum of 0 up to your number");
                     System.out.println("8 Find the factorial of the number");
                     System.out.println("9 find the square root of the number");
                     System.out.println("10 Find the square root of the number");
                     System.out.println("11 Determine whether the number is prime");         
                     System.out.println("12 Exit the Program");
                 }
             }while (menuChoice != menuChoice);
 }//end main }//end class

我似乎无法重新打印菜单,我已经尝试了多种方法,但得出的结论是它必须是do-while循环,但是它将永久运行,或者将打印一次并然后终止程序。

2 个答案:

答案 0 :(得分:0)

猜猜这会有所帮助:

do         
                 {    
                         //your switch case should be here


                         System.out.println("Please make a choice");
                         System.out.println("0 Print the number");
                         System.out.println("1 Determine if the number is odd or even");
                         System.out.println("2 Find half of the number");
                         System.out.println("3 Find the reciprocal of the number");
                         System.out.println("4 Raise the number to the power of 5");
                         System.out.println("5 Raise the number to a power of 5");
                         System.out.println("6 Generate 20 random numbers between 0 and the number");
                         System.out.println("7 Find the sum of 0 up to your number");
                         System.out.println("8 Find the factorial of the number");
                         System.out.println("9 find the square root of the number");
                         System.out.println("10 Find the square root of the number");
                         System.out.println("11 Determine whether the number is prime");         
                         System.out.println("12 Exit the Program");
                         menuChoice =kb.nextInt();

                 }while (menuChoice >=0&&menuChoice<12);

答案 1 :(得分:0)

首先,您不能执行此操作(menuChoice!= menuChoice);。这将永远是错误的。这就是它永远运行的原因。

因此请执行以下操作。

添加变量int,并像这样添加do

Scanner kb = new Scanner(System.in);

int标志= 1;
做{ //在这里做你的事情 Sysout“输入2退出”; 标志= kb.nextInt();

} while(flag!= 2)`