while循环如何显示循环运行次数

时间:2019-02-22 12:27:40

标签: java

只要条件sumaA> sumaB被满足,我就想运行“ while”循环。 如何显示迭代次数以及sumaA和sumaB的当前值?

import java.util.*;

class Sum {
    public static void main(String[] args) {

        int sumaA = 1000;
        int sumaB = 0;

        Random rand = new Random();
        int n = rand.nextInt(9);
        int g = rand.nextInt(49);{
            System.out.println("Random N= "+ n);

            sumaA += n;
            System.out.println("sumaA: "+sumaA);
            System.out.println();

            System.out.println("Random N= "+ g);
            sumaB += g;
            System.out.println("sumaB: "+sumaB);

            while (sumaA>sumaB);

            }
        System.out.println("loop runs numbers");
        System.out.println("value sumaA" + sumaA + "value sumaB " + sumaB);

        }
    }

4 个答案:

答案 0 :(得分:0)

您必须更正while循环,因为在当前状态下,它什么也不会做。

这是对您的代码的更正,基本上可以正常工作(我不知道所需的输出,但是看起来还可以):

public static void main(String[] args) {

    int sumaA = 1000;
    int sumaB = 0;

    Random rand = new Random();
    int n = rand.nextInt(9);
    int g = rand.nextInt(49);

    int runCounter = 0;

    while (sumaA > sumaB) {
        runCounter++;
        System.out.println("Loop run number " + runCounter);
        System.out.println("Random N = " + n);

        sumaA += n;
        System.out.println("sumaA: " + sumaA);
        System.out.println();

        System.out.println("Random N= " + g);
        sumaB += g;
        System.out.println("sumaB: " + sumaB);
    }

    System.out.println("loop ran " + runCounter + " times");
    System.out.println("value sumaA" + sumaA + "value sumaB " + sumaB);
}

请查看循环的外观。每次循环运行将要执行的所有内容都必须在循环体内(花括号)。

答案 1 :(得分:0)

您必须正确构造while循环,并获取局部变量counter并将其初始化为0。在迭代时,将counter变量加+1。请考虑以下代码:

public static void main( String[] args )
   {
      int sumaA = 1000;
      int sumaB = 0;
      int counter = 0;
      Random rand = new Random();
      int n = rand.nextInt( 9 );
      int g = rand.nextInt( 49 );
      while ( sumaA > sumaB )
      {
         System.out.println( "Random N= " + n );

         sumaA += n;
         System.out.println( "sumaA: " + sumaA );
         System.out.println();

         System.out.println( "Random N= " + g );
         sumaB += g;
         System.out.println( "sumaB: " + sumaB );
         counter++;
      }
      System.out.println( "loop runs numbers " + counter );
      System.out.println( "value sumaA " + sumaA + "value sumaB " + sumaB );
   }

答案 2 :(得分:0)

while循环在小节内的代码,直到括号内的条件为假。例如,这段代码:

int i = 0;
loops = 0
while(i < 6){
  loops++;
  System.out.println("loop runs numbers"+loops);
  i+=2;
  System.out.println("i: "+i);
}

输出:

  

循环运行数字1

     

i:2

     

循环运行数字2

     

i:4

     

循环运行数字3

     

i:6

然后停止,因为我不小于6。

现在代码的问题是,在while括号内没有任何指令,因此括号内的条件以true开头,永远不会为false,因为您没有在循环内更改任何内容。您应该做的是将代码移到while括号内,如果希望计算循环运行次数,则添加一个“ loops”变量。最终代码应如下所示:


import java.util.*;

class Sum {
    public static void main(String[] args) {
    int sumaA = 1000;
    int sumaB = 0;
    int loops = 0;

    while (sumaA>sumaB){
      Random rand = new Random();
      int n = rand.nextInt(9);
      int g = rand.nextInt(49);{
      System.out.println("Random N= "+ n);
      sumaA += n;
      System.out.println("sumaA: "+sumaA);
      System.out.println();
      System.out.println("Random N= "+ g);
      sumaB += g;
      System.out.println("sumaB: "+sumaB);
      loops++;
      System.out.println("loop runs numbers"+loops);
      System.out.println("value sumaA" + sumaA + "value sumaB " + sumaB);
    }
  }
}

答案 3 :(得分:0)

    do {  
    int n = rand.nextInt(9);
    int g = rand.nextInt(49);

        System.out.println("Random N= "+ n);

        sumaA += n;
        System.out.println("sumaA: "+sumaA);
        System.out.println();

        System.out.println("Random N= "+ g);
        sumaB += g;
        System.out.println("sumaB: "+sumaB);
        System.out.println("loop runs numbers");
        System.out.println("value sumaA" + sumaA + "value sumaB " + sumaB);
        }  while (sumaA>sumaB);

    }

你好,这是你想做什么吗?