アットウィキロゴ

繰り返し

for

  1. import java.io.*;
  2.  
  3. class sample05{
  4. public static void main(String[] args){
  5.  
  6. try{
  7.  
  8. System.out.print("繰り返し回数=");
  9. String a = br.readLine();
  10. int b = Integer.parseInt(a);
  11.  
  12. for(int i = 1; i <= b;i++){
  13. System.out.printf("%04d,", i);
  14. }
  15. System.out.println();
  16.  
  17. }catch(Exception ex){
  18. ex.printStackTrace();
  19.  
  20. }
  21. }
  22. }
  23.  
書式
for(初期値;継続条件;増分処理){
   testfunc();
}
 

while

  1. import java.io.*;
  2.  
  3. class sample06{
  4. public static void main(String[] args){
  5.  
  6. try{
  7.  
  8. System.out.print("繰り返し回数=");
  9. String a = br.readLine();
  10. int b = Integer.parseInt(a);
  11.  
  12. int i = 1;
  13. while(i <= b){
  14. System.out.printf("%04d,", i);
  15. i++;
  16. }
  17.  
  18. }catch(Exception ex){
  19. ex.printStackTrace();
  20.  
  21. }
  22. }
  23. }
  24.  
書式
while(継続条件){
   testfunc();
}
 




最終更新:2012年09月02日 10:15