Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Last active December 27, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/7330582 to your computer and use it in GitHub Desktop.
Save shigemk2/7330582 to your computer and use it in GitHub Desktop.
やさしいJava 3
名前 記憶出来る値の範囲
boolean trueまたはfalse
char 2バイト文字(¥u0000-¥uffff)
byte 1バイト整数(-128-127)
short 2バイト整数(-32768-32767)
int 4バイト整数(-2147483648-2147483647)
long 8バイト整数(-9223372036854775808-9223372036854775807)
float 4バイト単精度浮動小数点数
double 8バイト倍精度浮動小数点数
class Sample1
{
public static void main(String[] args)
{
// 変数にデータを格納する
// 変数を使うためには変数を宣言するという作業が必要である
// 型名 識別子 = 式;
int num;
num = 3;
System.out.println("変数numの値は" + num + "です");
}
}
// 値を上書きし、変数の値を変更する
class Sample2
{
public static void main(String[] args)
{
int num;
num = 3;
System.out.println("変数numの値は" + num + "です。");
num = 5;
System.out.println("変数numの値を変更です。");
System.out.println("変数numの値は" + num + "です。");
}
}
// 値を上書きし、変数の値を変更する
// 変数の宣言はブロック内で
class Sample3
{
public static void main(String[] args)
{
// なお、int型の変数に少数は格納できない
int num1, num2;
num1 = 3;
System.out.println("変数numの値は" + num1 + "です。");
// 変数num2の値は変数num1の値と同じになりました
num2 = num1;
System.out.println("変数num1の値を変数num2に代入しました。");
System.out.println("変数num2の値は" + num2 + "です。");
}
}
// 標準入力であるキーボードからの入力を受け付ける
import java.io.*;
class Sample4
{
public static void main(String[] args) throws IOException
{
System.out.println("文字列を入力してください");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// キーボードから入力した文字列をstrに読み込む
String str = br.readLine();
System.out.println(str + "が入力されました。");
}
}
// 標準入力であるキーボードからの入力を受け付ける
import java.io.*;
class Sample5
{
public static void main(String[] args) throws IOException
{
System.out.println("文字列を入力してください");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// キーボードから入力した文字列をstrに読み込む
String str = br.readLine();
int num = Integer.parseInt(str);
System.out.println(str + "が入力されました。");
}
}
// 標準入力であるキーボードからの入力を受け付ける
import java.io.*;
class Sample6
{
public static void main(String[] args) throws IOException
{
System.out.println("整数を2つ入力してください");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// キーボードから入力した文字列をstrに読み込む
// 二回続けて入力
String str1 = br.readLine();
String str2 = br.readLine();
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
System.out.println(num1 + "が入力されました。");
System.out.println(num2 + "が入力されました。");
}
}
class SampleP2
{
public static void main(String[] args)
{
// charだと少数は代入できん
double ch;
ch = 3.14;
System.out.println("変数chの値は" + ch + "です。");
}
}
import java.io.*;
class SampleP3
{
public static void main(String[] args) throws IOException
{
System.out.println("あなたは何歳ですか");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// キーボードから入力した文字列をstrに読み込む
String str = br.readLine();
int num = Integer.parseInt(str);
System.out.println(num + "歳です。");
}
}
import java.io.*;
class SampleP4
{
public static void main(String[] args) throws IOException
{
System.out.println("円周率の値はいくつですか");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// キーボードから入力した文字列をstrに読み込む
String str = br.readLine();
double num = Double.parseDouble(str);
System.out.println(num + "です。");
}
}
// 標準入力であるキーボードからの入力を受け付ける
import java.io.*;
class SampleP5
{
public static void main(String[] args) throws IOException
{
System.out.println("身長と体重を入力してください");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// キーボードから入力した文字列をstrに読み込む
// 二回続けて入力
String str1 = br.readLine();
String str2 = br.readLine();
double num1 = Double.parseDouble(str1);
double num2 = Double.parseDouble(str2);
System.out.println("身長は" + num1 + "です。");
System.out.println("体重は" + num2 + "です。");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment