一連のif else文とswitch文を使って、どのようなメカニズムが有機的な反応を起こすかを決めるプログラムを書こうとしています。else文の中でswitch文を使うにはどうすればいいですか
ここで私が間違っていることを理解してもらえますか?私は最初にelse文を動作させるのに問題があります。このプログラムは私のコンピュータ(私はBlueJエディタを使用しています)で動作しますが、最初の質問に「私はそれがソリューションに溶けていますか?デフォルトはelseステートメントになります。 if else文の内部のswitch文は、それ自身で正常に動作します。
else文の中で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");
}
}
}
Java 8を使用していますか? – Sweeper
'Solubility'の値をすでに印刷して検証しましたか? – JanLeeYu