2016-03-29 2 views
0

一連の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"); 
     } 
    } 
} 
+0

Java 8を使用していますか? – Sweeper

+0

'Solubility'の値をすでに印刷して検証しましたか? – JanLeeYu

答えて

0

それは、あなたと私はしばらくの間に一度行いますこれらのミスの別の一つです。

短い回答:==を使用して文字列を比較することはできません!

長い答え:あなたは

if文、あなたは==で文字列を比較しています。 neverEVERこれを行う必要があります。 ==は、プリミティブでない場合は、2つのオペランドのメモリアドレスを比較します。私はあなたが2つの文字列の文字が同じであるかどうかをチェックしたいと思います。しかし、同じ文字を持つ2つの文字列は、同じメモリアドレスを持たないかもしれません!

if (Solubility.equals("yes")) 

ます。またequalsIgnoreCaseメソッドを使用することができます:あなたは何をすべき

はこのように、文字列を比較するequalsメソッドを使用しています。それはそれが蓋の上で言うことをします。他のif文もすべて変更することを忘れないでください!

さらに、switch文を使用して文字列を切り替えることはできません。しかし、あなたはコンパイラのエラーを受け取っていないのを見て、私はあなたがJava 8を使用していると思う。

Java 8を使用していない場合、これを解決する最も良い方法はCharacterを切り替えることです。

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

これが最大の問題ではありませんが、それはまだ修正価値がある:

あなたは一度だけScannerをインスタンス化する必要があります。

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")) 
+0

これは主な問題だとは思わない。あなたは '溶解度=="はい "がもっと間違っていると思いませんか? – Sweeper

+0

はい、あなたは正しいです@スイーパーは、そこに着いていました。私は答えを更新する前に他の問題があるかどうか確認しようとしていました。 – pczeus

関連する問題