mardi 8 mai 2018

code fails to iterate through if statement [duplicate]

This question already has an answer here:

Upon running the code, and making a choice of what i'd like to calculate, the code terminates. It doesn't seem to be iterating through the if statement and because i am so new to java i don't know why...please help!

import java.util.*; public class test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("What would you like to calculate?: ");
    Scanner scanner = new Scanner(System.in);
    String choice = scanner.nextLine();
    if (choice == "speed"){
        System.out.println("You chose to find: " + choice );
        System.out.println("Enter the time taken: ");
        double time = scanner.nextDouble();
        System.out.println("Enter the distance travelled: ");
        double distance = scanner.nextDouble();

        double speed;

        speed = distance/time;

        System.out.println("Your speed was: " + speed);}

    else if (choice == "distance"){
        System.out.println("You chose to find: " + choice );
        System.out.println("Enter the time taken: ");
        double time = scanner.nextDouble();
        System.out.println("Enter the speed: ");
        double speed = scanner.nextDouble();

        double distance;

        distance = speed*time;

        System.out.println("Your distance travelled was: " + distance);}

    else if (choice == "time"){
        System.out.println("You chose to find: " + choice );
        System.out.println("Enter the distance travelled: ");
        double distance = scanner.nextDouble();
        System.out.println("Enter the speed: ");
        double speed = scanner.nextDouble();

        double time;

        time = distance/speed;

        System.out.println("Your distance travelled was: " + time);}
}

}

Aucun commentaire:

Enregistrer un commentaire