mardi 29 mars 2016

How do I use switch statements inside if else statements

I'm trying to write a program that can decide what mechanism a organic reaction will go through using a series of if else and switch statements.

Could you guys help me figure out what I'm doing wrong here? I'm having a problem getting the first if else statement to work. The program runs on my computer(I'm using the BlueJ editor), but when I respond to the first question "Is it soluble in solution?" it defaults to the else statement. The switch statements on the inside of the if else statement works fine by itself.

Can I use switch statements inside if else statements? Is there an easier way to program this?

Could you also explain why it doesn't work, or why another method would be more efficient?

Thanks a ton :)

import java.util.Scanner;
      /**
        * This program will decide what mechanism a reaction will undergo given information about the reactants.
        * I will also include a mechanism to give a rudimentary explanation of the decision making process to
       * get the reaction mechanism.
       */
public class mechanism
{
    public static void main(String[] args)
    {
        System.out.println("Hello, this program is designed to figure out what mechanism a reaction will under go.");
        //The decision tree will be a series of if-else statements. If I find a better method, I will use that

        System.out.println("Is the reactant soluble in the solvent? Answer in yes or no.");

        Scanner keyboard = new Scanner(System.in);

        String Solubility = keyboard.next(); //Defines if the reactant is soluble in the solvent
        String functional = "unassigned";//Defines if the functional roup is primary secondary or tertiary
        String Base = "unassigned";//Defines the strength of the base if needed
        String Polar = "unassigned";//Defines if the reactant is polarizable
        String Solvent = "unassigned"; //Defines if the solvent is protic or aprotic

        if ( Solubility == "yes" )
        {

            System.out.println("Is the functional group attached to a primary, secondary, or tertiary carbon?");
            System.out.println(" Answer in p for primary, s for secondary, and t for tertiary.");

            keyboard = new Scanner(System.in);
            functional = keyboard.next();

                  switch (functional){
                      case "p":   System.out.println("All unimolecular reactions are ruled out, leaving E2 and Sn2.");
                            System.out.println("Is the reactant a strong base? Answer in y for yes or n for no");

                            keyboard = new Scanner(System.in);
                            Base = keyboard.next();
                                if (Base == "y" ){
                                    System.out.println("The reaction undergoes E2");
                            }    else{
                                    System.out.println("The reaction undergoes Sn2");
                        }

                            break;
                        case "s":  System.out.println("No reactions have been ruled out.");
                                   System.out.println("Is the reactant a strong base? Answer in y or n");

                                   keyboard = new Scanner(System.in);
                                   Base = keyboard.next();
                                   if( Base == "y" ){
                                       System.out.println("yay");
                                    } else {
                                        System.out.println("whatever");
                                    }
                                   break;
                        case "t": System.out.println("tertiary");
                            break;

                    }
        } 
        else{
            System.out.println("No reaction will occur");
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire