2017-02-04 5 views
0

文字列が配列に実際にあるとブール値がtrueを返した場合は、アレイ内のユーザの入力文字列が、私はそれはいくつかのカスタムテキストを書きたいのであれば、これは単なるチェック以上のものです。チェック

{ 
    static void Main(string[] args) 
    { 
     string[] name= {"Bob","Bob2","Bob3","Bob4"}; 

     bool exists = name.Contains(string userName= Console.ReadLine()); 

     if (exists == true) 
      Console.WriteLine("Hi " + userName); 

     Console.ReadLine(); 
    } 
} 

私はこのようなコードを実行することはできません知っているが、私はそれを実行するために、同様の方法を探していますが、私は、ユーザー入力を保存するかどうかはわかりませんし、まだユーザーならばブールチェックを持っていますinupされた文字列は文字列配列にあります。

答えて

3

あなたはほとんどそれを持っていた、ただそれ自身の行までuserName割り当てを移動:ここ

static void Main(string[] args) 
{ 
    string[] name = { "Bob", "Bob2", "Bob3", "Bob4" }; 

    string userName = Console.ReadLine(); 
    bool exists = name.Contains(userName); 

    if (exists == true) 
     Console.WriteLine("Hi " + userName); 

     Console.ReadLine(); 

} 

が出力されます。

enter image description here

+0

ああ男。私は馬鹿のように感じる。ありがとう! –

0

ただ、TE Containsチェックの外に、あなたの入力変数を宣言します。

static void Main(string[] args) 
{ 
    string[] name= {"Bob","Bob2","Bob3","Bob4"}; 

    string userName = Console.ReadLine(); 
    bool exists = name.Contains(userName); 

    if (exists == true) 
     Console.WriteLine("Hi " + userName); 

    Console.ReadLine(); 
} 
0

私は

import java.util.Scanner; 


public class inp { 
public static void main (String args[]) { 
    String[] name = new String[] { 
    "Ben", 
    "Sam" 
    }; 
    int max = name.length; 
    int current = 0; 
    boolean is = false; 

    System.out.println("What name tho?"); 
    Scanner inp = new Scanner(System.in); 
    String nameInq = inp.next(); 

    while(is == false && current<max) { 
     if(name[current].equals(nameInq)) { 
      is = true; 
     }else { 
      current++; 

     } 
    } 
if(is) { 
    System.out.println("They are in the list"); 
}else { 
    System.out.println("nah"); 
} 

} 
} 

ないかなりefficentとして...これを思い付いたが、仕事を取得します。