如何在else语句中使用switch语句

时间:2016-03-29 23:56:40

标签: java if-statement switch-statement

我正在尝试编写一个程序,可以使用一系列if else和switch语句来决定有机反应将通过哪种机制。

你们能帮助我弄清楚我在做错了什么吗?我在第一个if else语句工作时遇到了问题。程序在我的计算机上运行(我正在使用BlueJ编辑器),但是当我回答第一个问题“它是否可溶于解决方案?”时它默认为else语句。 if else语句内部的switch语句本身可以正常工作。

我可以在if语句中使用switch语句吗?是否有更简单的方法来编程?

你能解释为什么它不起作用,或者为什么另一种方法会更有效率?

非常感谢:)

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

2 个答案:

答案 0 :(得分:0)

对多种输入类型使用switch语句是完全没问题的 - 即使在if语句中也是如此。

您的问题是您不断重新初始化Scanner对象。

初始化扫描仪后:

 Scanner keyboard = new Scanner(System.in);

然后在其他地方,您希望接收输入,只需重复使用它:

//keyboard = new Scanner(System.in); // You don't need this line
Base = keyboard.next();

此外,您永远不会输入if语句的原因是您将Solubility与'Yes'进行比较的方式。对于字符串,如果情况无关紧要,则应使用equals()equalsIgnoreCase

将if语句更改为以下行,您的代码将按预期工作:

    if ( Solubility.equalsIgnoreCase("yes"))

答案 1 :(得分:0)

这是你和我偶尔犯过的另一个错误。

简短回答:您不能使用==来比较字符串!

答案很长:

在if语句中,您要将字符串与==进行比较。您应该从不 EVER 这样做。 ==比较两个操作数的内存地址(如果它们不是原语)。我知道您要检查两个字符串的字符是否相同。但是两个具有相同字符的字符串可能没有相同的内存地址!

您应该使用equals方法比较字符串,如下所示:

if (Solubility.equals("yes"))

您还可以使用equalsIgnoreCase方法。它按照盖子上的说法做了。请记住也要更改所有其他if语句!

此外,您不能使用switch语句来切换字符串。但是看到你没有收到任何编译器错误,我认为你使用的是Java 8。

但是如果您不使用Java 8,IMO解决此问题的最佳方法是切换Character

char functionalChar = functional.charAt(0);
switch (functionalChar) {
    case 'p': // remember to use single quotes!
    ...
}

虽然这不是最大的问题,但仍值得纠正:

您只需要实例化Scanner一次。