REPORT#4
Q23
public class test{
pubic static void main(String atgs[]){
int x,y;
x =100; x+=1; x--; y=200 + x;
System.out.println(y++);
System.out.println(y); //出力結果を追加
}
}
|
実行結果
考察
System.out.plintln(y++)はインクリメント演算子が、yの後ろの方にあるので、出力結果が終わった直後に1が加算される。
|
Q24
public class test{
pubic static void main(String atgs[]){
int a=0, x=0;
a=5; a+=3; x= ++a;
System.out.println(x);
}
}
|
実行結果
考察
出力する前に行っている、x=++aは、aをインクリメントした値をxに代入するのでこのような結果になる。
|
Q25
public class test{
pubic static void main(String atgs[]){
int a=9,b=3;
a/=b;
System.out.println(a%=b);
}
}
|
実行結果
考察
a/=bは a= a/bと同じであるので、aには3が入り、その後xをyでの剰余が出力される。
xはyで割り切れるので出力は0となる。
Q26
public class test{
pubic static void main(String atgs[]){
int i,j;
for(i=0; i<3; i++) ++j;
System.out.println(i*j);
}
}
|
実行結果
考察
ここでは、for文を用いて、iとjを1ずつ足している。
for文を繰り返す回数とjをインクリメントする回数が同じなので両方ともに同じ回数だけ足すことと同じになる。
よって、3*3となり、出力結果が9となる。
|
Q27
public class test{
pubic static void main(String atgs[]){
int i=2;
while(i-- > 0) System.out.print(i);
System.out.println("");
while(--i > 0) System.out.print(i); //出力結果かを追加
}
}
|
実行結果
考察
出力結果から考察すると、テストの計算だと、先にiを比較して、その後に、iをデクリメントすることがわかる。
逆に"--i > 0"とすると、iをデクリメントしてからその値を比較しているため、1までしか出力されない。
|
Q28
public class test{
pubic static void main(String atgs[]){
int num=10000;
for(int i=0; i<4; i++) num>>=1;
System.out.println(num);
System.out.println(10000/16); //出力結果を追加
}
}
|
実行結果
考察
"num>>=1"は、シフト演算でnumを右に1回シフトすることを示す。
つまり、numを2で割ることと同じある。
このfor文は4回繰り返すので2^4つまり、16で割ることと同じである。
|
Q29
public class test{
pubic static void main(String atgs[]){
int num=0;
for(int i=1; i<=10; i++)
if(++num % i == 0) num++;
System.out.println(++num);
}
}
|
実行結果
考察
ここでの、for文は9回繰り返される。また、if文の{}のなかはnumをインクリメントした値とiの剰余が0ならば、
numをインクリメントするプログラムである。ここで、numが2のとき、
iとの剰余が0となるので、numはこのときにもインクリメントされ、
出力する前にnumをインクリメントするので、出力結果が11となる。
|
Q30
public class test{
pubic static void main(String atgs[]){
int a=9;
if(a++ != 10 | ++a ==10) a++;
System.out.println(a);
}
}
|
実行結果
考察
if文の中では、aを10と比較した後にaをインクリメントしている。また、aをインクリメントした後に10と比較する。
この時点で、前者がtrueとなっているので、aをインクリメントする。結果として、aは3回インクリメントを行ったので、
12という値が出力される。
|
Q31
public class test{
pubic static void main(String atgs[]){
for(int i =0; i<5; i++)
System.out.println("i==" +i);
System.out.println("Hello");
}
}
|
実行結果
考察
for文はあるが、{}でくくられてないので、次の一文以外は影響をうけない。よって、1回しか出力されない。
|
Q32
public class test{
pubic static void main(String atgs[]){
int i;
for(i=0; i<9; i +=3){}
System.out.println(i);
}
}
|
実行結果
考察
for文では、iは1回ループで3づつインクリメントされる。 i=9となった時点でループを抜ける。
|
Q33
public class test{
pubic static void main(String atgs[]){
for(int i = 0; i<8; i++){
System.out.println(i);
i += 3;
}
}
|
実行結果
考察
iは0で初期化されているため、始めの出力は0となる。
その後、"i+=3"でiは3となり、i++で4となる、その後また、"i+=3"でiは7となり、
"i++"でi=8となり、if文を抜ける
|
Q34
public class test {
public static void main(String[] args) {
int i = 0;
for( sayHello(); i <= 6; i += 3){
sayHello();
}
}
static void sayHello(){
System.out.println("Hello");
}
}
|
実行結果
考察
ここでの、for文はは3回繰り返される。for文の初期値の部分でsayHelloが呼びされているので、
"Hello"が出力される。よって、for文と繰り返す回数+1回だけ"Hello"が出力される。
|
Q35
public class test {
public static void main(String[] args) {
player p1 = new Player();
player p2 = new Player();
p1.id = 1000;
p2.id = 2000;
p1.num += p1.id;
p2.num += p2.id;
System.outprintln(Player.num);
}
}
class Player{
int id =0;
static int num = 0;
}
|
実行結果
考察
"p1.num += p1.id"でp1.num = 1000となり、"p2.num += p2.id"で1000+2000で3000となる。よって、3000が出力される。つまり、numはstaticなのでp1もp2も同じクラスを示している。
|
Q36
public class test {
public static void main(String[] args) {
player p1 = new Player();
player p2 = new Player();
p1.id = 1000;
p2.id = 2000;
p1.num += p1.id;
p2.num += p2.id;
System.out.println("p1.num ==" + p1.num);
System.out.println("p2.num ==" + p2.num);
}
}
class Player{
int id =0;
static int num = 0;
}
|
実行結果
p1.num == 3000
p2.num ==3000
|
考察
どちらもクラスを共有しているので、出力される値は同じである。
|
Q37
public class test {
public static void main(String argv[]){
Card c1 = new Card();
Card c2 = new Card();
Card c3 = c1;
c1.deposit = 1000;
c2.deposit = 2000;
c3.deposit = 3000;
int sum = c1.deposit + c2.deposit + c3.deposit;
System.out.println(sum);
}
}
class Card{
int deposit;
}
|
実行結果
考察
c1にはCardのオブジェクトが入ってる。"Card c3 = c1"でオブジェクトを共有する。よって、"c1.deposit + c2.deposit + c3.deposit"は"3000+2000+3000"となり、 8000が出力される。
|
Q38
public class test {
public static void main(String argv[]){
Card c1 = new Card();
Card c2 = c1;
c1.deposit = 1000;
c2.deposit = 2000;
Bank.useCard(c1);
Bank.useCard(c2);
}
}
class Card{int deposit;}
class Bank{
public static void useCard(Card c){
c.deposit -= 500;
}
|
実行結果
考察
c1,c2は共有しているので、どちらとも2000が入っている。
ということは、"Bank.useCard(c1)"で、2000-500で1500という値になっている。
さらに"Bank.useCard(c2)" で500引かれているので、出力結果が両方とも1000となる。
|
感想
雨が夜更け過ぎに雪に変わる事は決して無い。
クリスマスの奇跡何て起きない。
今日は別にいつもと何の変わりもない月曜日である。
|