2012-03-08 260 views
2

我一直在试图编写这个程序几个小时,所以请裸露在我身边。我甚至分别分解了这些循环,但是一旦我把所有东西放在一起,它就无法工作,我需要它。循环语句逻辑

所以,让我打破什么,我需要每一个循环做,然后我得到什么输出:

1)第一个循环,我需要使用一个DO /时。在这个循环中,用户被提示输入一个单词。如果用户键入错误的单词,则会收到错误消息:

无效!再试一次! 2次尝试离开!

无效!再试一次! 1次尝试离开!

对不起!你没有更多的尝试了!

这个输出工作,只要错一个字每一个进入时间,但如果1后输入正确的单词失败的尝试它仍然适用错误消息

2)在我的第二个循环( for循环),用户被要求输入“3 * 8 =”如果输入了错误的数字3次,或者输入了24,那么循环的这部分工作正常。

问题出在24进入后的循环中。输出如下:

谢谢你等等等等。如果您是赢家,我们会致电5555555555。 3 * 8 =其中3 * 8不应显示。我意识到我可以休息一下;在此声明之后,但说明明确指出我不能使用break命令。

正确的输出应为:谢谢你等等等等。如果您是赢家,我们会致电5555555555。

public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 

    int attempt = 2; 
    int answer = 24; 
    long phoneNumber = 0; 
    String firstName = ""; 
    String lastName = ""; 
    String wordOfTheDay = ""; 

    System.out.printf("Enter the word of the day: "); 
    wordOfTheDay = input.nextLine(); 

    if(wordOfTheDay.equals("tired")) 
    { 
     for(attempt = 2; attempt >= 0; --attempt) 
     { 
      System.out.print(" 3 * 8 = "); 
      answer = input.nextInt(); 
      input.nextLine(); 

      if(answer == 24) 
      { 
       System.out.printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n"        +"in a drawing for an all-expenses-paid vacation in the Bahamas: "); 

       firstName = input.next(); 
       lastName = input.next(); 
       phoneNumber = input.nextLong(); 

       System.out.printf(
        "Thank you %s %s. We'll call you at %d if you're a winner.", 
        firstName, 
        lastName, 
        + phoneNumber); 
      } 

      else if(answer != 24) 
      { 
       if(attempt!=0) 
       { 
        System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt); 
        continue; 
       } 
       else 
       { 
        System.out.print("Sorry! You have no more attempts left!"); 
       } 
      } 
     } 
    } 
    else 
    { 
     do 
     { 
      System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt); 
      --attempt; 
      System.out.printf("Enter the word of the day: "); 
      wordOfTheDay = input.nextLine(); 
     } while (attempt >= 1); 

     if(attempt == 0) 
     { 
      System.out.print("Sorry! You have no more attempts left!"); 
     } 
    } 
} 

我希望我说得够清楚。

回顾一下,我需要解决我的问题/同时不让我在失败的尝试后输入正确的单词。

此外,我需要摆脱3 * 8 =显示后,用户输入正确的输入。

谢谢!

+0

唯一的检查,你正在为当天的正确词汇在两个循环之外,所以在你第一次检查它时,不管你输入什么,你都不会再检查它。 – DaveJohnston 2012-03-08 09:39:54

+0

我应该重新将它作为家庭作业吗? – Juvanis 2012-03-08 09:50:58

+0

你可以尝试改善你的格式吗?例如你的IDE会为你做这个。 – 2012-03-08 09:55:33

回答

1

通常情况下,你可以使用break声明,但不允许你设置attempt = -1将有同样的效果:

if(answer == 24) 
{ 
    ... 
    attempt = -1; // An ugly 'break' 
} 

编辑:

移动的do { } while();之前if检查:

// These two lines of code are no longer required. 
    //System.out.printf("Enter the word of the day: "); 
    //wordOfTheDay = input.nextLine(); 

    do 
    { 
     System.out.printf("Enter the word of the day: "); 
     wordOfTheDay = input.nextLine(); 

     if(!wordOfTheDay.equals("tired")) 
     { 
      System.out.printf(
       "Invalid! Try Again! %d attempt(s) left!\n ", --attempt); 
     } 
     else 
     { 
      attempt = 0; // Another ugly 'break' 
     } 
    } while (attempt >= 1); 


    if(wordOfTheDay.equals("tired")) 
    { 
    } 
    // Remove else branch as not required. 
+0

这固定了我的第二个输出错误。我不知道她是否会接受这种不间断的方式,但我会找到这样或那样的方式。感谢您的帮助,现在找出第一个输出错误。 – Abweichung 2012-03-08 10:11:04

+0

移动'do {} while();''获得'wordOfTheDay'到'if(wordOfTheDay.equals(“tired”))''之前。 – hmjd 2012-03-08 10:20:46

+0

我这样做了,但是错误信息提示并不是每次都显示出来,在最后一次失败的尝试中需要 说声对不起!你没有更多的尝试了!“例如,我会输入”tir“,它会提示输入一天中的字词而不是无效!然后再次提示。 – Abweichung 2012-03-08 10:31:22

