Ich versuche ein Programm zu schreiben, das entscheiden kann, welchen Mechanismus eine organische Reaktion durchläuft, indem ich eine Reihe von if else- und switch-Anweisungen verwende.Wie benutze ich switch Statements innerhalb von if else Statements
Könnt ihr mir helfen, herauszufinden, was ich hier falsch mache? Ich habe ein Problem damit, die erste if else-Anweisung zu erhalten. Das Programm läuft auf meinem Computer (ich benutze den BlueJ-Editor), aber wenn ich auf die erste Frage "Ist es löslich in Lösung?" Standardmäßig wird die else-Anweisung verwendet. Die switch-Anweisungen im Inneren der if else-Anweisung funktionieren von selbst.
Kann ich Schaltanweisungen in if else Anweisungen verwenden? Gibt es einen einfacheren Weg dies zu programmieren?
Können Sie auch erklären, warum es nicht funktioniert, oder warum eine andere Methode effizienter wäre?
Dank einer Tonne :)
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");
}
}
}
Verwenden Sie Java 8? – Sweeper
Haben Sie bereits den Wert von "Solubility" gedruckt und überprüft? – JanLeeYu