2017-04-24 18 views
0

如果我嘗試輸入「oz」或「OZ」else else語句似乎不會繼續,我不知道爲什麼。任何幫助,將不勝感激!謝謝!!!我似乎無法追加我的else語句

Scanner input = new Scanner(System.in); 
    StringBuilder sb = new StringBuilder(); 
    String Ma = "mass" + "MASS"; 
    String La = "oz" + "OZ"; 

     System.out.println("Hi, what are you trying to find?"); 
     System.out.println("mass"); 
     System.out.println("vol"); 
     System.out.println("temp"); 
     System.out.println("sphere"); 
     System.out.println("density"); 
     String convert = input.nextLine(); 

     if (Ma.contains(sb.append(convert))) { 
      System.out.println("You are trying to convert mass."); 
      System.out.println("Type the unit of measurement you are trying to convert to."); 
      System.out.println("If you are trying to find mass type mass."); 
      System.out.println("pound"); 
      System.out.println("ounce"); 
      System.out.println("ton"); 
      System.out.println("gram"); 
      System.out.println("mass"); 
      String mass = input.nextLine(); 
     } 
     else if (La.contains(sb.append(convert))) { 
       System.out.println("34223412351Type the number of oz."); 
       double oz = input.nextDouble(); 
       System.out.println(oz + " oz equal to " + oz/16 + " lb."); 
       System.out.println(oz + " oz equal to " + oz/32000 + " ton."); 
       System.out.println(oz + " oz equal to " + oz * 0.02835 + " kg."); 
       System.out.println(oz + " oz equal to " + oz * 28.35 + " g."); 
       System.out.println(oz + " oz equal to " + oz * 2835 + " cg."); 
       System.out.println(oz + " oz equal to " + oz * 28350 + " mg."); 
     } 
    } 
} 

回答

0

爲什麼你需要,你可以直接使用Ma.contains(轉換)和La.contains(轉換)StringBuilder的,這將運行,如果其他正常。

它在您的情況下不運行盎司的原因是字符串生成器正在追加'盎司'兩次。 1.當它檢查在Ma 中看到2.當它檢查La 中看到所以在La你的sb包含'ozoz',這就是爲什麼else從未運行。

如果您需要使用stringbuilder,請使用sb.append從掃描儀讀取字符串,而不是在if條件下