如何让程序继续进入while循环?

时间:2015-11-19 07:39:33

标签: java

编写一个名为makeGuesses的方法,它将猜测1到50之间的数字,直到它猜测至少为48.它应该报告每个猜测,最后应报告所做的猜测总数。以下是一个示例执行:

 guess = 43
 guess = 47
 guess = 45
 guess = 27
 guess = 49
 total guesses = 5

这是我的代码所以:

import java.util.*;

public class Guesses 
{
   public static void main(String[] args) 
   {
    Scanner console = new Scanner(System.in);
    Random r = new Random();

    int i = -1;
    int count = 0;
       if (!(i >= 1 && i <= 50))
       { 
        System.out.print("guesses = ");
        i = r.nextInt(50)+1;
        System.out.println(i);
        count++;
       }

     System.out.println("total guesses = "+ count);
    }
}

但是它只会引起注意而不会继续下去吗? 我的产量远远超过这个:

guesses = 36
total guesses = 1

OR

guesses = 23
total guesses = 1

只能猜一次。

2 个答案:

答案 0 :(得分:0)

这部分代码

<script>
    $(document).ready(function() {
        $("#adropdownDetails").change(function() {
            var value = $('#adropdownDetails:selected').text();
            $.ajax({
                type : 'POST',
                url : 'envi',
                data : {
                   selectedaname :$('#adropdownDetails:selected').text()
                },
                success : function() {
                    alert("success");
                }
            });
        });
     });
</script>

是一个If语句,而不是一个循环。这就是为什么它只进入一次。在它之前添加while循环或者替换if while或者你需要的任何东西

答案 1 :(得分:0)

我会这样做:

public static void guessCounter() {
    int maxNr = 50;
    int upperLimit = 48;
    int totalGuesses = 0;
    int currGuess;
    Random r = new Random();
    do {
        currGuess = r.nextInt(maxNr) + 1;
        System.out.println("guess = " + currGuess);
        totalGuesses++;
    } while (currGuess < upperLimit);
    System.out.println("total guesses = " + totalGuesses);
}