我在字符串中做错了双重转换?

时间:2014-12-25 05:44:20

标签: java string swing double

几年前我上完课后,我刚买了一本关于Java编码的书。我决定开始编写一个程序来重新开始。无论如何,我计算气缸体积的程序存在问题。你能批评它并告诉我发生了什么吗?如果有任何帮助,我正在使用JCreator。

import javax.swing.*;
public class Cylinder_Volume
{
    public static void main (String[] args)
    {
        string input;
        input=JOptionPane.showInputDialog("What is the radius of the cylinder?");
        double r;
        r=double.parsedouble(input);
        input=JOptionPane.showInputDialog("What is the height of the cylinder?");
        double h;
        h=double.parsedouble(input);
        double pi=3.1415926535;
        double volume=pi*r*r*h;
        System.out.println("The volume of the cylinder is: "+volume+".");
    }
}

5 个答案:

答案 0 :(得分:3)

2个错误

1)它是String而非string 2)double.parsedouble不正确它应该是Double.parseDouble 不要忘记java是case sensitive,方法名称有camel case

import javax.swing.*;
public class Cylinder_Volume
{
    public static void main (String[] args)
    {
        String input;//first error you have types string //s should be capital
        input=JOptionPane.showInputDialog("What is the radius of the cylinder?");
        double r;
        r=Double.parseDouble(input);//2nd problem
        input=JOptionPane.showInputDialog("What is the height of the cylinder?");
        double h;
        h=Double.parseDouble(input);
        double pi=3.1415926535;
        double volume=pi*r*r*h;
        System.out.println("The volume of the cylinder is: "+volume+".");
    }
}

答案 1 :(得分:0)

您可能希望使用Double.parseDouble(input)代替double.parsedouble(input)

答案 2 :(得分:0)

尝试

Double.parseDouble(0.0);

使用Double作为对象,而不是基元。对对象的调用返回原语double

答案 3 :(得分:0)

import javax.swing.*;
public class Cylinder_Volume
{
    public static void main (String[] args)
    {
        String input;
        input=JOptionPane.showInputDialog("What is the radius of the cylinder?");
        Double r;
        r=Double.parseDouble(input);
        input=JOptionPane.showInputDialog("What is the height of the cylinder?");
        Double h;
        h=Double.parseDouble(input);
        Double pi=3.1415926535;
        Double volume=pi*r*r*h;
        System.out.println("The volume of the cylinder is: "+volume+".");
    }
}

答案 4 :(得分:0)

我认为您的代码应如下所示: import javax.swing。*;

public class CandidateCode
{
    public static void main (String[] args)
    {
        String input;
        input=JOptionPane.showInputDialog("What is the radius of the cylinder?");
        double r=Double.parseDouble(input);
        input=JOptionPane.showInputDialog("What is the height of the cylinder?");
        double h=Double.parseDouble(input);;
        double pi=3.1415926535;
        double volume=pi*r*r*h;
        System.out.println("The volume of the cylinder is: "+volume+".");
    }
}

我对它做了一些改变。

  1. String而非string
  2. 要解析double,您应该使用Double类,因为double是原始类型。