仅在“其他”中重定向循环内的循环

时间:2018-02-05 11:14:31

标签: java

我是初学者,正在从事练习作业。如果用户回答错误,我需要能够重新打印相同的添加问题,但我不知道该怎么做。我的所有尝试都会导致出现新的随机添加问题或在原始文件中添加另一个新的随机问题,这也是不可取的。我确信这很简单,但我迷路了。提前感谢任何提示!

package ov3uppgift8;
import java.util.Scanner;
import java.util.Random;

public class Ov3uppgift8 {

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

        System.out.println("*****************");
        System.out.println("* MATH TRAINING *");
        System.out.println("*****************");
        System.out.println("Enter 0 to close program.");
        int ans;
        do {    
            int a = rand.nextInt(10) + 1;
            int b = rand.nextInt(10) + 1;
            System.out.print(a + " + " + b +" = ");
            ans = input.nextInt();    

            if (ans==(a+b)) {
                System.out.println("CORRECT!");
            } else if (ans==0) {
                System.out.println("Good bye!");
            } else if (ans!=(a+b)) {
                System.out.println("Incorrect, try again.");
            }
      } while (ans!=0);     
    }

}

5 个答案:

答案 0 :(得分:1)

稍微简化你的代码,不要过度思考。你每次进入循环时都会产生答案,在外面生成你的数字,这样他们就能保持一致,同时,没有必要,如果答案是正确的,那么就打破你的循环,或者你把它放在0。另外,你需要为了确保用户输入了一个数字,所以在获取输入时应该放置一个try-catch。

Scanner input = new Scanner(System.in);
Random rand = new Random ();

System.out.println("*****************");
System.out.println("* MATH TRAINING *");
System.out.println("*****************");
System.out.println("Enter 0 to close program.");

int a = rand.nextInt(10) + 1;
int b = rand.nextInt(10) + 1;

int ans;

while (true) {
    System.out.printf("%d + %d = ?%n", a, b);

    try {
        ans = Integer.parseInt(input.next());
    } catch (NumberFormatException e) {
        System.out.println("Please enter a number!");
        continue;
    }

    if (ans == 0) {
        System.out.println("Goodbye!");
        break;
    } else if (ans == a + b) {
        System.out.println("Correct!");
        break;
    } else {
        System.out.println("Incorrect!");
    }
}

答案 1 :(得分:0)

使用这个简单的源代码,类似于你(小改动):

try (Scanner input = new Scanner(System.in)) {
    Random rand = new Random();

    System.out.println("*****************");
    System.out.println("* MATH TRAINING *");
    System.out.println("*****************");
    System.out.println("Enter 0 to close program.");

    int ans, a, b;
    ans = a = b = 0;

    do {    
        a = rand.nextInt(10) + 1;
        b = rand.nextInt(10) + 1;
        System.out.print(a + " + " + b + " = ");

        try{
            //Getting input as String and casting to Integer
            ans = Integer.parseInt(input.nextLine());

            if (ans == 0) {
                System.out.println("Good bye!");
            } else if (ans == (a + b)) {
                System.out.println("CORRECT!");
            } else if (ans != (a + b)) {
                System.out.println("Incorrect, try again.");
            }

        }catch (NumberFormatException nfe){
            System.out.println(nfe);
        }

    } while (ans != 0);

}

答案 2 :(得分:0)

class generateNum{
    Random rand = new Random ();
    int a=rand.nextInt(10)+1;
    int b=rand.nextInt(10)+1;
    public void generate(){

         System.out.print(a + " + " + b +" = ");
     }
    public int getsum(){
        return a+b;
    }
}
public class Test02 {

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

            System.out.println("*****************");
            System.out.println("* MATH TRAINING *");
            System.out.println("*****************");
            System.out.println("Enter 0 to close program.");

                int ans;      

                generateNum gnum = new generateNum();
                gnum.generate();
                ans = input.nextInt();
                int com=gnum.getsum();

                do
                {    
                if (ans==com)

                {
                    System.out.println("CORRECT!");
                    gnum = new generateNum();
                    gnum.generate();
                    ans = input.nextInt();
                    com=gnum.getsum();
                }
                 else if (ans==0) 

                {
                    System.out.println("Good bye!");
                }

                else if (ans!=com)

                {
                    System.out.println("Incorrect, try again.");
                    gnum.generate();
                    ans = input.nextInt();
                }
                }
          while (ans!=0);     
        }
}

答案 3 :(得分:0)

您的情况几乎没有变化。更新你的else-if(ans!=(a + b))块。

import java.util.Random;
import java.util.Scanner;

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

        System.out.println("*****************");
        System.out.println("* MATH TRAINING *");
        System.out.println("*****************");
        System.out.println("Enter 0 to close program.");
            int ans;
            do
            {    
            int a = rand.nextInt(10) + 1;
            int b = rand.nextInt(10) + 1;
                System.out.print(a + " + " + b +" = ");
            ans = input.nextInt();    

            if (ans==(a+b))

            {
                System.out.println("CORRECT!");
            }
             else if (ans==0) 

            {
                System.out.println("Good bye!");

            }

              else if (ans!=(a+b))

                {

                 boolean flag = false;
                 do{
                    System.out.println("Incorrect, try again.(0 for skip)");
                        int againAns = input.nextInt();
                        if(againAns == (a+b)){
                            System.out.println("CORRECT!");
                        flag = true;
                        }else if(againAns == 0){
                        System.out.println("You skip the answer..");
                        flag = true;
                    }
                    }while(flag != true);
                }
                }while (ans!=0);  
    }
}

答案 4 :(得分:0)

请参阅内联修改:

    package ov3uppgift8;
    import java.util.Scanner;
    import java.util.Random;

    public class Ov3uppgift8 {

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

            System.out.println("*****************");
            System.out.println("* MATH TRAINING *");
            System.out.println("*****************");
            System.out.println("Enter 0 to close program.");
                int ans;
                do
                {    
                int a = rand.nextInt(10) + 1;
                int b = rand.nextInt(10) + 1;
                    System.out.print(a + " + " + b +" = ");
                ans = input.nextInt();    

                if (ans==(a+b))

                {
                    System.out.println("CORRECT!");
                }
                 else if (ans==0) 

                {
                    System.out.println("Good bye!");

                }

                else if (ans!=(a+b))

                {
                    // here you keep asking the user over and over 
                    //until they give the right result
                    do{
                        System.out.println("Incorrect, try again.");
                        //show again the equation to the user
                        System.out.print(a + " + " + b +" = ");
                        ans = input.nextInt();
                    }while(ans!=(a+b));
                }
                }
          while (ans!=0);     
        }

    }