vendredi 1 mai 2015

Java If Statement issue [duplicate]

This question already has an answer here:

I have this code in Java to give some information about either a circle, a triangle or a quadrilateral. I have specified an if statement, which I'm going to add more information to.

I don't know whether an if statement is the right way to let the user choose if he wants to have information about a circle, triangle or rectangle. Because I actually wanted to have some kind of buttons each for the circle, triangle and rectangle. So if you have any tips on that I would really appreciate it.

This is the code I'm having trouble with, everytime I type in 'cirkel' which I have mentioned in the if statement, it goes on to the else statement and I get a dialog saying 'bye'.

import java.util.*;
import javax.swing.JOptionPane;

public class Vormen {

    public static void main(String[] args){
        String vorm;
        double diameter;
        double r;
        double omtrek;
        double oppervlakte;

        Scanner keyboard = new Scanner(System.in);
        vorm = JOptionPane.showInputDialog("Kiest u voor de 'cirkel', 'driehoek' of 'vierhoek'?");

        if (vorm == "cirkel"){
            diameter = diameter = Double.parseDouble(JOptionPane.showInputDialog("Wat is de diameter van de cirkel (in m)"));
            r = diameter / 2;
            omtrek = 2 * Math.PI * r;
            oppervlakte = Math.PI * r * r;
            JOptionPane.showMessageDialog(null, "De straal = " + r + " m\n\n" + "De omtrek = " + omtrek + " m\n\n" + "De oppervlakte = " + oppervlakte + " m^2");
        }

        else {
            JOptionPane.showMessageDialog(null, "bye");
        }

    }

}

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire