class Js_1_1{
public static void main(String[] args){
System.out.println("Hello World!");
System.out.println("This is a program.");
}
}
|
class Js_1_1{
public static void main(String[] args){System.out.println("Hello World!");System.out.println("This is a program.");}
}
|
/*
Hello Worldと表示するプログラム。
*/
class Js_1_1{
public static void main(String[] args){//メインのプログラム部分
System.out.println("Hello World!");
System.out.println("This is a program.");
}
}
|
Go to Index
class Js_1_2{
public static void main(String[] args){
int a,b,c;//説明の都合上ここを1行目とする。
a=1;b=2;
c=a+b;
System.out.println("cの値は"+c);
a=a-b;
System.out.println("aの値は"+a);
}
}
|
Go to Index
import java.io.*;
class Js_1_3{
public static void main(String[] args) throws Exception{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));//ここを1行目とする。
System.out.print("A=?");
int a=Ineteger.parseInt(bf.readLine());
System.out.print("B=?");
int b=Ineteger.parseInt(bf.readLine());
if(a==b){
System.out.println("AとBは等しい");
}else{
System.out.println("AとBは異なる");
}
}
}
|
if(条件文){
条件文が真の場合の処理
}else{
条件文が偽の場合の処理
}
|
import java.io.*;
class Js_1_4{
public static void main(String[] args) throws Exception{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));//ここを1行目とする。
System.out.print("N=?");
int n=Integer.parseInt(bf.readLine());
int sum,i;
i=1;
while(i<n){
sum+=i;
i++;
}
System.out.println(sum);
}
}
|
while(条件文){
条件文が真の間繰り返す
}
|
for(文1;条件文;文2){
処理
}
|
文1;
while(条件文){
処理
文2;
}
|
class Js_1_4{
public static void main(String[] args) throws Exception{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));//ここを1行目とする。
int n=Integer.parseInt(bf.readLine());
int sum,i;
for(i=1;i<n;i++){
sum+=i;
}
System.out.println(sum);
}
}
|
import java.io.*;
class Js_1_5{
public static void main(String[] args) throws Exception{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));//ここを1行目とする。
System.out.print("n=?");
int n=Integer.parseInt(bf.readLine());
int sum,i;
for(i=2;i<n;i++){
if(n%i==0){
break;
}
}
if(i==n){
System.out.println("nは素数");
}
}
}
|
Go to Index
| char | 半角1文字を扱う変数の型。1バイトデータ。 |
| byte | 1バイトの整数型。-128〜127 |
| short | 2バイトの整数型。-32768〜32767 |
| int | 4バイトの整数型。-2^31〜2^31-1 |
| long | 8バイトの整数型。-2^63〜2^63-1 |
| float | 4バイトの実数型。小数×10の何乗の形であらわされるので浮動小数点とも言われる。 |
| double | 8バイトの実数型。 |
| boolean | 真偽値を扱う変数の型。値はtrue,falseのいずれか |
while(true){
処理
}
|
import java.io.*;
class Js_1_5{
public static void main(String[] args) throws Exception{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));//ここを1行目とする。
System.out.print("n=?");
int n=Integer.parseInt(bf.readLine());
double a=(double)n;
double b=Math.sqrt(a);
int c=(int)b;
System.out.println("Aの平方根を整数で表すと"+b);
}
}
|
Go to Index
| A+B | AとBの和を計算する演算子 |
| A-B | AからBを引く演算子 |
| A*B | AとBの積を計算する演算子 |
| A/B | AとBが整数型ならAとBの商を計算し、A・Bのいずれかが実数型なら割り算を行う演算子 |
| A%B | AとBの余りを求める演算子 |
| A++ | A=A+1と同じだがこちらの方が速い |
| A-- | A=A-1と同じだがこちらの方が速い |
| A(演算子)=B | A=A(演算子)Bと同じ。例えばA+=3はA=A+3と同じである。 |
| A==B | AとBを比較し、等しければ値はtrueに、そうでなければfalseとなる。(以下わかりやすいようにtrueを真と呼ぶ。以下の表現はA⇒trueだが、すべて¬Aならfalseとなる。) |
| A!=B | AとBが異なれば真 |
| A<B | A小なりBなら真 |
| A>B | A大なりBなら真 |
| A<=B | AがB以下なら真 |
| A>=B | AがB以上なら真 |
| !A | boolean型のAがfalseなら真。その意味でA==falseに同じ。 |
| A && B | boolean型のA,Bが共にtrueなら真 |
| A || B | boolean型A,Bのいずれかがtrueなら真 |
Go to Index
import java.io.*;
class Js_1_6{
public static void main(String[] args) throws Exception{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));//ここを1行目とする。
System.out.print("n=?");
int n=Integer.parseInt(bf.readLine());
int i;
int[] a=new int[n];
a[0]=1;a[1]=1;
for(i=2;i<n;i++)
a[i]=a[i-1]+a[i-2];
for(i=0;i<n;i++)
System.out.println("数列の第"+(i+1)+"項は"+a[i]);
}
}
|
・宣言の仕方 (変数の型)[] 変数名; または (変数の型) 変数名[]; ・配列の大きさの指定の仕方 変数名=new 変数の型[大きさ(int型であること)]; |
(変数の型)[] 変数名=new 変数の型[大きさ(int型であること)]; |
配列変数名.length |
2次元の場合 (変数の型)[][] 変数名=new 変数の型[大きさ][大きさ]; 多次元の場合 (変数の型)[][]… 変数名=new 変数の型[大きさ][大きさ]…; |
Go to Index