java - 我在字符串到 double 转换中做错了什么?

标签 java string swing double

几年前参加完类(class)后,我刚刚买了一本关于 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+".");
    }
}

最佳答案

2 个错误

1) 它是字符串而不是字符串 2) double.parsedouble 不正确,应该是 Double.parseDouble 不要忘记java是区分大小写并且方法名称具有驼峰式大小写

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+".");
    }
}

关于java - 我在字符串到 double 转换中做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27644510/

相关文章:

java - 如何在 Prowide Core (WIFE) 中设置 MT518 swift 消息模型对象的方向

java - 数据库值分离

java - 接口(interface)常量的使用

javascript - 通过http请求发送字符串

java - GUI 不更新简单游戏中的值

java - 在 JMapViewer 中绘制折线

java - 请建议我一个合适的 Java 数据类型

java - 从数组列表中删除最后一个随机字符串

java - 有没有办法让 JSlider(禁用用户输入)在 Java 中保持正常可见性?

c - 如何使用 C 中的插入排序为每一行提供有序索引来对文件中的字符串进行排序