- 演算子はオペランドと組み合わせて式を作る
- キャスト演算子を使うと型を変換できる
- 代入のときに型が変換される場合がある
- 四則演算のときに型が変換される場合がある
- 値が小さなサイズの型に変換された場合、値の一部が失われることがある
Last active
December 27, 2015 13:59
-
-
Save shigemk2/7337621 to your computer and use it in GitHub Desktop.
やさしいJava 4
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) | |
{ | |
System.out.println("1+2は" + (1+2) + "です"); | |
System.out.println("3*4は" + (3*4) + "です"); | |
} | |
} | |
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
// 演算子に異なる2つのオペランドを記述した場合は、一方のオペランドを大きなサイズの型に変換してから演算を行う | |
class Sample10 | |
{ | |
public static void main(String[] args) | |
{ | |
int d = 2; | |
double pi = 3.14; | |
System.out.println("直径が" + d + "センチの円です"); | |
System.out.println("円周は" + (d*pi) + "センチです"); | |
} | |
} |
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 Sample11 | |
{ | |
public static void main(String[] args) | |
{ | |
int num1 = 5; | |
int num2 = 4; | |
// double div = num1 / num2; // 演算結果は1.0で思うようにいかない int/intの結果をdoubleに型変換してるから。 | |
double div = (double)num1 / (double)num2; // これでdouble/doubleで期待した演算結果が得られる | |
System.out.println("5/4は" + div + "です"); | |
} | |
} |
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) | |
{ | |
int num1 = 2; | |
int num2 = 3; | |
int sum = num1+num2; | |
System.out.println("変数num1の値は" + num1 + "です"); | |
System.out.println("変数num2の値は" + num2 + "です"); | |
System.out.println("num1+num2の値は" + sum + "です"); | |
num1 = num1+1; | |
System.out.println("num1にnum1をたすと" + num1 + "です"); | |
} | |
} |
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("変数を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); | |
System.out.println("足し算の結果は" + (num1+num2) + "です"); | |
} | |
} |
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 Sample4 | |
{ | |
public static void main(String[] args) | |
{ | |
int num1 = 10; | |
int num2 = 5; | |
// オペランド+は、数値を文字列に変換して文字列を連結する | |
System.out.println("num1とnum2にいろいろな演算を行います"); | |
System.out.println("num1+num2は" + (num1+num2) + "です"); | |
System.out.println("num1-num2は" + (num1-num2) + "です"); | |
System.out.println("num1*num2は" + (num1*num2) + "です"); | |
System.out.println("num1/num2は" + (num1/num2) + "です"); | |
System.out.println("num1%num2は" + (num1%num2) + "です"); | |
} | |
} |
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 a = 0; | |
int b = 0; | |
b = ++a; | |
// aは増え、bは増える | |
System.out.println("代入前にインクリメントしたのでbの値は" + b + "です。"); | |
b = a++; | |
// aは増えるが、bは増えない | |
System.out.println("代入後にインクリメントしたのでbの値は" + b + "です。"); | |
System.out.println("a: " + a); | |
System.out.println("b: " + b); | |
} | |
} |
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 Sample6 | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.println("整数を3つ入力してください"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str1 = br.readLine(); | |
String str2 = br.readLine(); | |
String str3 = br.readLine(); | |
int sum = 0; | |
sum += Integer.parseInt(str1); | |
sum += Integer.parseInt(str2); | |
sum += Integer.parseInt(str3); | |
System.out.println("3つの数の合計は" + 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 Sample7 | |
{ | |
public static void main(String[] args) | |
{ | |
System.out.println("1+2は" + 1+2 + "です"); // 演算されず、文字列として連結される | |
System.out.println("3*4は" + 3*4 + "です"); | |
} | |
} |
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) | |
{ | |
int inum = 160; | |
System.out.println("身長は" + inum + "センチです"); | |
System.out.println("double型の変数に代入します"); | |
double dnum = inum; // 大きなサイズの型に代入します。 | |
// int inum2 = dnum; // 小さいサイズの型には代入できず、エラーになる | |
System.out.println("身長は" + dnum + "センチです"); | |
} | |
} |
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 Sample9 | |
{ | |
public static void main(String[] args) | |
{ | |
double dnum = 160.0; | |
System.out.println("身長は" + dnum + "センチです"); | |
System.out.println("int型の変数に代入します"); | |
// int inum = dnum; // 小さいサイズの型には代入できず、コンパイルできない | |
int inum = (int)dnum; | |
System.out.println("身長は" + inum + "センチです"); | |
} | |
} |
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 SampleP2 | |
{ | |
public static void main(String[] args) | |
{ | |
System.out.println("0-4="+(0-4)); | |
System.out.println("3.14*2="+(3.14*2)); | |
System.out.println("5/3="+(5/3)); | |
System.out.println("30%7="+(30%7)); | |
System.out.println("(7+32)/5="+((double)(7+32)/(double)5)); | |
} | |
} |
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 str = br.readLine(); | |
int area = Integer.parseInt(str) * Integer.parseInt(str); | |
System.out.println("正方形の面積は"+area+"です"); | |
} | |
} |
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("三角形の高さと底辺を入力してください"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String height = br.readLine(); | |
String buttom = br.readLine(); | |
Double area = (Double.parseDouble(height) * Double.parseDouble(buttom)) / 2.0; | |
System.out.println("三角形の面積は"+area+"です"); | |
} | |
} |
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("科目1-5の点数を入力してください"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String one = br.readLine(); | |
String two = br.readLine(); | |
String three = br.readLine(); | |
String four = br.readLine(); | |
String five = br.readLine(); | |
int ammount = Integer.parseInt(one) + | |
Integer.parseInt(two) + | |
Integer.parseInt(three) + | |
Integer.parseInt(four) + | |
Integer.parseInt(five); | |
System.out.println("五科目合計は"+ammount+"点です"); | |
Double average = (double)ammount / 5; | |
System.out.println("五科目平均は"+average+"点です"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment