2016-04-09 5 views
1

ここに私の質問です。 for文に文字列を入れるには 例:for(user.equals(X)によって入力された文字列) do this 私はこのメソッドを使用して、ユーザーが希望する特定の文字列を入力するまで繰り返します文字列に基づくJavaの "for"ステートメント反復

+1

は、あなたが '試してみました{。 ...} '? – user1314742

答えて

0

このshoudlはあなたが望むことを完全に行います。ユーザーが "Mike"と等しくない名前を入力すると、ループは続行されます。それが役に立てば幸い!

import java.util.Scanner; 
public class Program { 

public static void main(String[] args) { 

    String name = "Mike"; 
    Scanner scan = new Scanner(System.in); 

    System.out.println("Enter your name: "); 
    String user = scan.nextLine(); 

    while (!user.equals(name)) { 
     System.out.println("Enter your name: "); 
     user = scan.nextLine(); 
    } 
    System.out.println("Good bye"); 

} 
} 
0

私は

for(; !user.equals(X) ;) 
    { user = <scanner input> ; //accept a scanner input } 

はあなたを助けるものと推測します。

0

whileループの出口のためのkey [この例でQを]定義し、条件が満たされるまで、ユーザ入力を読み取ります。

例:(!user.equals(X))しながら、

public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("Welcome to ...."); 
     System.out.println("press \"q\" to exit or just type word until the end of the life"); 
     String userInput =scan.nextLine(); 
     while (!"q".equalsIgnoreCase(userInput)) { 
      System.out.println("The user input was: " + userInput); 
      System.out.println("try again please (\"q\" to exit) "); 
      userInput =scan.nextLine(); 
     } 

    } // Ends playGame 

はので、このコードは、渡された中のqまでの文字列を与えることをユーザーに求めます...

関連する問題