mardi 11 juillet 2017

while loop keeps ending without initiating the If Statement

I'm trying to create a simple calculator program on java as a simple first project as i learn java.

Problem: After i run the program and after doing my calculation, i wanted to give the user the option on whether they want to end the program or do some more calculations. for some reason the program is not using the (If) statements that i have placed in it, it seems to skip it and end my program without allowing the user to input his choice at the end.

I'm really sorry if my question is not clear, i couldn't find a solution for my problem online, and i do apologise if my code looks really messy.

Thank guys

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double fnum, snum, answer;
        String choice, name, in;



        System.out.println("Hello, whats your name?");
        name = input.nextLine();
        System.out.println("");
        System.out.println("Oh so your name is " + name + "!");
        System.out.println("");
        System.out.println("This program is a calculator that will do simple calculation of two numbers");
        System.out.println("There are four different options to choose from:");

        boolean running = true;

        CAL:
            while(running ) {
                System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division");
                System.out.println("Then press enter!");


                choice = input.nextLine();

                while (!choice.equals("a") && !choice.equals("A") &&!choice.equals("b")&&!choice.equals("B")
                        &&!choice.equals("c")&&!choice.equals("C")&&!choice.equals("d")&&!choice.equals("D")) {
                    System.out.println("Wrong choice, Please try again");
                    System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division");

                    choice = input.nextLine();
                }

                if(choice.equals("a") || choice.equals("A")) {

                    System.out.println(name +" You have chosen Addition");
                    System.out.println("Type the first number:");
                    fnum = input.nextDouble();
                    System.out.println("Type the second number:");
                    snum = input.nextDouble();

                    System.out.println("your answer is:");
                    Addition addy = new Addition(fnum,snum);
                    System.out.println(addy.getans());

                }

                if(choice.equals("b") || choice.equals("B")) {

                    System.out.println(name +" You have chosen Subtraction");
                    System.out.println("Type the first number:");
                    fnum = input.nextDouble();
                    System.out.println("Type the second number:");
                    snum = input.nextDouble();
                    answer = fnum - snum;
                    System.out.println("your answer is:"
                            + answer);

                }



                if(choice.equals("c") || choice.equals("C")) {

                    System.out.println(name +" You have chosen Multiplication");
                    System.out.println("Type the first number:");
                    fnum = input.nextDouble();
                    System.out.println("Type the second number:");
                    snum = input.nextDouble();
                    answer = fnum * snum;
                    System.out.println("your answer is:"
                            + answer);

                }

                if(choice.equals("d") || choice.equals("D")) {

                    System.out.println(name +" You have chosen Addition");
                    System.out.println("Type the first number:");
                    fnum = input.nextDouble();

                    while (fnum == 0) {
                        System.out.println("invalid try again!");
                        fnum = input.nextDouble();
                    }

                    System.out.println("Type the second number:");
                    snum = input.nextDouble();
                    answer = fnum / snum;


                    System.out.println("your answer is:"
                            + answer);


                }


                System.out.println("");
                System.out.println("Thank you " + name +  " for using this simple calculator :)");

                System.out.println("If you would like to try again press a the press Enter, if you wish to exit press any botton and then press enter");

                in = input.nextLine();

                if (in.equals("a")) {
                    System.out.println("");
                    System.out.println("Thank you, please choose again");
                    System.out.println("");
                }else {
                    System.out.println("Thank you and goodbye");
                    break;
                }



                }


            }

}

Aucun commentaire:

Enregistrer un commentaire