Skip to content

Instantly share code, notes, and snippets.

@hatone
Created April 26, 2012 05:58
Show Gist options
  • Select an option

  • Save hatone/2496410 to your computer and use it in GitHub Desktop.

Select an option

Save hatone/2496410 to your computer and use it in GitHub Desktop.
forループまわり
public class Diamond {
public static void main (String[] args) {
for (int j = 0; j < 8; j++) {
for (int i = 7; i-j>0; i-- ) {
System.out.print(" ");
}
for (int i = 1; i<j*2; i++){
System.out.print("*");
}
System.out.println("");
}
for (int j = 1; j < 7; j++) {
for (int i = 0; i < j; i++) {
System.out.print(" ");
}
for (int i = 13; i - j*2 > 0 ; i--){
System.out.print("*");
}
System.out.println("");
}
}
}
演習1:
Triangle.javaというファイルを作成し、以下の実行結果が表示されるプログラムをfor文を使用して作成しなさい。
---- 実行結果 ----
*
**
***
****
*****
******
*******
********
*********
**********
*********
********
*******
******
*****
****
***
**
*
----
演習2:
Diamond.javaというファイルを作成し、以下の実行結果が表示されるプログラムをfor文を使用して作成しなさい。
---- 実行結果 ----
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
----
演習3:
Mottled.javaというファイルを作成し、以下の実行結果が表示されるプログラムをfor文を使用して作成しなさい。
---- 実行結果 ----
* * * * * * *
*** *** *** *** *** *** ***
***** ***** ***** ***** ***** ***** *****
******* ******* ******* ******* ******* ******* *******
***** ***** ***** ***** ***** ***** *****
*** *** *** *** *** *** ***
* * * * * * *
* * * * * * *
*** *** *** *** *** *** ***
***** ***** ***** ***** ***** ***** *****
******* ******* ******* ******* ******* ******* *******
***** ***** ***** ***** ***** ***** *****
*** *** *** *** *** *** ***
* * * * * * *
* * * * * * *
*** *** *** *** *** *** ***
***** ***** ***** ***** ***** ***** *****
******* ******* ******* ******* ******* ******* *******
***** ***** ***** ***** ***** ***** *****
*** *** *** *** *** *** ***
* * * * * * *
----
public class Triangle{
public static void main(String[] args) {
for(int i = 0; i<5; i++) {
for(int j = 0; j < i; j++) {
System.out.print("a");
}
System.out.println("");
}
for(int i = 5; i>0; i--) {
for(int j = 0; j < i; j++) {
System.out.print("b");
}
System.out.println("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment