2015-09-06 42 views
-1

我怎樣才能得到輸出多少次程序已經循環?我怎樣才能得到輸出的程序有多少次循環?

輸出示例:

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. 

繼承人是我的代碼:

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

} 
} 
+1

如果你問有多少次,對於循環已經執行只是打印我(但你並不需要將執行4次做) – e4c5

+0

我知道循環將是4倍,因爲它提示用戶輸入4值..我的問題是過程上的循環排序的價值,以獲得最小的和最大的價值..如果我可以張貼圖片可以更容易理解.. – Harith

+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."); 

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

    } 
} 
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 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]; 
    int i; 
    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); 
    System.out.println("The loop has executed "+i+" times"); 
} 
} 
+0

這應該告訴你你的第二次循環執行了多少次。 – Sam

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!!"); 
     } 
    }