简体   繁体   中英

What am I doing wrong in a string to double conversion?

I just bought a book on Java coding after taking a class a few years ago. I decided to start writing a program to get back in the swing of things. Anyways, my program to calculate the volume of a cylinder is having issues. Could you please critique it and tell me what's going on? If it's any help, I'm using 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 errors

1) it's String not string 2) double.parsedouble not correct it should be Double.parseDouble don't forget java is case sensitive and method names have 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+".");
    }
}

您可能要使用Double.parseDouble(input)而不是double.parsedouble(input)

Try

Double.parseDouble(0.0);

Use Double as an object, not a primitive. The call on the object, returns the primitive double .

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

I think your code should look like follwing: 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+".");
    }
}

There are couple of changes that I've made to it.

  1. Its String and not string .
  2. To parse double you should use Double class as double is primitive type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM