如何在循环中重复处理

时间:2017-11-25 09:22:52

标签: java

我正在编写代码让用户猜出这个数字。 Thay只有两次机会获得全部 如果用户输错了(超过1-4), 他们可以再做一次。 在这种情况下,用户必须回答2和4才能获得所有内容。

System.out.println("you have only two chance to get all");
    int guessnum[] = new int[2];;        
    for (int i = 0; i < guessnum.length; i++) {
        System.out.print((i+1)+" Enter number 1-4 : ");
        int num = sc.nextInt();
        if (num == 1) {            
           System.out.println("not here");
        }
        else if (num == 2) {            
           System.out.println("wow!! you got it");
        }
        else if (num == 3) {            
           System.out.println("not here");

        }
        else if (num == 4) {            
           System.out.println("wow!! you got it");
        }   
        else {            
           System.out.println("number must be 1-4 only, try again");
          //how to repeat in same loop
        }
    }

5 个答案:

答案 0 :(得分:0)

使用break声明

else if (num == 2) {            
   System.out.println("wow!! you got it");
   break;
}

答案 1 :(得分:0)

for (int i = 0; i < guessnum.length; i++) {
            int count = 1;

            System.out.print((i+1)+" Enter number 1-4 : ");
            int num = sc.nextInt();
            if (num == 1) {
                System.out.println("not here");
            }
            else if (num == 2) {
                System.out.println("wow!! you got it");
            }
            else if (num == 3) {
                System.out.println("not here");

            }
            else if (num == 4) {
                System.out.println("wow!! you got it");
            } else {
                if (i != 2) {
                    System.out.println("number must be 1-4 only, try again,and change another number");
                } else {
                    break;
                }
                //do again in same loop
            }

           i++;
    }

你可以试试这个

答案 2 :(得分:0)

这是我的所有代码,用户需要两次机会才能赢得胜利。

import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("you have only two chance to get all");
        int guessnum[] = new int[2];
        int x=0;        
        for (int i = 0; i < guessnum.length; i++) {
            System.out.print((i+1)+" Enter number 1-4 : ");
            int num = sc.nextInt();
            if (num == 1) {            
               System.out.println("not here");
            }
            else if (num == 2) {            
               System.out.println("wow!! you got it");
               x++;
            }
            else if (num == 3) {            
               System.out.println("not here");

            }
            else if (num == 4) {            
               System.out.println("wow!! you got it");
               x++;
            }   
            else {            
               System.out.println("number must be 1-4 only, try again");
                --i;
            }
        }
        if (x == 0)             
               System.out.println("\nyou don't get anything");          
        if (x == 1)             
               System.out.println("\nyou got 1 only");
        if (x == 2)           
               System.out.println("\ncongrat!!!! you got all");
    }
}

答案 3 :(得分:-1)

如果你想使用var thing=stuff;循环,你可以在for

之后的最后一个--i;块中写下else这样的内容

因此,此代码将解决您的问题:

System.out.println("number must be 1-3 only, try again");

<强>更新

在我上面的回答中,我刚才说过,如何重复System.out.println("you have only two chance to get all"); int guessnum[] = new int[2];; for (int i = 0; i < guessnum.length; i++) { System.out.print((i+1)+" Enter number 1-4 : "); int num = sc.nextInt(); if (num == 1) { System.out.println("not here"); } else if (num == 2) { System.out.println("wow!! you got it"); } else if (num == 3) { System.out.println("not here"); } else if (num == 4) { System.out.println("wow!! you got it"); } else { System.out.println("number must be 1-4 only, try again"); --i; } } 循环,只需对原始代码进行最少的更改。正如标题中所说的那样。但@JayPrakash表示,这并不是完美的答案并将其投票。好吧,让我们试着找到一个完美的:

for

答案 4 :(得分:-1)

        int i = 1;
        Scanner sc = new Scanner(System.in);
        do {
                System.out.println("you have only two chance to get all");
                System.out.print((i) + " Enter number 1-4 : ");
                int num = sc.nextInt();
                switch (num) {
                    case 1:
                        System.out.println("not here");
                        break;
                    case 2:
                        System.out.println("wow!! you got it");
                        goItOrNot = false;
                        break;
                    case 3:
                        System.out.println("not here");
                        break;
                    case 4:
                        System.out.println("wow!! you got it");
                        break;
                    default:
                        System.out.println("number must be 1-4 only, try again");
                        break;
                }
                i++;
            }
        } while (i < 3);