2017-04-24 8 views
0

"oz"または "OZ"にelse文を入力しようとするとdoes notが進行しているように見えますが、なぜそれはわかりません。どんな助けもありがとう!感謝!!!あなたが直接Ma.contains(変換)とLa.contains(変換)を使用することができますStringBuilderのを必要としないのはなぜ私は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

、これは適切であれば、他に実行されます。

あなたのケースでオンスのために実行されていないのは、文字列ビルダーが 'oz'を2回追加しているためです。 1. Ma で確認するとき2. La で確認するときあなたのsbには 'ozoz'が含まれているので、elseは実行されません。

ここでstringbuilderを使用する必要がある場合は、if条件ではなくscannerから文字列を読み取った後にsb.appendを使用してください。

関連する問題