如何获得程序循环次数的输出?

时间:2015-09-06 06:51:50

标签: java

如何获得程序循环次数的输出?

示例输出:

Enter Your Name: Harith

Harith, Please enter 4 numbers

Number 1:6

Number 2:7

Number 3:5

Number 4:10

Smallest = 5
Largest = 10

This program has repeated for 2 times.

Heres是我的代码:

import java.io.*;
class loop
{
public static void main(String args[])throws IOException
{
    BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    String temp,name;
    int number,largest,smallest;
    System.out.print("Enter Your Name: ");
    name = cin.readLine();
    int num[] = new int[4];
    System.out.println(name+", Please enter 4 numbers");
    for(int i=0;i<4;i++)
    {   
        System.out.print("Number "+(i+1)+":");
        temp = cin.readLine();
        num[i] = Integer.parseInt(temp);
    }
    largest=num[0];
    smallest=num[0];
    for(int i=0;i<4;i++)
    {
        if(num[i]>largest)
        {
            largest=num[i];
        }
        else if(num[i]<smallest)
        {
            smallest=num[i];
        }
    }
    System.out.println("Smallest = "+smallest);
    System.out.println("Largest = "+largest);

}   
}

4 个答案:

答案 0 :(得分:0)

如果您只想知道循环循环次数,请引入一个变量,例如int loopCounter = 0; 然后在程序中每个循环结束之前将该变量递增1并最后打印该变量。 例如。

import java.io.*;

class loop {

    public static void main(String args[]) throws IOException {
        int loopCounter = 0;
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String temp, name;
        int number, largest, smallest;
        System.out.print("Enter Your Name: ");
        name = cin.readLine();
        int num[] = new int[4];
        System.out.println(name + ", Please enter 4 numbers");
        for (int i = 0; i < 4; i++) {
            System.out.print("Number " + (i + 1) + ":");
            temp = cin.readLine();
            num[i] = Integer.parseInt(temp);
            loopCounter++;
        }
        largest = num[0];
        smallest = num[0];
        for (int i = 0; i < 4; i++) {
            if (num[i] > largest) {
                largest = num[i];
            } else if (num[i] < smallest) {
                smallest = num[i];
            }
            loopCounter++;
        }
        System.out.println("Smallest = " + smallest);
        System.out.println("Largest = " + largest);
        System.out.println("Looped " + loopCounter + " times.");

    }
}

答案 1 :(得分:0)

请输入一个变量来计算循环迭代次数

package com.test.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class A1 {

    public static void main(String args[])throws IOException
    {
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String temp,name;
        int largest,smallest;
        System.out.print("Enter Your Name: ");
        name = cin.readLine();
        int num[] = new int[4];
        System.out.println(name+", Please enter 4 numbers");
        int loopCount = 0;
        for(int i=0;i<4;i++)
        {   
            System.out.print("Number "+(i+1)+":");
            temp = cin.readLine();
            num[i] = Integer.parseInt(temp);
            loopCount++;
        }
        System.out.println("Loop Counted :: "+loopCount);
        largest=num[0];
        smallest=num[0];
        for(int i=0;i<4;i++)
        {
            if(num[i]>largest)
            {
                largest=num[i];
            }
            else if(num[i]<smallest)
            {
                smallest=num[i];
            }
        }
        System.out.println("Smallest = "+smallest);
        System.out.println("Largest = "+largest);

    }   
}

答案 2 :(得分:0)

while(i can't enter) { do nothing and wait...}

答案 3 :(得分:0)

经过几次尝试和错误后,我的答案就出现了。谢谢你的帮助。

import java.io.*;
    class loop
    {
        public static void main(String args[])throws IOException
        {
            BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
            String temp,name;
            int largest,smallest,time=0,i;
            do{
            System.out.print("Enter Your Name: ");
            name = cin.readLine();
            int num[] = new int[4];
            System.out.println(name+", Please enter 4 numbers\n");
            for(i=0;i<4;i++)
            {   
                System.out.print("Number " +(i+1)+":");
                temp = cin.readLine();
                num[i] = Integer.parseInt(temp);
            }
            largest=num[0];
            smallest=num[0];
            for(i=0;i<4;i++)
            {
                if(num[i]>largest)
                {
                    largest=num[i];
                }
                else if(num[i]<smallest)
                {
                    smallest=num[i];
                }
            }
            System.out.println("Smallest = "+smallest);
            System.out.println("Largest = "+largest+"\n");
            time++;
            }while(smallest<=5);
            System.out.println("The program has been repeated for "+(time)+" times");
            System.out.print("Thanks You!!");
        }
    }