Last active
December 27, 2015 16:29
-
-
Save shigemk2/7355059 to your computer and use it in GitHub Desktop.
やさしいJava 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample1 | |
{ | |
public static void main(String[] args) | |
{ | |
for(int i=1; i<=5; i++) { | |
System.out.println("繰り返しています"); | |
} | |
System.out.println("繰り返しが終わりました"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class Sample10 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("成績を入力してください(1-5)"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); | |
int res = Integer.parseInt(str); | |
switch(res) { | |
case 1: | |
case 2: | |
System.out.println("もうすこしがんばりましょう"); | |
break; | |
case 3: | |
case 4: | |
System.out.println("この調子でがんばりましょう"); | |
break; | |
case 5: | |
System.out.println("大変優秀です"); | |
break; | |
default: | |
System.out.println("1-5までの成績を入力せよ"); | |
break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class Sample11 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("何番目の処理を飛ばしますか(1-10)"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); | |
int res = Integer.parseInt(str); | |
for (int i=1; i<=10; i++) { | |
if (i == res) | |
continue; | |
System.out.println(i+"番目の処理です"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample2 | |
{ | |
public static void main(String[] args) | |
{ | |
for(int i=1; i<=5; i++) { | |
System.out.println(i+"番目の繰り返しです"); | |
} | |
System.out.println("繰り返しが終わりました"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class Sample3 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("いくつ*を入力しますか"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); | |
int num = Integer.parseInt(str); | |
for(int i=1; i<=num; i++) { | |
System.out.println("*"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class Sample4 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("いくつまで合計を求めますか"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); | |
int num = Integer.parseInt(str); | |
int sum = 0; | |
for(int i=1; i<=num; i++) { | |
sum += i; | |
} | |
System.out.println("1から"+num+"までの合計は"+sum+"です"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample5 | |
{ | |
public static void main(String[] args) | |
{ | |
int i = 1; | |
while (i <= 5) { | |
System.out.println(i+"番目の繰り返しです"); | |
i++; | |
} | |
System.out.println("繰り返しが終わりました"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample6 | |
{ | |
public static void main(String[] args) | |
{ | |
int i = 1; | |
do { | |
System.out.println(i+"番目の繰り返しです"); | |
i++; | |
}while (i <= 5); | |
System.out.println("繰り返しが終わりました"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample7 | |
{ | |
public static void main(String[] args) | |
{ | |
for(int i=0;i<5;i++) { | |
for(int j=0;j<3;j++) { | |
System.out.println("iは"+i+":jは"+j); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample8 | |
{ | |
public static void main(String[] args) | |
{ | |
boolean bl = false; | |
for(int i=0;i<5;i++) { | |
for(int j=0;j<5;j++) { | |
if (bl == false) { | |
System.out.print("*"); | |
bl = true; | |
} | |
else { | |
System.out.print("-"); | |
bl = false; | |
} | |
} | |
System.out.print("\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class Sample9 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("何番目でループを中止しますか(1-10)"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); | |
int res = Integer.parseInt(str); | |
for(int i=1;i<=10;i++) { | |
System.out.println(i+"番目の処理です"); | |
if (i == res) { | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleP1 { | |
public static void main(String[] args) | |
{ | |
System.out.println("1-10までの偶数を出力します"); | |
for (int i=1; i<=10; i++) { | |
if ((i%2)==0) { | |
System.out.println(i); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class SampleP2 { | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("テストの点数を入力してください(0で終了)"); | |
int sum = 0; | |
int res = 0; | |
do { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); | |
res = Integer.parseInt(str); | |
sum += res; | |
} while (res != 0); | |
System.out.println("テストの合計点は"+sum+"点です"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleP3 { | |
public static void main(String[] args) | |
{ | |
int res = 1; | |
for (int i=1; i<=9; i++) { | |
for (int j=1; j<=9; j++) { | |
System.out.print((res*j)+"\t"); | |
} | |
res++; | |
System.out.print("\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleP4 { | |
public static void main(String[] args) | |
{ | |
for (int i=1; i<=5; i++) { | |
for (int j=1; j<=i; j++) { | |
System.out.print("*"); | |
} | |
System.out.print("\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class SampleP5 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("2以上の素数を入力してください"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); | |
int res = Integer.parseInt(str); | |
boolean prime = true; | |
for(int i=2;i<res;i++) { | |
if (res == 2) { | |
prime = true; | |
break; | |
} | |
else if (res % i == 0) { | |
prime = false; | |
} | |
} | |
if (prime == true) { | |
System.out.println(res+"は素数です"); | |
} | |
else { | |
System.out.println(res+"は素数ではありません"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment