循环if语句

时间:2014-10-18 03:40:59

标签: java loops while-loop

我想在这种情况下问这个循环,在这种情况下问他们一个问题""什么是2 + 2?"然后接受输入,然后程序应该询问"你确定Y / N"如果他们没有我想要它回到这个循环的开始并允许他们重做问题。因为它是我无法循环,在这部分工作后,我将需要以相同的格式再做9个问题


  import java.util.Scanner;
class Quiz
{
  public static void main (String[] args) 
  {
  int q1=0 , q2=0;
  boolean correct = false;
  char yn1='y';
  String q3 , q4;
        Scanner input1 = new Scanner( System.in );

        int count = 0 ;
        //Question 1 start
        while (correct == false) 
                { 
                    System.out.println("What is 2+2? ");
                    System.out.println("Choices:  0,2,4,8");
                    q1 = input1.nextInt(); //used after loop


                    System.out.println("Are You Sure? (y/n)");
                    char c = input1.next().charAt(0);              // Changed LINE
                    if (c=='y')                                    // Changed LINE
                    {
                        if ( q1 == 4)  //q1 was stated during loop
                            System.out.println("You were correct 2+2 = 4");

                        else
                            System.out.println("You were wrong");

                        break;                        
                    } 
                }
        //Question 2 start
                while (correct == false) 
                { 
                    System.out.println("how many legs does a legless cow have?");
                    System.out.println("Choices:  0,25,4,31");
                    q2 = input1.nextInt();


                    System.out.println("Are You Sure? (y/n)");
                    char c = input1.next().charAt(0);              
                    if (c=='y')                                    
                    {
                        if ( q2 == 0)  
                            System.out.println("You were correct, the cow has 0 legs");

                        else
                            System.out.println("You were wrong");

                    break;                        
                    } 
                }

        //Question 3 start
                while (correct == false) 
                { 
                    System.out.println("What is the capital city of Canada?");
                    System.out.println("Choices:  Toronto, Montreal, Vancouver, Ottawa (capitals count)");
                     q3 = input1.nextLine();


                    System.out.println("Are You Sure? (y/n)");
                    char c = input1.next().charAt(0);              
                    if (c=='y')                                    
                    {
                        if ( "Ottawa".equals(q3))  
                            System.out.println("You were correct, The capital is Ottawa");

                        else
                            System.out.println("You were wrong");

                    break;                        
                    } 
                }



    }
  }

出现了一个新问题我已经使用了一个有用的示例,并且可能会尝试将其更改为数组,但之后不会将其更改为基础工作。到目前为止所有使用int工作的问题即使用。

          //Question 1 start
    while (correct == false) 
        { 
            System.out.println("What is 2+2? ");
            System.out.println("Choices:  0,2,4,8");
            q1 = input1.nextInt(); //used after loop


            System.out.println("Are You Sure? (y/n)");
            char c = input1.next().charAt(0);              // Changed LINE
            if (c=='y')                                    // Changed LINE
            {
                if ( q1 == 4)  //q1 was stated during loop
                    System.out.println("You were correct 2+2 = 4");

                else
                    System.out.println("You were wrong");

                break;                        
            } 
        }

但是现在我想使用一个单词awnser所以我创建了一个字符串并将其放入而不是int但是现在不是允许输入q3而是跳过输入y / ni不要理解为什么所有的突然它会这样做,但其他问题可以正确使用int。

while (correct == false) 
                { 
                    System.out.println("What is the capital city of Canada?");
                    System.out.println("Choices:  Toronto, Montreal, Vancouver, Ottawa (capitals count)");
                    String q3 = input1.nextLine();


                    System.out.println("Are You Sure? (y/n)");
                    char c = input1.next().charAt(0);              
                    if (c=='y')                                    
                    {
                        if ( "Ottawa".equals(q3))  
                            System.out.println("You were correct, The capital is Ottawa");

                        else
                            System.out.println("You were wrong");

                    break;                        
                    } 
                }

对不起,如果这还没有足够的详细信息或格式错误的话,请务必修复它。

3 个答案:

答案 0 :(得分:1)

阅读输入时,请始终使用q1 = Integer.parseInt(input1.nextLine());(更好的是,使用BufferedReader类)。这样,您的阅读将顺利进行。

