import javax.swing.*;
import java.text.*;
public class UG3 {
public static void main (String[] arg) {
String radius = JOptionPane.showInputDialog("Input Radius");
String height = JOptionPane.showInputDialog("Input Height");// input radius and height.
try { // trying to make sure that the user inputs information otherwise it will cancel.
int rad = Integer.parseInt(radius);
int hei = Integer.parseInt(height);
NumberFormat r = NumberFormat.getInstance();
r.setMaximumFractionDigits(2);
r.setMinimumFractionDigits(2);
JOptionPane.showMessageDialog(null, "The volume of the cylinder is approximately "+ r.format(volume) +"CM^3"); // output results. // Here is the problem " volume " is: "cannot be resolved".
int yes = JOptionPane.showConfirmDialog(null, "Would you like to find the volume of a cylinder once more?", "", JOptionPane.YES_NO_OPTION);
if(yes == JOptionPane.YES_OPTION) {
main(arg);
}
else {
JOptionPane.showMessageDialog(null, "You'r welcome! \n \n Click 'OK' to exit", "Cylinder.", JOptionPane.ERROR_MESSAGE);
}
}
catch (NumberFormatException e) { // cancel.
JOptionPane.showMessageDialog(null, "Sorry but you have to enter radius and height to complete the task.");
}
} //main
public static double volume(double radius, double height){
return Math.PI * Math.pow(radius,2) * height;
}
}//Cylinder
我寫了「評論」,告訴你在哪裏。無論如何,「音量無法解決」爲什麼? 我是否想要從第一種方法中刪除某些東西?有什麼缺失?不能讓我的頭靠近它。 :S我如何計算在Java中的不同方法我的氣瓶體積
您從未將半徑和高度解析/轉換爲數字(在if語句之前)。事實上,你*應該會得到一個錯誤'二元運算符的壞操作數類型'>''...那是你得到的? *總是*包含錯誤,如果你有一個。 – tnw
謝謝!但我設法解決它與嘗試-catch :)我如何解決新方法中的體積計算? :D –
您是否嘗試使用您已經定義的方法? – tnw