[英]How do I use switch statements inside if else statements
我正在嘗試編寫一個程序,該程序可以使用一系列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");
}
}
}
最好將switch語句用於多種輸入類型-即使在if語句中也可以。
您的問題是您不斷重新初始化Scanner對象。
初始化掃描儀后:
Scanner keyboard = new Scanner(System.in);
然后在您希望接收輸入的其他地方,只需重新使用它即可:
//keyboard = new Scanner(System.in); // You don't need this line
Base = keyboard.next();
同樣,您從未輸入if語句的原因是您將“ Solability”( Solubility
與“ Yes”(是)進行比較的方式。 對於字符串,如果大小寫無關緊要,則應使用equals()
或equalsIgnoreCase
。
將if語句更改為以下行,您的代碼將按預期工作:
if ( Solubility.equalsIgnoreCase("yes"))
這是您和我偶爾會犯下的另一錯誤。
簡短的答案: 您不能使用==比較字符串!
長答案:
在if語句中,您正在將==
字符串進行比較。 你永遠也不會做。 ==
比較兩個操作數的內存地址(如果它們不是基元)。 我知道你要檢查,如果兩個字符串中的字符是相同的。 但是,兩個具有相同字符的字符串可能沒有相同的內存地址!
您應該做的是使用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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.