I'm decently new to programming, and I'm taking an introductory course at my high school right now. One of the assignments we're working on right now is a program that asks the user what formula they would like to calculate (various area/volume/etc types of formulas), asks them for the data needed to make the calculation, and then gives them the answer. I've gotten really interested in programming mostly due to this assignment, because I've realized just how much of a brain teaser this work can be.
With that, I decided to go a little bit above and beyond. I cleaned up my messy code, and I'm now trying to make a while loop that will allow the user to continue calculating formulas without running the program again, as well as give them an error message when they input improper data. I've figured out the latter part, but I want to implement the former before putting the solution to the second issue into the code.
import java.util.Scanner;
public class FormulaRemake {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n","What formula would you like to calculate?","Area of a Circle (AOC)","Circumference of a Circle (COC)","Area of a Trapezoid (AOT)","Volume of a Cylinder (VOC)","Volume of a Sphere (VOS)","Volume of a Cone (VON)");
String formula = input.nextLine();
while((formula.equalsIgnoreCase("AOC"))||(formula.equalsIgnoreCase("COC"))||(formula.equalsIgnoreCase("AOT"))||(formula.equalsIgnoreCase("VOC"))||(formula.equalsIgnoreCase("VOS"))||(formula.equalsIgnoreCase("VON")))
{
if(formula.equalsIgnoreCase("AOC"))
{
System.out.println("What is the circle's radius?");
double inputRadius = input.nextDouble();
System.out.printf("%.2f",Math.PI*(Math.pow(inputRadius,2)));
}
if(formula.equalsIgnoreCase("COC"))
{
System.out.print("What is the circle's radius?");
double inputRadius = input.nextDouble();
System.out.printf("%.2f",2*Math.PI*inputRadius);
}
if(formula.equalsIgnoreCase("AOT"))
{
System.out.println("What is the height of the trapezoid?");
double inputHeight = input.nextDouble();
System.out.println("What is the first length of the trapezoid?");
double inputLengthFirst = input.nextDouble();
System.out.println("What is the second length of the trapezoid?");
double inputLengthSecond = input.nextDouble();
System.out.printf("%.2f",(1/2)*inputHeight*(inputLengthFirst+inputLengthSecond));
}
if(formula.equalsIgnoreCase("VOC"))
{
System.out.println("What is the cylinder's radius?");
double inputRadius = input.nextDouble();
System.out.println("What is the cylinder's height?");
double inputHeight = input.nextDouble();
System.out.printf("%.2f",(Math.PI*(Math.pow(inputRadius,2)*inputHeight)));
}
if(formula.equalsIgnoreCase("VOS"))
{
System.out.println("What is the sphere's radius?");
double inputRadius = input.nextDouble();
System.out.println(( 4.0 / 3.0 ) * Math.PI * Math.pow( inputRadius, 3 ));
System.out.printf("%.2f",(4.0/3.0)*Math.PI*Math.pow(inputRadius, 3));
}
if(formula.equalsIgnoreCase("VON"))
{
System.out.println("What is the cone's radius?");
double inputRadius = input.nextDouble();
System.out.println("What is the cone's height?");
double inputHeight = input.nextDouble();
System.out.printf("%.2f",(1.0/3.0)*Math.PI*(Math.pow(inputRadius,2)*inputHeight));
}
}
}
}
I have a while line, but I'm not really sure what to do with it. I don't know how to make my code loop while (hehe, while) the active line is within the while block. Should I be saving this class, creating a new class, then referencing it in another class, like
string mathDone = true (at the end of every equation)
while(mathDone.equalsIgnoreCase(true))
{
String continueCalculation = input.nextLine;
System.out.println("Would you like to continue calculation?");
if(continueCalculation.equalsIgnoreCase("yes"))||(continueCalulation.equalsIgnoreCase("y"))
{ (whatever the run command is goes here) formulaRemake } }
asides from that, I'm kind of clueless. I know there are posts on stackoverflow on a similar topic, but I can't figure out how to apply them to my situation. I'm too new (and clearly, too stupid) to figure out how to use those posts to help me.
Aucun commentaire:
Enregistrer un commentaire