One last question: How to get menu to repeat!
Code :
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 :)
Re: One last question: How to get menu to repeat!
You have a loop. Why don't you use it?
Re: One last question: How to get menu to repeat!
Code :
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?
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.
Re: One last question: How to get menu to repeat!
Thanks Actinistia, I'm finally on my way to understanding this!
Re: One last question: How to get menu to repeat!