我需要在我的Java代码中添加一些东西

时间:2015-07-02 11:40:18

标签: java eclipse

import javax.swing.*;
import java.text.*;
import java.util.*;
public class UG3 {
    private static Scanner sc;

        public static void main (String[] arg) {

            String indata = JOptionPane.showInputDialog("Ange cylinderns" + " radie och höjd i CM:"); // inputs information, radius first and heights comes after.
            sc = new Scanner(indata);

            double radie  = sc.nextDouble(); // radius
            double Höjd = sc.nextDouble(); // height
            double volym = Math.PI * Math.pow(radie,2) * Höjd;

            NumberFormat r = NumberFormat.getInstance();

            r.setMaximumFractionDigits(2);
            r.setMinimumFractionDigits(2);

            JOptionPane.showMessageDialog(null, "Volymen av cylindern är "+ r.format(volym) +"CM^3"); // outputs the results
    } //main
} //Cylinder

这是输入信息的代码,结果是圆柱的体积。

但我想补充一点。

该类必须有一个单独的方法来解决音量。

程序应该询问,计算是否需要解决更多的气瓶。

我不确定我该怎么做。我从来没有写过比一堂课更大的课程。 :S

2 个答案:

答案 0 :(得分:0)

我发现扫描仪在这种情况下毫无用处。去PANE或回家!我不知道您在代码中使用了哪种语言,因此我根据您的规范将其翻译成英语。我使用了相同的结构,但摆脱了一些非常过时和无用的变量。如果您需要关于我整周都会在这里做什么的信息!

import javax.swing.*;
import java.text.*;
public class Help_Input {
        public static void main (String[] arg) {

            String radius = JOptionPane.showInputDialog("Input Radius");
            String height = JOptionPane.showInputDialog("Input Height");// inputs information, radius first and heights comes after.

            int rad = Integer.parseInt(radius);
            int hei = Integer.parseInt(height);
            double volume = Math.PI * Math.pow(rad,2) * hei;

            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"); // outputs the results
            int ag = JOptionPane.showConfirmDialog(null, "Would you like to find the volume of a cylinder once more?", "", JOptionPane.YES_NO_OPTION);
            if(ag == JOptionPane.YES_OPTION){
                main(arg);
            };
    } //main
//Cylinder
}

答案 1 :(得分:0)

import javax.swing。; import java.text。;

公共课UG3 {

public static void main (String[] arg) {



        String radius = JOptionPane.showInputDialog("Input Radius");
        String height = JOptionPane.showInputDialog("Input Height");// input radius and height.

    if (radius >= 1 && height >= 1) {  // trying to make sure that the user inputs information otherwise it will cancel.

        int rad = Integer.parseInt(radius);
        int hei = Integer.parseInt(height);
        double volume = Math.PI * Math.pow(rad,2) * hei;

        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.

        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);
        }
    }


    else { // cancel.

        JOptionPane.showMessageDialog(null, "Sorry but you have to enter radius and height to complete the task."); 
    }




 } //main
// trying to solve the problem in a different method.
/* 
 public static double getVolume(double radius, double height){
     double volume = Math.PI * Math.pow(radius,2) * height;
     return(volume);
 }
*/

} //气缸

将我的代码更改为你的代码并尝试再添加2个,我希望它没问题?

尝试用新方法解决音量,但不知道我是怎么做的。不知何故,我陷入了第一个If声明。