2014-02-19 2 views
1

После «Пожалуйста, перезагрузитесь и повторите попытку» Я пытаюсь запустить программу, чтобы начать «Привет, как вас зовут?».Как сделать мой скрипт либо замкнутым?

import java.util.Scanner; 


public class Learning { 

public static void main(String[] args) 

{ 
    System.out.println("Hello, What is your name?"); 
    Scanner scanner = new Scanner(System.in); 
    String yourName = scanner.nextLine(); 
    System.out.println("Is: " +yourName + " your name?"); 

    Scanner scanner1 = new Scanner(System.in); 
    String isCorrect = scanner1.nextLine(); 

    if (isCorrect.equals("Yes")) 
    { 
     System.out.println("Thank you, Please proceed with the quiz!"); 
    } 
    else 
    { 
     System.out.println("Please restart and try again."); 
     System.exit(0); 
    } 
+0

'Я пытаюсь ...' Давайте посмотрим. Я не вижу твоей попытки. –

+1

Вы уже читали о [петлях] (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)? – PakkuDon

+0

Я не вижу петли. Почему вы не создаете цикл? Как цикл не работает, если вы не создаете цикл? –

ответ

1

Я добавил некоторое время заявление:

while(isCorrect.equals("noValue")&&(!isCorrect.equals("yes")||!isCorrect.equals("yes"))) { 

Так продолжается до тех пор, isCorrect dosn't равно «noValue» и он приравнивает «да» или «Да» Вот весь код:

import java.util.Scanner; 

public class Learning { 

public static void main(String[] args) { 

String isCorrect = "noValue"; 
//If isCorrect equals noValue, keep going until it equals yes or Yes 
while(isCorrect.equals("noValue")&&(!isCorrect.equals("yes")||!isCorrect.equals("Yes"))) { 
    System.out.println("Hello, What is your name?"); 
    //You don't really need two scanners if they are both for the same thing 
    Scanner scanner = new Scanner(System.in); 
    String yourName = scanner.nextLine(); 
    System.out.println("Is: " +yourName + " your name?"); 

    isCorrect = scanner.nextLine(); 
    //include the if and else statements so the program knows what to say 
    //and if it should repeat or not 
    if(isCorrect.equals("yes")||isCorrect.equals("Yes")) { 
     System.out.println("Thank you, please proceed with the quiz!"); 
    } else { 
     System.out.println("Please try again"); 
     isCorrect = "noValue"; 
    } 
} 
//Continue with the quiz here! 
} 
} 

Надеюсь, я помог!

1

просто используйте цикл do-while и удалите System.exit (0);

do { 

    System.out.println("Hello, What is your name?"); 
    Scanner scanner = new Scanner(System.in); 
    String yourName = scanner.nextLine(); 
    System.out.println("Is: " +yourName + " your name?"); 

    Scanner scanner1 = new Scanner(System.in); 
    String isCorrect = scanner1.nextLine(); 

    if (isCorrect.equals("Yes")) 
    { 
     System.out.println("Thank you, Please proceed with the quiz!"); 
    } 
    else 
    { 
     System.out.println("Please restart and try again."); 
    } 
} while (!isCorrect.equals("Yes")); 
Смежные вопросы