我需要添加十进制位置,我知道如何编写它。我修改了这段代码并尝试了不同的东西,这就是为什么我的其他陈述与if不同。
另外,我会通过GUI进行扫描。无论如何,当我选择if语句然后它在控制台中显示结果,然后转到else语句。我尝试了几件事,但无法弄清楚原因。
import java.text.DecimalFormat;
import javax.swing.*;
public class MathFormula {
public static void main(String args[]) {
//Declarations
double getNumDouble;
double volumeSphere;
String getNum;
double area;
double volume;
String getUserDecision; //Declared getUserDecision for outer loop decision
DecimalFormat df = new DecimalFormat("####0.000");
java.util.Scanner input = new java.util.Scanner(System.in);
//End Declarations
// Greeting
greeting();
System.out.println(" "); //Left blank for clarity
// Ask user to start the program (the outer loop)
getUserDecision = decisionOuter();
while (getUserDecision.equalsIgnoreCase("Y")) {
getUserDecision = decisionInner();
if (getUserDecision.equalsIgnoreCase("S")) {
System.out.print("Enter you number for volume of sphere: ");
getNum = input.next();
getNumDouble = Double.parseDouble(getNum);
volSphere(getNumDouble);
} else if (getUserDecision.equalsIgnoreCase("C"))
System.out.println(" ");
area = arCircle();
System.out.println("you have worked: " + df.format(area));
getUserDecision = decisionOuter();
}
goodbye();
}
public static String decisionOuter() //Outerloop decision
{
String userDecisionString;
userDecisionString = JOptionPane.showInputDialog("Would you like to use the program Y/N: ");
return userDecisionString;
}
public static String decisionInner() //Inner loop decision
{
String userDecisionString2;
userDecisionString2 = JOptionPane.showInputDialog("C or S: ");
return userDecisionString2;
}
public static void volSphere(double getNumDouble) //Vol formula
{
double volumeSphere;
volumeSphere = (4 * Math.PI * getNumDouble * getNumDouble * getNumDouble) / 3;
System.out.println("entered " + getNumDouble + "returned" + volumeSphere);
}
public static double arCircle() //area formula
{
double getNum;
double areaCircle;
String getNumString;
getNumString = JOptionPane.showInputDialog("Enter you number for radius of circle: ");
getNum = Double.parseDouble(getNumString);
areaCircle = (getNum * getNum * Math.PI);
return areaCircle;
}
public static void greeting() //greeting
{
final String msgGreet = "Welcome.";
System.out.println(msgGreet);
}
public static void goodbye() //goodbye
{
final String msgBye = "goodbye.";
System.out.println(msgBye);
}
}
答案 0 :(得分:1)
因为你还没有包含围绕其他内容的curley括号。它只考虑'System.out.println(“”);'作为你的else语句,它下面的所有其他语句都被执行,如果你想要排除,那么在else条件中包含你想要的代码周围的curley括号
//...
}
else if (getUserDecision.equalsIgnoreCase("C"))
{
System.out.println(" ");
area = arCircle();
System.out.println("you have worked: " + df.format(area));
}
getUserDecision = decisionOuter();
//...