Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: One last question: How to get menu to repeat!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default One last question: How to get menu to repeat!

    import java.util.Scanner;
     
    public class Quiz7
    {
    	public static void main(String[] args)
    	{
    		Scanner kb1 = new Scanner(System.in);
    		String programName;//
    		String dueDate;//
    		int radius;//
    		int length;//
    		int width;//
    		int height;//
     
    		System.out.print("What is the name of the program? ");
    		programName=kb1.nextLine();
    		System.out.print("What is the due date of the program? ");
    		dueDate=kb1.nextLine();
    		displayMyInfo(programName, dueDate);
    		int iMenuInput1 = menu();
     
     
     
     
    			switch(iMenuInput1)
    			{
    				case 1:
    						System.out.print("Enter the radius: ");
    						radius=kb1.nextInt();
    						double dAreaCircle;//
    						dAreaCircle = areaCircle(radius);
    						System.out.print("The area of the circle is" +dAreaCircle);
    						break;
    				case 2:
    						System.out.print("Enter the radius: ");
    						radius=kb1.nextInt();
    						double dCircumference;//
    						dCircumference = circumference(radius);
    						System.out.print("The circumference of the circle is " +dCircumference);
    						break;
    				case 3:
    						System.out.print("Enter the length: ");
    						length=kb1.nextInt();
    						System.out.print("Enter the width: ");
    						width=kb1.nextInt();
    						int iAreaRectangle;//
    						iAreaRectangle = areaRectangle(length, width);
    						System.out.print("The area of the rectangle is " +iAreaRectangle);
    						break;
    				case 4:
    						System.out.print("Enter the length: ");
    						length=kb1.nextInt();
    						System.out.print("Enter the width: ");
    						width=kb1.nextInt();
    						int iperimeterRectangle; //
    					    iperimeterRectangle = perimeterRectangle(length, width);
    						System.out.print("The perimeter of the rectangle is " +iperimeterRectangle);
    						break;
    				case 5: 
    				        System.out.print("Enter the length: ");
    						length=kb1.nextInt();
    						System.out.print("Enter the width: ");
    						width=kb1.nextInt();
    						System.out.print("Enter the height: ");
    						height = kb1.nextInt();
    						int iVolumeBox;//
    						iVolumeBox = volumeBox(length, width, height);
    						System.out.print("The volume of the box is " +iVolumeBox);
    						break;
     
    				case 6:
    						System.out.print("Enter the radius: ");
    						radius=kb1.nextInt();
    						System.out.print("Enter the height: ");
    						height=kb1.nextInt();
    						double dVolumeCone;
    						dVolumeCone = volumeCone(radius, height);
    						System.out.print("The volume of the cone is " +dVolumeCone);
    						break;
     
    				default:
    							System.exit(0);
    			}//end of switch
     
    	int count = 0;
    	while (count < 7)
    	{
    		count++;
     
    	}
    	}//end of main
     
     
    	public static void displayMyInfo(String programName, String dueDate)
    	{
    			System.out.print("The program name is" +programName);
    			System.out.print("The due date of the program is" +dueDate);
    	}//end of displayMyInfo
     
    	public static int menu()
    	{
    		Scanner kb2 = new Scanner(System.in);
    		int iMenuInput2;
    		System.out.print("* * * * * * * * M E N U * * * * * * * *");
    		System.out.print("\n\n1>   Area of a circle");
    		System.out.print("\n2>    Circumference of a circle");
    		System.out.print("\n3>    Area of a rectangle");
    		System.out.print("\n4>   Perimeter of a rectangle");
    		System.out.print("\n5>    Volume of a box");
    		System.out.print("\n6>    Volume of a cone");
    		System.out.print("Enter your choice ");
    		iMenuInput2=kb2.nextInt();
    		return iMenuInput2;
    	}//end of menu
     
    	public static double areaCircle(int radius)
    	{
     
    		final double PI = 3.1416f;
    		double dAreaCircle;
    		dAreaCircle = PI * radius * radius;
    		return dAreaCircle;
    	}//end of areaCircle
     
     
     
    	public static double circumference(int radius)
    	{
    		final double PI = 3.1416f;
    		float fCircumference;
    		fCircumference = (float)(2 * PI * radius);
    		return fCircumference;
    	}//end of circumference
     
    	public static int areaRectangle(int length, int width)
    	{
    		int iAreaRectangle;
    		iAreaRectangle = length * width;
    		return iAreaRectangle;
    	}//end of areaRectangle
     
    	public static int perimeterRectangle(int length, int width)
    	{
    		int iPerimeterRectangle;
    		iPerimeterRectangle = 2 *(length * width);
    		return iPerimeterRectangle;
    	}//end of perimeterRectangle
     
    	public static int volumeBox(int length, int width, int height)
    	{
    		int iVolumeBox;
    		iVolumeBox = length * width * height;
    		return iVolumeBox;
    	}//end of volumeBox
     
    	public static double volumeCone(int radius, int height)
    	{
    		final double PI = 3.1416f;
    		float fVolumeCone;
    		fVolumeCone = (float)(PI*radius*radius*radius*height)/3.0f;
    		return fVolumeCone;
     
    	}//end of volumeCone
     
     
     
     
     
    }//end of Quiz7

    I want the menu to repeat itself 7 times, but the way I have it set up, it only shows the menu repeatedly and does not allow me to input the formulas. I'll let you guys sleep after I figure this one out. Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: One last question: How to get menu to repeat!

    You have a loop. Why don't you use it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: One last question: How to get menu to repeat!

    import java.util.Scanner;
     
    public class Quiz7
    {
    	public static void main(String[] args)
    	{
    		Scanner kb1 = new Scanner(System.in);
    		String programName;//
    		String dueDate;//
    		int radius;//
    		int length;//
    		int width;//
    		int height;//
     
    		System.out.print("What is the name of the program? ");
    		programName=kb1.nextLine();
    		System.out.print("What is the due date of the program? ");
    		dueDate=kb1.nextLine();
    		displayMyInfo(programName, dueDate);
    		int iMenuInput1 = menu();
     
     
     
     
    			switch(iMenuInput1)
    			{
    				case 1:
    						System.out.print("Enter the radius: ");
    						radius=kb1.nextInt();
    						double dAreaCircle;//
    						dAreaCircle = areaCircle(radius);
    						System.out.print("The area of the circle is" +dAreaCircle);
    						break;
    				case 2:
    						System.out.print("Enter the radius: ");
    						radius=kb1.nextInt();
    						double dCircumference;//
    						dCircumference = circumference(radius);
    						System.out.print("The circumference of the circle is " +dCircumference);
    						break;
    				case 3:
    						System.out.print("Enter the length: ");
    						length=kb1.nextInt();
    						System.out.print("Enter the width: ");
    						width=kb1.nextInt();
    						int iAreaRectangle;//
    						iAreaRectangle = areaRectangle(length, width);
    						System.out.print("The area of the rectangle is " +iAreaRectangle);
    						break;
    				case 4:
    						System.out.print("Enter the length: ");
    						length=kb1.nextInt();
    						System.out.print("Enter the width: ");
    						width=kb1.nextInt();
    						int iperimeterRectangle; //
    					    iperimeterRectangle = perimeterRectangle(length, width);
    						System.out.print("The perimeter of the rectangle is " +iperimeterRectangle);
    						break;
    				case 5: 
    				        System.out.print("Enter the length: ");
    						length=kb1.nextInt();
    						System.out.print("Enter the width: ");
    						width=kb1.nextInt();
    						System.out.print("Enter the height: ");
    						height = kb1.nextInt();
    						int iVolumeBox;//
    						iVolumeBox = volumeBox(length, width, height);
    						System.out.print("The volume of the box is " +iVolumeBox);
    						break;
     
    				case 6:
    						System.out.print("Enter the radius: ");
    						radius=kb1.nextInt();
    						System.out.print("Enter the height: ");
    						height=kb1.nextInt();
    						double dVolumeCone;
    						dVolumeCone = volumeCone(radius, height);
    						System.out.print("The volume of the cone is " +dVolumeCone);
    						break;
     
    				default:
    							System.exit(0);
    			}//end of switch
     
    	int count = 0;
    	while (count < 7)
    	{
    		count++;
    		menu();
     
    	}
    	}//end of main
     
     
    	public static void displayMyInfo(String programName, String dueDate)
    	{
    			System.out.print("The program name is " +programName);
    			System.out.print("\nThe due date of the program is " +dueDate);
    	}//end of displayMyInfo
     
    	public static int menu()
    	{
    		Scanner kb2 = new Scanner(System.in);
    		int iMenuInput2;
    		System.out.print("\n\n        * * * * * * * * M E N U * * * * * * * *");
    		System.out.print("\n\n1>    Area of a circle");
    		System.out.print("\n2>    Circumference of a circle");
    		System.out.print("\n3>    Area of a rectangle");
    		System.out.print("\n4>    Perimeter of a rectangle");
    		System.out.print("\n5>    Volume of a box");
    		System.out.print("\n6>    Volume of a cone");
    		System.out.print("\nEnter your choice ");
    		iMenuInput2=kb2.nextInt();
    		return iMenuInput2;
    	}//end of menu
     
    	public static double areaCircle(int radius)
    	{
     
    		final double PI = 3.1416f;
    		double dAreaCircle;
    		dAreaCircle = PI * radius * radius;
    		return dAreaCircle;
    	}//end of areaCircle
     
     
     
    	public static double circumference(int radius)
    	{
    		final double PI = 3.1416f;
    		float fCircumference;
    		fCircumference = (float)(2 * PI * radius);
    		return fCircumference;
    	}//end of circumference
     
    	public static int areaRectangle(int length, int width)
    	{
    		int iAreaRectangle;
    		iAreaRectangle = length * width;
    		return iAreaRectangle;
    	}//end of areaRectangle
     
    	public static int perimeterRectangle(int length, int width)
    	{
    		int iPerimeterRectangle;
    		iPerimeterRectangle = 2 *(length * width);
    		return iPerimeterRectangle;
    	}//end of perimeterRectangle
     
    	public static int volumeBox(int length, int width, int height)
    	{
    		int iVolumeBox;
    		iVolumeBox = length * width * height;
    		return iVolumeBox;
    	}//end of volumeBox
     
    	public static double volumeCone(int radius, int height)
    	{
    		final double PI = 3.1416f;
    		float fVolumeCone;
    		fVolumeCone = (float)(PI*radius*radius*radius*height)/3.0f;
    		return fVolumeCone;
     
    	}//end of volumeCone
     
     
     
     
     
    }//end of Quiz7

    With my current code setup, it repeats the menu 7 times, but does not allow me to input the formula any time after the first. Any help?

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: One last question: How to get menu to repeat!

    The idea is right but you're wrapping the while loop around only the menu() method, which just prints out the method and takes an input that doesn't do anything. I believe you just have to wrap it around the switch statement to loop it.

  5. The Following User Says Thank You to Actinistia For This Useful Post:

    CrimsonFlash (October 24th, 2011)

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: One last question: How to get menu to repeat!

    Thanks Actinistia, I'm finally on my way to understanding this!

  7. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: One last question: How to get menu to repeat!

    Mark SOLVED.
    Thanks

Similar Threads

  1. How to repeat a while loop?
    By BITmixit in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 12th, 2011, 12:51 PM
  2. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 11:56 PM
  3. Wanting to repeat my program...
    By Shaybay92 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 8th, 2011, 08:29 AM
  4. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 08:19 AM
  5. repeat creation of text fields
    By kurt-hardy in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 12:05 PM