TraceTable, For, Array, Break, Boolean, Logical Expression in if-Statement

ConversionTable

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8a%e5%8d%8811-34-40

Initially, I forgot to add i!=1 in the if statement. Therefore, the program will output a “\n” when i=1. Drawing trace table can help me to find out this kind of problem in the future.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8a%e5%8d%8811-34-57

PrimeNumber

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-12-%e4%b8%8b%e5%8d%881-46-56

Attention HERE! There are two new methods involved: Break Statement & Boolean.

The break statement has two usages: inside loop or inside switch.

When the break statement is encountered inside a loop, the loop is immediately terminated and the program will go to the next statement following the loop. In this case, if (i%j==0), break statement will be invoked. Then it will directly terminate the second forLoop statement for(int j=2;j<i;j++){}.

Boolean only has two possible value: true or false. While I was programming, a simple “=” completely altered my result. You can see that the sentence boolean prime=false has one “=” while if(prime==true){} has “==”(two equal marks).

Why? That is because what inside if statement is logical expression, we should always write like if(x==0). However, boolean prime=false is an declaration and assignment statement while only requires one “=”.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-12-%e4%b8%8b%e5%8d%881-46-49

Mutiplesof3

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8a%e5%8d%8811-59-39%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8a%e5%8d%8811-59-03

Stars

屏幕快照 2016-12-11 下午12.30.12.png

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8b%e5%8d%8812-30-39

GradeAverage

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8b%e5%8d%8812-43-42%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8b%e5%8d%8812-43-48

I created an int Array called anArray which contained num index numbers. Then ask the user to enter the value of each index within Array anArray. sum+=anArray[i]; means sum=sum+anArray[i].

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8b%e5%8d%8812-43-01

Trace Table

屏幕快照 2016-12-11 下午12.50.26.png

IMG_2486.JPG


Personal Reflection: I feel that JAVA is getting more and more difficult now. It always take time to write a working program since there are always errors involved which are difficult for you to figure out. Debugger is a very useful tool to trace your errors. Try to use it!

留下评论