Eli*_*i K 1 java loops if-statement while-loop
我是编程的新手,我现在正在我的高中学习入门课程。我们现在正在做的一项任务是一个程序,它询问用户他们想要计算什么公式(各种面积/体积/等类型的公式),询问他们进行计算所需的数据,然后给他们答案。我之所以对编程非常感兴趣,主要是因为这项任务,因为我已经意识到这项工作有多大的脑筋急转弯。
有了这个,我决定超越一点。我清理了我凌乱的代码,现在我正在尝试创建一个 while 循环,允许用户继续计算公式而无需再次运行程序,并在他们输入不正确的数据时给他们一条错误消息。后面的部分我已经想通了,但是我想在将第二个问题的解决方案放入代码之前实现前者。
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));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一段时间线,但我不确定如何处理它。我不知道如何让我的代码循环 while (hehe, while) 活动行在 while 块内。我应该保存这个类,创建一个新类,然后在另一个类中引用它,比如
字符串 mathDone = true (在每个等式的末尾)
while(mathDone.equalsIgnoreCase(true))
{
String continueCalculation = input.nextLine;
System.out.println("Would you like to continue calculation?");
if(continueCalculation.equalsIgnoreCase("yes"))||(continueCalulation.equalsIgnoreCase("y"))
Run Code Online (Sandbox Code Playgroud)
{(无论运行命令是什么)formulaRemake } }
除此之外,我有点不知所措。我知道有关于类似主题的 stackoverflow 的帖子,但我不知道如何将它们应用于我的情况。我太新了(显然,太愚蠢了)无法弄清楚如何使用这些帖子来帮助我。
使用 continue 和 break 语句,例如:
i=input;
while(true)
{
if(i==1){break;}
else
{
i=input;
continue;
}
}
Run Code Online (Sandbox Code Playgroud)