課題内容
Report#4:オフライン試験のQ23〜Q38のついて考察せよ。{〜12/25(Mon)}
* コードと実行結果を示し、各問について考察すること。
Q23
public class q23{
public static void main(String args[]){
int x, y; //int型で変数x,yを宣言
x = 10 //xに100を代入
x += 1; //xにに1加える)
x--; //xから1引く
y = 200 + x; //yに200 + xを代入
する
System.out.println(y++); //yを表示するが、yの後
に++があるため表示した後にyを1増やす
}
}
出力結果
300
Q24
public class q24{
public static void main(String args[]){
int a =0, x=0; //int型で変数aとxを宣言し、0で初期化
a = 5; //aに5を代入し
a +=3; //aに3を加える
x = ++a; //xにaに1加えた値を代入
System.out.println(x); //xを表示する
}
}
実行結果
9
Q25
public class q25{
public static void main(String args[]){
int a=9, b=3; //int型でaとbを宣言しaは9でbは3で初期化
a /= b; //aにa/bを代入(a/bは3)
System.out.println(a %= b); //aにa%bの値を代入し、aの値を表示
}
}
実行結果
0
Q26
public class q26{
public static void main(String args[]){
int i,j; //int型でi、jを宣言
for(i=0, j=0; i<3; i++) ++j; //i,jを0で初期化してiが3未満の間iを1増やし
その間jもデクリメントする
System.out.println(i * j); //iとjをかけたもの
を表示
}
}
実行結果
9
Q27
public class q27{
public static void main(String args[]){
int i=2; //int型でiを宣言し2で初期化
while(i-- > 0) //i>0の間デクリメントされる。
iはwhile文の中で比較された後にデクリメントする
System.out.print(i); //iを表示
実行結果
10
Q28
public class q28{
public static void main(String args[]){
int num=10000; //int型でnumを宣言し、10000で初期化
for(int i=0; i < 4; i++) num >>= i; //int型でiを宣言し0で初期化。i<4の間ループし
iはインクリメント、numは左にiビットシフトする
System.out.println(num); //iを表示
実行結果
156
Q29
public class q29{
public static void main(String args[]){
int num = 0; //int型でnumを宣言し0で初期化
for(int i = 1; i<=10; i++){ //int型でiを宣言し1で初期化。
i<=の間ループしiをインクリメントする
if(++num % i == 0) num++; //numに1加えた値をiで割った余りが0ならばnumに1加える
}
System.out.println(++num); //numに1加えて表示
}
}
実行結果
12
Q30
public class q30{
public static void main(String args[]){
int a = 9; //int型aを宣言し9で初期化
if(a++ != 10 | a++ == 10) a++; //まずaと10が違う比較し、次にaに1
加えてaと10が同じか比べる。後に1加え、この2つを比較したものをorでくくったものがtrueなら加える。
System.out.println(a);
}
}
実行結果
12
Q31
public class q30{
public static void main(String args[]){
int a = 9; //int型aを宣言し9で初期化
if(a++ != 10 | a++ == 10) a++; //まずaと10が違うか比較し、次にaに1
加えてaと10が同じか比べる。後に1加え、
この2つを比較したものをorでくくったものがtrueなら加える。
System.out.println(a);
}
}
実行結果
i == 0
i == 1
i == 2
I == 3
i == 4
Hello
Q32
public class q32{
public static void main(String args[]){
int i; //int型iを宣言
for(i = 0; i < 9;i += 3){} //iを0で宣言しiが9未満なれば処理した後にiに3加える
System.out.println(i)
//iを表示
}
}
実行結果
9
Q33
public class q33{
public static void main(String args[]){
for( int i=0; i < 8; i ++){ //int型でiを宣言し0で初期化。
iが8未満なら処理を行なった後iをインクリメント
System.out.println(i); //iを表示
i += 3; //iに3加える
}
}
実行結果
0
4
Q34
class q34{
public static void main(String args[]){
int i = 0; //int型iを宣言し0で初期化
for( sayHello(); i <= 6; i += 3){ //iが6以下ならばループし処理が終わったら3を足す
sayHello(); //sayHello()を呼び出す
}
}
static void sayHello(){ //static関数の定義
System.out.println("Hello"); //"Hello"を表示
}
}
実行結果
Hello
Hello
Hello
Hello
Q35
class q35{
public static void main(String args[]){
Player p1 = new Player ();
Player p2 = new Player ();
p1.id = 1000; //p1.idに1000を代入
p2.id = 2000; //p2.idに2000を代入
p1.num += p1.id; //p1.numにp1.idを代入
p2.num += p1.id; //p2.numにp2.idを代入
System.out.println(Player.num); //player.numを表示
}
}
class Player{
int id =0; //int型idを0で初期化
static int num =0; //static関数int型numを0で初期化
}
実行結果
3000
Q36
class Test{
public static void main(String args[]){
Player p1 =new Player();
Player p2 =new Player();
p1.id = 1000; //p1.idに1000を代入
p2.id = 2000; //p2.isに2000を代入
Player.num += p1.id; //Player.numにp1.idの値を加える
Player.num += p2.id; //Player.numにp2.idの値を加える
System.out.println("p1.num == "+p1.num); //"p1.num == "+p1.numを表示
System.out.println("p2.num == "+p2.num); //"p2.num == "+p2.numを表示
}
}
class Player{
int id = 0; //int型idを定義し0で初期化
static int num = 0; //static変数でint型numを定義し0で初期化
}
実行結果
p1.num == 3000
p2.num == 3000
Q37
class Test{
public static void main(String args[]){
Card c1 = new Card(); //コンストラクタ(引数無し)を使って初期化
Card c2 = cew Card(); //コンストラクタ(引数無し)を使って初期化
Card c3 = c1; //c3にc1を代入
c1.deposit = 1000; //c1に1000を代入
c2.deposit = 2000; //c2に2000を代入
c3.deposit = 3000; //c3に3000を代入
int sum = c1.deposit + c2.deposit + c3.deposit; // sumにc1,c2,c3を足した値を代入
System.out.println(sum); //sum(上で計算した値)を表示
}
}
class Card{
int deposit; //ローカル変数depositの宣言
}
実行結果
8000
Q38
class Test2{
public static void main(String args[]){
Card c1 =new Card(); //コンストラクタ(引数無し)を使って初期化
Card c2 = c1;
c1.deposit =1000; //c1に1000を代入
c2.deposit =2000; //c2に2000を代入
Bank.useCard( c1 ); //Bank.useCard()を呼び出しc1を計算する
Bank.useCard( c2 ); //Bank.useCard()を呼び出しc2を計算する
/*c1.depositとc2.depositの関係は?*/ //コメント
System.out.println("c1.deposit==" +c1.deposit); //"c1.deposit=="とc1の値を表示
System.out.println("c2.deposit==" +c2.deposit); //"c2.deposit=="とc2の値を表示
}
}
class Card{ int deposit; }
class Bank{
public static void useCard( Card c){
c.deposit -= 500; // c.depositから500引く
}
}
実行結果
c1.deposit==1000
c2.deposit==1000