其次,您可以将if (q1 == 4)放在if (yn1.equals("Y"))语句中。如果用户键入了“Y”,则设置correct = true;以继续下一个问题。此外,如果用户的答案是正确的,那么增加正确答案的计数器,否则打印错误。所以循环看起来像这样:

    while (correct == false) {
        System.out.println("What is 2+2? ");
        System.out.println("Choices:  0,2,4,8");
        q1 = Integer.parseInt(input1.nextLine());
        System.out.println("Are You Sure? (Y/N)");
        yn1 = input1.nextLine();
        if (yn1.equals("Y")) {
            correct = true;
            if (q1 == 4) {
                System.out.println("You were correct 2+2 = 4");
                count++;
            } else
                System.out.println("You were wrong");
        }
    }

要考虑的一些事项:

  • 您打印"Choices: 0,2,4,8",但没有任何内容阻止用户输入33000。选择陈述是否必要?然后,您还需要检查用户是否已进入该范围内。
  • 而不是复制&将这个相同的循环粘贴n次,使用一个数组。有所有问题&答案现在存储在一些String数组中。像这样:

    for (int i = 0; i < questions.length; i++) {
        boolean nextQ = false;
        while (nextQ == false) {
            System.out.println(questions[i]);
            String ans = input1.nextLine();
            System.out.println("Are You Sure? (Y/N)");
            yn = input1.nextLine();
            if (yn.equalsIgnoreCase("Y")) {
                nextQ = true;
                if (ans.equals(answers[i])) {
                    System.out.println("You were correct " + questions[i]
                            + " = " + answers[i]);
                    count++;
                } else
                    System.out.println("You were wrong");
            }
        }
    }
    

我将布尔变量correct更改为nextQ以避免混淆。希望能让你朝着正确的方向前进。

答案 1 :(得分:0)

我试图编译你的程序。但是我遇到了一些错误: -

error: variable q1 might not have been initialized

根据我的理解你的问题。这是你想要的程序: -

import java.util.Scanner;
class stack1
{
  public static void main (String[] args) 
  {
  int q1=0 , q2 , q3, q4, q5, q6 , q7 ,q8 ,q9 ,q10 , a ;
  boolean correct = false;
  char yn1='Y';

        Scanner input1 = new Scanner( System.in );

        int count = 0 ;
       while (correct == false) 
                { 
        System.out.println("What is 2+2? ");
        System.out.println("Choices:  0,2,4,8");
        q1 = input1.nextInt(); //used after loop


                System.out.println("Are You Sure? (Y/N)");
                char c = input1.next().charAt(0);              // Changed LINE
                if (c=='y')                                    // Changed LINE
                {
        System.out.println("Exiting the program");
               correct = true; //It should now stop the loop and carry on 
                     break;                        
                } 


                }


                if ( q1 == 4)  //q1 was stated during loop
                    System.out.println("You were correct 2+2 = 4");

                else

                    System.out.println("You were wrong");

              //Soon will add int score and add 1 each time its correct
              // will be using 9 more loops the exact same way



    }
  }

我已提及所有已更改的行,并附有评论&#39; //已更改行&#39;

答案 2 :(得分:0)

这是编辑过的问题的答案: -

import java.util.Scanner;
class stack2
{
  public static void main (String[] args) 
  {
  int q1=0 , q2 , q3, q4, q5, q6 , q7 ,q8 ,q9 ,q10 , a ;
  boolean correct = false;
  char yn1='Y';
  String s="";
System.out.println(s);
        Scanner input1 = new Scanner( System.in );

        int count = 0 ;
       while (correct == false) 
                { 
        System.out.println("What is the capital city of Canada?");
        System.out.println("Choices:  Toronto, Montreal, Vancouver, Ottawa (capitals count)");
        s  = input1.nextLine();
        System.out.println(s);  
                System.out.println("Are You Sure? (Y/N)");
                char c = input1.next().charAt(0);              // Changed LINE

        if (c=='y')                                    // Changed LINE
                {
        System.out.println("Exiting the program");
               correct = true; //It should now stop the loop and carry on 
                     break;                        
                } 
          if (s.equals("Ottawa"))  //q1 was stated during loop
                    System.out.println("You were correct, The capital is Ottawa");

                else

                    System.out.println("You were wrong");

                }




              //Soon will add int score and add 1 each time its correct
              // will be using 9 more loops the exact same way



    }
 }

进入输入时要小心。应该是“渥太华”。祝你好运