有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在循环中重复进程

我正在编写代码,让用户猜测数字。他们只有两次机会获得全部 如果用户输入错误(超过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) 个答案

  1. # 1 楼答案

    使用break语句

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

    这是我所有的代码,在两个机会,用户需要把2和4赢

    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. # 3 楼答案

    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++;
        }
    

    你可以试试这个

  4. # 4 楼答案

            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);
    
  5. # 5 楼答案

    如果要使用for循环,可以在System.out.println("number must be 1-3 only, try again");之后的最后一个else块中编写类似的--i;代码

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

    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;
        }
    }
    

    更新

    在我上面的回答中,我刚刚说过,如何在对原始代码进行最小更改的情况下重复for循环。正如标题中所问的那样。但是@JayPrakash说这不是一个完美的答案,所以投票否决了它。好的,让我们试着找到一个完美的:

    public static void main(String[] args) {
        guess(2, 1, 4, new int[]{2, 4});
    }
    
    /**
     *
     * @param tries tries count
     * @param from from range, inclusive
     * @param to to range inclusive
     * @param puzzled array with puzzled values
     * @return array, which contains only puzzled answers from a user
     */
    public static int[] guess(int tries, int from, int to, int[] puzzled) {
        if (puzzled == null || puzzled.length < 1) throw new IllegalArgumentException("puzzled");
        if (Math.abs(from - to) < 1) throw new IllegalArgumentException("range");
        if (tries < 1 || Math.abs(from - to) < tries - 1) throw new IllegalArgumentException("tries"); //`tries - 1` because `to` is inclusive
        if (from > to) {
            int tmp = from; from = to; to = tmp;
        }
        Scanner sc = new Scanner(System.in);
        int answers[] = new int[tries], //all previous user answers
            result[] = new int[tries];
    
        System.out.printf("You have only %d chances to get all\n", tries);
        int i = 0, j = 0;
        while (i < tries && j < puzzled.length) { // `j < puzzled.length` break if all puzzled answers is found
            System.out.printf("%d Enter number %d-%d: ", (i + 1), from, to);
            int number = sc.nextInt();
            if (number < from || number > to) {
                System.out.printf("Number must be in %d-%d range only, try again\n", from, to);
                continue;
            }
            if (contains(answers, number, i)) {
                System.out.printf("Number %d is used before\n", number);
                continue;
            }
    
            answers[i++] = number;
            if (contains(puzzled, number)) {
                System.out.println("Wow!! you got it");
                result[j++] = number;
            } else {
                System.out.println("Not here");
            }
        }
        if (j == puzzled.length)
            System.out.println("You got all");
        else
            System.out.printf("You got %d only\n", j);
        return Arrays.copyOfRange(result, 0, j);
    }
    
    private static boolean contains(int[] array, int value) {
        return contains(array, value, array.length);
    }
    
    private static boolean contains(int[] array, int value, int lookTo) {
        for (int i = 0; i < lookTo; i++)
            if (array[i] == value)
                return true;
        return false;
    }