0

当您得到正确的结果时,您需要退出循环。这是使用break声明完成的:

if(answer == 24) 
{ 
    System.out.printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n" +"in a drawing for an all-expenses-paid vacation in the Bahamas: "); 
    firstName = input.next(); 
    lastName = input.next(); 
    phoneNumber = input.nextLong(); 
    System.out.printf("Thank you %s %s. We'll call you at %d if you're a winner.", firstName, lastName,        + phoneNumber); 
    break; 
    // that's the break statement 
} 
+0

根据问题,他不允许使用'break'。 – hmjd 2012-03-08 09:42:33

+0

不幸的是我不能使用break语句。这些指令说: 代码System.exit(0)只在main()的末尾有一次,这意味着你将不得不将循环控制变量设置为一个强制终止do/while和for循环的值用户输入的提示是正确的。你不能使用break语句。 – Abweichung 2012-03-08 09:43:34

0

好吧,显然你必须打破外层循环。

如果在if区块中有正确答案(即'answer == 24'),则将某个布尔变量'haveAnswer'设置为'true'(初始化为'false'并在' System.out中。打印(“3 * 8 =”);'对于“如果(haveAnswer){打破;}”

enter code here`public static void main(String[] args) 
{ 

Scanner input = new Scanner(System.in); 

int attempt = 2; 
int answer = 24; 
long phoneNumber = 0; 
String firstName = ""; 
String lastName = ""; 
String wordOfTheDay = ""; 


System.out.printf("Enter the word of the day: "); 
wordOfTheDay = input.nextLine(); 

if(wordOfTheDay.equals("tired")) 
{ 
    boolean haveAnswer = false; 
    for(attempt = 2; attempt >= 0; --attempt) 
     { 
     if (haveAnswer) 
      break; 
     System.out.print(" 3 * 8 = "); 
     answer = input.nextInt(); 
     input.nextLine(); 

     if(answer == 24) 
     { 
      System.out.printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n"        +"in a drawing for an all-expenses-paid vacation in the Bahamas: "); 

     firstName = input.next(); 
     lastName = input.next(); 
     phoneNumber = input.nextLong(); 

     System.out.printf("Thank you %s %s. We'll call you at %d if you're a winner.", firstName, lastName,        + phoneNumber); 

      haveAnswer = true; 
      break; 
     } 

     else if(answer != 24) 
     { 

      if(attempt!=0) 
      { 

       System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt); 
        continue; 
      } 
      else 
      { 

       System.out.print("Sorry! You have no more attempts left!"); 
      } 
     } 

    } 

} 
else 
{ 
do 
{ 

    System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt); 
    --attempt; 
    System.out.printf("Enter the word of the day: "); 
    wordOfTheDay = input.nextLine(); 
} while (attempt >= 1); 


    if(attempt == 0) 
    { 

    System.out.print("Sorry! You have no more attempts left!"); 
    } 
} 

System.exit(0); 

} 
} 
1

除非我失去了一些东西,你都在等待第一个输入,如果它是错的,你是在一个do-while循环去然后永不检查再次如果输入是正确的。

你必须移动,如果do-while循环中:

public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    int attempt = 2; 
    int answer = 24; 
    long phoneNumber = 0; 
    String firstName = ""; 
    String lastName = ""; 
    String wordOfTheDay = ""; 

    System.out.printf("Enter the word of the day: "); 
    wordOfTheDay = input.nextLine(); 
    do { 
     if (wordOfTheDay.equals("tired")) { 

      for (attempt = 2; attempt >= 0; --attempt) { 
       System.out.print(" 3 * 8 = "); 
       answer = input.nextInt(); 
       input.nextLine(); 

       if (answer == 24) { 
        System.out 
          .printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n" 
            + "in a drawing for an all-expenses-paid vacation in the Bahamas: "); 

        firstName = input.next(); 
        lastName = input.next(); 
        phoneNumber = input.nextLong(); 

        System.out 
          .printf("Thank you %s %s. We'll call you at %d if you're a winner.", 
            firstName, lastName, +phoneNumber); 
       } 

       else if (answer != 24) { 

        if (attempt != 0) { 

         System.out 
           .printf("Invalid! Try Again! %d attempt(s) left!\n ", 
             attempt); 
         continue; 
        } else { 

         System.out 
           .print("Sorry! You have no more attempts left!"); 
        } 
       } 

      } 

     } else { 

      System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", 
        attempt); 
      System.out.printf("Enter the word of the day: "); 
      wordOfTheDay = input.nextLine(); 
      if (!wordOfTheDay.equals("tired")) --attempt; 

     } 

    } while (attempt >= 1); 

    if (attempt == 0) { 

     System.out.print("Sorry! You have no more attempts left!"); 
    } 

    System.exit(0); 

} 
+0

我刚把它复制到JCreator的测试类中。这只有在第一次尝试错误时才有效。我可以输入一次错误地累进这个词,它会提示3 * 8 =,但如果我错误地输入了两次,然后在最后一次尝试中输入正确,它仍然会给我“”对不起! “ – Abweichung 2012-03-08 10:05:20

+0

是的,因为当你到达最后一次尝试时,'attempt'为0,所以它检查while状态并退出。 – Tudor 2012-03-08 10:39:34

+0

我已经编辑了else分支,其中尝试'递减,现在应该是正确的 – Tudor 2012-03-08 10:48:22

0

此代码不完整,但它具有您需要的ide。试试,。

布尔firstTry = true; 布尔keepLo​​oping = true;

do 
    { 
    if(!firstTry){ 
      System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt); 
    } 

     System.out.printf("Enter the word of the day: "); 
     wordOfTheDay = input.nextLine(); 
    if(wordOfTheDay.equals("hey!")){ 
     keepLooping = false; 
    } 
    --attempt; 
    } while (attempt >= 1 && keepLooping); 
0

你最好用户休息一下跳出循环。否则循环会继续,打印“3 * 8 =”并等待你的输入。 如果你不想使用休息时间,那么使用attemptp = -1也是有意义的。

if(answer == 24) 
     { 
      System.out.printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n"        +"in a drawing for an all-expenses-paid vacation in the Bahamas: "); 

     firstName = input.next(); 
     lastName = input.next(); 
     phoneNumber = input.nextLong(); 
     attempt = -1; 
     System.out.printf("Thank you %s %s. We'll call you at %d if you're a winner.", firstName, lastName,        + phoneNumber); 

     } 
2

我想你正在尝试这样做。

import java.util.Scanner; 

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

     Scanner input = new Scanner(System.in); 

     int answer = 0; 
     long phoneNumber = 0; 
     String firstName = ""; 
     String lastName = ""; 
     String wordOfTheDay = ""; 

     int mainMenuAttempt = 3; 
     do { 
      System.out.printf("Enter the word of the day: "); 
      wordOfTheDay = input.nextLine(); 

      if (wordOfTheDay.equals("tired")) { 

       int answerAttempt = 3; 
       do { 
        System.out.print(" 3 * 8 = "); 
        answer = input.nextInt(); 
        input.nextLine(); 
        answerAttempt--; 

        if (answer != 24 && answerAttempt >0) 
         System.out.printf(
           "Invalid! Try Again! %d attempt(s) left!\n ", 
           answerAttempt); 

       } while (answerAttempt >0 && answerAttempt < 3 && answer != 24); 

       if (answer == 24) { 
        System.out 
          .printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n" 
            + "in a drawing for an all-expenses-paid vacation in the Bahamas: "); 

        firstName = input.next(); 
        lastName = input.next(); 
        phoneNumber = input.nextLong(); 

        System.out 
          .printf("Thank you %s %s. We'll call you at %d if you're a winner.", 
            firstName, lastName, +phoneNumber); 
       } 

      } 

      mainMenuAttempt--; 

     } while (mainMenuAttempt >0 && mainMenuAttempt < 3 && !wordOfTheDay.equals("tired") && answer!=24); 
     System.exit(0); 
    } 
} 
2

试着把整个事情分解成非常小的函数,分别做一件特定的事情。例如,您可能有一个函数,用于检查单词是否正确,如果是则返回0,否则返回0。

private int checkWord(String correctWord, String wordToCheck, int attempts) 
{ 
    if(wordToCheck.equals(correctWord)) 
    { 
     return 0; 
    } 
    return attempts; 
} 

然后,您可以创建一个函数,它接受用户输入,并调用该函数来检查所述输入。然后它可以输出一个依赖于前一个函数返回码的错误信息。该函数然后会返回一个代码来告诉上面的函数情况。

public int checkWordVerbose(String question, String correctWord, int attempts) 
{ 
    if(attempts <= 3) 
    { 
     Scanner scan = new Scanner(System.in); 
     System.out.print(question); 
     String input = scan.nextLine(); 
     int failureCode = checkWord(correctWord, input, attempts); 

     if(failureCode == 0) 
     { 
      return 0; 
     } 
     else 
     { 
      System.out.println("Invalid! Attempts left: " + (3 - failureCode)); 
      return 1; 
     } 
    } 
    else 
    { 
     System.out.println("Sorry! You have no more attempts left!\n"); 
     return 2; 
    } 
} 

最后,你可以把它简单地包含了循环的功能,并保持逻辑保持这个循环:

public int checkWord(String correctWord) 
{ 
    int failureCode = 1; 
    int attempts = 1; 
    do 
    { 
     failureCode = checkWordVerbose("Enter the word of the day: ", correctWord, attempts); 
     attempts++; 
    } while(failureCode == 1); 
    return failureCode; 
} 

然后在主,所有你需要做的是检查是否返回checkWord(String correctWord) 0.重复同样的事情为您的其他检查(或者你甚至可能能够使用一些相同的功能),执行另一个,如果在第一个,如:

if(checkWord("tired") == 0) 
{ 
    if(checkMath("24") == 0) 
    { 
     // Success! 
    } 
}