Methods, String, DoWhile, For, Array

StringDemo

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-08-%e4%b8%8b%e5%8d%889-53-57

String class is used to store text. System.out.prinln(“\nThe length of “+greeting1+” is “+greeting1.length()); is used to output the length of the text stored in String greeting1.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-08-%e4%b8%8b%e5%8d%889-54-09

There is one other way to achieve the same purpose: int len=greeting2/length(); System.out.println(len).If we want to convert all letters in the String to uppercase, we write: System.out.println(greeting2Upper). 

int comma = invertedName.indexof(‘,’) is used to find the first comma “,” in the inverted name (Lincoln, Abraham) and record its index.

String lastname = invertedName.substring(0,comma) is used to output all letters from index 0 to comma (index 6) in invertedName.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-08-%e4%b8%8b%e5%8d%889-52-17

屏幕快照 2016-12-08 下午10.00.53.png

ForDemo

屏幕快照 2016-12-11 上午10.52.08.png

屏幕快照 2016-12-11 上午10.53.46.png

DoWhileDemo

屏幕快照 2016-12-11 上午10.56.36.png

The above DoWhileDemo and ForDemo have the same result. We use For statement when we know how many time we want to repeat while we use DoWhile statement when we do NOT know.

EnhancedForLoop

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8a%e5%8d%8810-59-36%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8a%e5%8d%8810-59-47

ArrayDemo

屏幕快照 2016-12-08 下午10.13.33.png

(line 17) String[] staffNames = new String[staff]  I created an String array called staffNames which has a length of the number of staff that I inputed. I added an for loop. In the array staffNames, when the index is 0 which is less than staff number, I input the first staff’s name, then i++. Then i=1, i=2…

System.out.println(staffNames[i]) asked the program to output all the String stored in array staffNames.

屏幕快照 2016-12-08 下午10.17.51.png

CityNames

屏幕快照 2016-12-11 上午11.05.12.png

if(option.chatAt(0) == ‘D’ || option.chatAt(0) == ‘d’) 0 represents the zeroth index number, if any text in Array cityNames have a ‘D’ or ‘d’ in the zeroth index number, it will be output. chatAt() is a method.

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

MaxandMin

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8a%e5%8d%8811-10-48%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8a%e5%8d%8811-10-56%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-11-%e4%b8%8a%e5%8d%8811-10-36

IMG_2485.JPG

Trace Table

 

留下评论