如何循环直到用户在Java上输入正确的内容?

时间:2016-09-16 05:57:19

标签: java string loops for-loop

import java.util.Scanner;

public class SecretWord {
  public static void main( String[] args ) {

    Scanner input = new Scanner(System.in);
    String secret = "Please", guess;

    System.out.print( "Secret word?" );
    guess = input.next();

    for (int i = 0; guess.equals(secret); i++) {
      if( guess.equals(secret) ) {
        System.out.println("enter");
      } else {
        System.out.println( "try again" );
      }
    }
  }
}

如何使用,当用户输入“请”以外的任何内容时,会让他/她再试一次?然后用户必须输入“请”,结束循环,然后打印“回车”。

4 个答案:

答案 0 :(得分:1)

改为使用while循环

while (!guess.equals(secret)) {
    if( guess.equals(secret) ) {
        System.out.println( "enter" );
    } else {
        System.out.println( "try again" ); {
        System.out.println("Secret word")
        guess = input.next();
    }
}

除此之外,for循环具有以下语法

for (before, conditionsIsTrue, end)

这意味着对你来说循环将是这样的,

for(int i=0; if(guess.equals(secret)), i++)

由于这个条件永远不会适用于第一个循环,所以你永远不会进入for循环。

你也可以使用do-while,它使用后期测试,

do {
    System.out.println("Secret word")
    guess = input.next();
    if( guess.equals(secret) ) {
        System.out.println( "enter" );
    } else {
        System.out.println( "try again" ); {
    }
} while (!guess.equals(secret));

答案 1 :(得分:1)

您必须移动循环内的input.next(),我建议您使用while代替for循环:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String secret = "Please", guess = "";

    System.out.print("Secret word?");

    while (!guess.equals(secret)) {
        guess = input.next();

        if (guess.equals(secret)) {
            System.out.println("enter");
        } else {
            System.out.println("try again");
        }
    }
}

答案 2 :(得分:0)

试试这个:

 import java.util.Scanner;

    public class SecretWord {
      public static void main( String[] args ) {

        Scanner input = new Scanner(System.in);
        String secret = "Please", guess;

        while(true){
            System.out.print( "Secret word?" );
            guess = input.next();
            if(guess.equals(secret)){
                System.out.println("Enter");
                break;
            }else{
                System.out.println("Try Agian");
            }
        }
      }
    }

答案 3 :(得分:0)

这是使用' do-while'的典型例子之一。鼓励建构(并且很少)。但似乎您明确要使用' for',因此以下代码使用' for':

import java.util.Scanner;

public class SecretWord {
public static void main( String[] args ) {

  Scanner input = new Scanner(System.in);
  String secret = "Please", guess = null ;

  for ( ; !secret.equals(guess);  ) {
      System.out.print( "Secret word?" );
      guess = input.next();

      if( guess.equals(secret) ) {
          System.out.println( "enter" );
      }
      else {
          System.out.println( "try again" ); {
      }
  }}}}

我希望这有效。现在使用' do-while' :

import java.util.Scanner;
public class ModifiedSecretWord {
  public static void main( String[] args ) {

      Scanner input = new Scanner(System.in);
      String secret = "Please", guess;

      do{
          System.out.print( "Secret word?" );
          guess = input.next();  
      }
      while( !guess.equals(secret) ) ;

      System.out.println("Shazam!!!");
}}