2017-02-07 8 views
1

私は、文字列中の単語の1つをユーザが入力するためにスキャナ方法を使用していますが、ユーザが文字列の1つを入力してもまだelseが実行されています。それをどうやって防ぐのですか?if/elseの実行を中止するにはどうすればよいですか?

public static void main(String[] args) {   
    while(true) { 
     StringBuffer StringBuffer = new StringBuffer(); 
     Scanner input = new Scanner(System.in); 
     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 (String.valueOf(convert).contains("mass, volume, sphere, temp, density, pound, ounce, ton, gram,")) { 
      StringBuffer.append(String.valueOf(convert)); 
     } else { 
      System.out.println("Wrong input. Try again."); 
     } 
    } 
} 
+1

String.valueOf(変換)は必要ありません。変換するだけです。 – Locke

答えて

1

他の方法で回避、変種のあなたの文字列にcontainsを呼び出します。 Clone Talkconvertが既にStringあるので、あなたは、String.valueOfを必要としないに述べたようにと(ただし、それは同様にそれに対応しています。):

なぜif(convert.contains("....")) doesnの」:

public static void main(String[] args) { 
    while (true) { 
     StringBuffer StringBuffer = new StringBuffer(); 
     Scanner input = new Scanner(System.in); 
     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 ("mass, volume, sphere, temp, density, pound, ounce, ton, gram,".contains(convert)) { 
      StringBuffer.append(convert); 
     } else { 
      System.out.println("Wrong input. Try again."); 
     } 
    } 
} 

コメントをアドレッシングtは動作しますか?

最も簡単な方法は、the documentation of String.containsを見てすることです:trueを返します。この文字列の場合にのみ場合はchar値の指定されたシーケンスが含まれています。

質問から元の例では、この文字列ない私の答えに)のいずれか"mass"または"volume"かなどをすることができconvert、あります;

char値の指定されたシーケンスは、長い文字列"mass, volume, ..."です。

"mass""mass, volume, etc."を含むことができますか?この例では、性能向上を感じるのに十分な大きさになります"mass, volume, etc.".contains("mass") == true

HashSet.containsは、それは文字列のようには見えません

よりパフォーマンスだろう、しかし:それは周りに確かに、他の方法です。一般的に言えば、可能な入力の数が少なくなく、可読性と保守容易性の点で良い点です。

// This part may vary: 
private static String [] variants = {"mass", "volume", "sphere", "temp", "density", "pound", "ounce", "ton", "gram"}; 
private static Set<String> inputs = new HashSet<>(Arrays.asList(variants)); 

public static void main(String[] args) { 
    while (true) { 
     StringBuffer StringBuffer = new StringBuffer(); 
     Scanner input = new Scanner(System.in); 
     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 (inputs.contains(convert)) { 
      StringBuffer.append(convert); 
     } else { 
      System.out.println("Wrong input. Try again."); 
     } 
    } 
} 
+0

なぜ 'if(convert.contains(" .... "))'が機能しないのか説明していますか? – Yousaf

+0

@Yousaf私の更新を参照してください。 – zhelezoglo

+1

'HashSet.contains'はもっとパフォーマンスが良いでしょう:) –

関連する問題