Last active
December 27, 2015 19:09
-
-
Save shigemk2/7374386 to your computer and use it in GitHub Desktop.
やさしいJava 10
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) | |
{ | |
String str = "Hello"; | |
char ch1 = str.charAt(0); | |
char ch2 = str.charAt(1); | |
int len = str.length(); | |
System.out.println(str+"の1番目の文字は"+ch1+"です"); | |
System.out.println(str+"の2番目の文字は"+ch2+"です"); | |
System.out.println(str+"の長さは"+len+"です"); | |
} | |
} |
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 Sample2 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("英字を入力してください"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); | |
String stru = str.toUpperCase(); // 大文字変換 | |
String strl = str.toLowerCase(); // 小文字変換 | |
System.out.println("大文字に変換すると"+stru+"です"); | |
System.out.println("小文字に変換すると"+strl+"です"); | |
} | |
} |
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 str1 = br.readLine(); | |
System.out.println("検索文字を入力してください"); | |
String str2 = br.readLine(); | |
char ch = str2.charAt(0); | |
int num = str1.indexOf(ch); | |
if (num != -1) | |
System.out.println(str1+"の"+(num+1)+"番目に「"+ch+"」が見つかりました"); | |
else | |
System.out.println(str1+"に「"+ch+"」はありません"); | |
} | |
} |
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 str1 = br.readLine(); | |
System.out.println("追加する文字列を入力してください"); | |
String str2 = br.readLine(); | |
StringBuffer sb = new StringBuffer(str1); | |
sb.append(str2); | |
System.out.println(str1+"に"+str2+"を追加すると"+sb+"です"); | |
} | |
} |
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 Sample5 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("整数を2つ入力してください"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str1 = br.readLine(); | |
String str2 = br.readLine(); | |
int num1 = Integer.parseInt(str1); | |
int num2 = Integer.parseInt(str2); | |
int ans = Math.max(num1, num2); | |
System.out.println(num1+"と"+num2+"のうち大きいほうが"+ans+"です"); | |
} | |
} |
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 Car | |
{ | |
private int num; | |
private double gas; | |
public Car() | |
{ | |
num = 0; | |
gas = 0.0; | |
System.out.println("車を作成しました"); | |
} | |
public void setCar(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("ナンバーを"+num+"ガソリン量を"+gas+"にしました"); | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample6 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1; | |
System.out.println("car1を宣言しました"); | |
car1 = new Car(); | |
car1.setCar(1234,20.5); | |
Car car2; | |
System.out.println("car2を宣言しました"); | |
car2 = car1; | |
System.out.println("car2にcar1を代入しました"); | |
System.out.println("car1がさす"); | |
car1.show(); | |
System.out.println("car2がさす"); | |
car2.show(); | |
} | |
} |
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 Car | |
{ | |
private int num; | |
private double gas; | |
public Car() | |
{ | |
num = 0; | |
gas = 0.0; | |
System.out.println("車を作成しました"); | |
} | |
public void setCar(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("ナンバーを"+num+"ガソリン量を"+gas+"にしました"); | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample7 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1; | |
System.out.println("car1を宣言しました"); | |
car1 = new Car(); | |
car1.setCar(1234,20.5); | |
Car car2; | |
System.out.println("car2を宣言しました"); | |
car2 = car1; | |
System.out.println("car2にcar1を代入しました"); | |
System.out.println("car1がさす"); | |
car1.show(); | |
System.out.println("car2がさす"); | |
car2.show(); | |
System.out.println("car1がさす車に変更を加えます"); | |
car1.setCar(2345,30.5); | |
System.out.println("car1がさす"); | |
car1.show(); | |
System.out.println("car2がさす"); | |
car2.show(); | |
} | |
} |
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 Car | |
{ | |
private int num; | |
private double gas; | |
private String name; // クラス型の変数を使ったフィールド | |
public Car() | |
{ | |
num = 0; | |
gas = 0.0; | |
name = "名無し"; | |
System.out.println("車を作成しました"); | |
} | |
public void setCar(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("ナンバーを"+num+"ガソリン量を"+gas+"にしました"); | |
} | |
public void setName(String nm) | |
{ | |
name = nm; | |
System.out.println("名前を"+name+"にしました"); | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
System.out.println("名前は"+this.name+"です"); | |
} | |
} | |
class Sample8 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1; | |
System.out.println("car1を宣言しました"); | |
car1 = new Car(); | |
car1.setCar(1234,20.5); | |
car1.setName("1号車"); | |
car1.show(); | |
} | |
} |
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 Car | |
{ | |
private int num; | |
private double gas; | |
public Car() | |
{ | |
num = 0; | |
gas = 0.0; | |
System.out.println("車を作成しました"); | |
} | |
public void setCar(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("ナンバーを"+num+"ガソリン量を"+gas+"にしました"); | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample9 | |
{ | |
public static void main(String[] args) | |
{ | |
Car[] cars; | |
cars = new Car[3]; | |
for (int i=0; i<cars.length; i++) { | |
cars[i] = new Car(); | |
} | |
cars[0].setCar(1234,20.5); | |
cars[1].setCar(2345,30.5); | |
cars[2].setCar(3456,40.5); | |
for (int i=0; i<cars.length; i++) { | |
cars[i].show(); | |
} | |
} | |
} |
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("文字列を入力してください"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str1 = br.readLine(); | |
StringBuffer sb = new StringBuffer(str1); | |
sb.reverse(); | |
System.out.println(str1+"を逆順にすると"+sb+"です"); | |
} | |
} |
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 SampleP3 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("文字列を入力してください"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str1 = br.readLine(); | |
StringBuffer sb = new StringBuffer(str1); | |
char ch = 'a'; | |
System.out.println(ch+"の挿入位置を入力してください"); | |
String str2 = br.readLine(); | |
int num = Integer.parseInt(str2); | |
sb.insert(num, ch); | |
System.out.println(sb+"になりました"); | |
} | |
} |
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 SampleP4 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("整数を2つ入力してください"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str1 = br.readLine(); | |
String str2 = br.readLine(); | |
int num1 = Integer.parseInt(str1); | |
int num2 = Integer.parseInt(str2); | |
int ans = Math.min(num1, num2); | |
System.out.println(num1+"と"+num2+"のうち小さいほうが"+ans+"です"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment