{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter 04 - Conditional Execution

Chapter 04 - Conditional Execution -...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU &RQGLWLRQDO ([HFXWLRQ &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS 7H[W 3URFHVVLQJ 7KH FKDU 7\SH 6\VWHP RXW SULQWI 0HWKRGV ZLWK &RQGLWLRQDO ([HFXWLRQ 3UHFRQGLWLRQV DQG 3RVWFRQGLWLRQV 7KURZLQJ ([FHSWLRQV 5HYLVLWLQJ 5HWXUQ 9DOXHV &DVH 6WXG\ %RG\ 0DVV ,QGH[ %0, 2QH SHUVRQ 8QVWUXFWXUHG 6ROXWLRQ 7ZR SHUVRQ 8QVWUXFWXUHG 6ROXWLRQ 7ZR SHUVRQ 6WUXFWXUHG 6ROXWLRQ /RRS 7HFKQLTXHV &XPXODWLYH 6XP )HQFHSRVW /RRSV DND ORRS DQG D KDOI LI HOVH 6WDWHPHQWV 5HODWLRQDO 2SHUDWRUV &XPXODWLYH 6XP ZLWK LI )HQFHSRVW ZLWK LI 1HVWHG LI HOVH 6XEWOHWLHV RI &RQGLWLRQDO ([HFXWLRQ 2EMHFW (TXDOLW\ 5RXQGRII (UURUV )DFWRULQJ LI HOVH 6WDWHPHQWV 0LQ 0D[ /RRSV ,QWURGXFWLRQ ,Q WKH ODVW IHZ FKDSWHUV ZH KDYH VHHQ KRZ WR VROYH FRPSOH[ SURJUDPPLQJ WDVNV XVLQJ IRU ORRSV WR UHSHDW FHUWDLQ WDVNV PDQ\ WLPHV :H KDYH EHHQ DEOH WR LQWURGXFH VRPH IOH[LELOLW\ LQWR RXU SURJUDPV WKURXJK WKH XVH RI FODVV FRQVWDQWV DQG WKH DELOLW\ WR UHDG YDOXHV IURP WKH XVHU ZLWK D 6FDQQHU REMHFW 1RZ ZH DUH JRLQJ WR H[SORUH D PXFK PRUH SRZHUIXO WHFKQLTXH IRU ZULWLQJ FRGH WKDW FDQ DGDSW WR GLIIHUHQW VLWXDWLRQV :H DUH JRLQJ WR VWXG\ FRQGLWLRQDO H[HFXWLRQ LQ WKH IRUP RI D FRQWURO VWUXFWXUH NQRZQ DV WKH LI HOVH :LWK LI HOVH VWDWHPHQWV ZH ZLOO EH DEOH WR LQVWUXFW WKH FRPSXWHU WR H[HFXWH GLIIHUHQW OLQHV RI FRGH GHSHQGLQJ XSRQ ZKHWKHU FHUWDLQ FRQGLWLRQV DUH WUXH 7KH LI HOVH VWDWHPHQW OLNH WKH IRU ORRS LV VR SRZHUIXO WKDW \RX ZLOO ZRQGHU KRZ \RX PDQDJHG WR ZULWH SURJUDPV ZLWKRXW LW :H ZLOO DOVR EH H[SDQGLQJ RXU XQGHUVWDQGLQJ RI FRPPRQ SURJUDPPLQJ VLWXDWLRQV 7KH FKDSWHU EHJLQV ZLWK DQ H[SORUDWLRQ RI ORRS WHFKQLTXHV ZH KDYHQ W \HW H[SORUHG DQG LQFOXGHV DQ H[SORUDWLRQ RI WH[W SURFHVVLQJ LVVXHV :H ZLOO DOVR ILQG WKDW DGGLQJ FRQGLWLRQDO H[HFXWLRQ WR RXU UHSHUWRLUH ZLOO UHTXLUH XV WR UHYLVLW WKH LVVXH RI PHWKRGV SDUDPHWHUV DQG UHWXUQ YDOXHV VR WKDW ZH FDQ EHWWHU XQGHUVWDQG VRPH RI WKH ILQH SRLQWV 213 /RRS 7HFKQLTXHV 7KH PRUH \RX SURJUDP WKH PRUH \RX ZLOO ILQG WKDW FHUWDLQ SDWWHUQV HPHUJH %HIRUH ZH GHOYH LQWR FRQGLWLRQDO H[HFXWLRQ ZH DUH JRLQJ WR ORRN DW WZR FRPPRQ ORRS SDWWHUQV WKDW FRPH XS RIWHQ LQ SURJUDPPLQJ FXPXODWLYH VXP DQG IHQFHSRVW ORRSV &XPXODWLYH 6XP :H RIWHQ ZDQW WR ILQG WKH VXP RI D VHULHV RI QXPEHUV <RX FRXOG LPDJLQH GHFODULQJ D GLIIHUHQW YDULDEOH IRU HDFK YDOXH ZH ZDQW WR LQFOXGH EXW WKDW ZRXOG QRW EH SUDFWLFDO ,I \RX KDYH WR DGG RQH KXQGUHG QXPEHUV WRJHWKHU \RX GRQ W ZDQW WR KDYH WR GHFODUH RQH KXQGUHG GLIIHUHQW YDULDEOHV )RUWXQDWHO\ WKHUH LV D VLPSOHU ZD\ 7KH WULFN LV WR NHHS D UXQQLQJ WDOO\ DQG WR SURFHVV RQH QXPEHU DW D WLPH ,I \RX KDYH D YDULDEOH FDOOHG VXP IRU H[DPSOH \RX ZRXOG DGG LQ WKH QH[W QXPEHU E\ VD\LQJ sum = sum + next; RU XVLQJ WKH VKRUWKDQG DVVLJQPHQW RSHUDWRU sum += next; 7KLV VWDWHPHQW VD\V WR WDNH WKH ROG YDOXH RI VXP DGG WKH YDOXH RI D YDULDEOH FDOOHG QH[W DQG VWRUH WKLV DV WKH QHZ YDOXH RI VXP 7KLV RSHUDWLRQ LV SHUIRUPHG IRU HDFK QXPEHU WR EH VXPPHG 7KHUH LV D VOLJKW SUREOHP ZKHQ H[HFXWLQJ WKLV VWDWHPHQW IRU WKH ILUVW QXPEHU EHFDXVH WKHUH LV QR ROG YDOXH RI VXP WKH ILUVW WLPH DURXQG 7R JHW DURXQG WKLV \RX LQLWLDOL]H VXP WR D YDOXH WKDW ZLOO QRW DIIHFW WKH DQVZHU ]HUR +HUH LV D SVHXGRFRGH GHVFULSWLRQ RI WKH FXPXODWLYH VXP DOJRULWKP sum = 0. for (all numbers to sum) { obtain "next". sum += next. } 7R LPSOHPHQW WKLV DOJRULWKP \RX PXVW GHFLGH KRZ PDQ\ WLPHV WR JR WKURXJK WKH ORRS DQG KRZ WR REWDLQ D QH[W YDOXH +HUH LV DQ LQWHUDFWLYH SURJUDP WKDW SURPSWV WKH XVHU IRU KRZ PDQ\ QXPEHUV WR VXP WRJHWKHU DQG IRU WKH QXPEHUV WKHPVHOYHV 214 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Finds the sum of a sequence of numbers. import java.util.*; public class ExamineNumbers1 { public static void main(String args) { System.out.println("This program adds a sequence of numbers."); System.out.println(); Scanner console = new Scanner(System.in); System.out.print("How many numbers do you want me to examine? "); int totalNumber = console.nextInt(); double sum = 0.0; for (int i = 1; i <= totalNumber; i++) { System.out.print(" #" + i + "? "); double next = console.nextDouble(); sum += next; } System.out.println(); System.out.println("sum = " + sum); } } 7KH SURJUDP ZLOO H[HFXWH VRPHWKLQJ OLNH WKLV This program adds a sequence of numbers. How many numbers do you want me to examine? 6 #1? 3.2 #2? 4.7 #3? 5.1 #4? 9.0 #5? 2.4 #6? 3.1 sum = 27.5 /HW V WUDFH WKH H[HFXWLRQ LQ GHWDLO %HIRUH ZH HQWHU WKH IRU ORRS ZH LQLWLDOL]H WKH YDULDEOH VXP WR EH +------+ sum | 0.0 | +------+ 2Q WKH ILUVW H[HFXWLRQ RI WKH IRU ORRS ZH UHDG LQ D YDOXH RI WKH VXP +------+ sum | 3.2 | +------+ +-----+ next | 3.2 | +-----+ IURP WKH XVHU DQG DGG WKLV YDOXH WR 7KH VHFRQG WLPH WKURXJK WKH ORRS ZH UHDG LQ D YDOXH RI DQG DGG WKLV LQWR RXU VXP 215 +------+ sum | 7.9 | +------+ +-----+ next | 4.7 | +-----+ 1RWLFH WKDW WKH VXP QRW LQFOXGHV ERWK RI WKH QXPEHUV HQWHUHG E\ WKH XVHU EHFDXVH ZH KDYH DGGHG WKH QHZ YDOXH WR WKH ROG YDOXH RI 7KH WKLUG WLPH WKURXJK WKH ORRS ZH DGG LQ WKH YDOXH +------+ sum | 13.0 | +------+ +-----+ next | 5.1 | +-----+ 1RWLFH WKDW WKH YDULDEOH VXP QRZ FRQWDLQV WKH VXP RI WKH ILUVW WKUHH QXPEHUV ZH UHDG LQ DQG DGG LW WR WKH VXP +------+ sum | 22.0 | +------+ +-----+ next | 9.0 | +-----+ 7KHQ 7KHQ ZH DGG LQ WKH ILIWK YDOXH RI +------+ sum | 24.4 | +------+ +-----+ next | 2.4 | +-----+ $QG ILQDOO\ DGG LQ WKH VL[WK YDOXH RI +------+ sum | 27.5 | +------+ +-----+ next | 3.1 | +-----+ :H WKHQ H[LW WKH IRU ORRS DQG SULQW WKH YDOXH RI VXP 7KHUH LV DQ LQWHUHVWLQJ VFRSH LVVXH LQ WKLV SDUWLFXODU SURJUDP 1RWLFH WKDW WKH YDULDEOH VXP LV GHFODUHG RXWVLGH WKH ORRS ZKLOH WKH YDULDEOH QH[W LV GHFODUHG LQVLGH WKH ORRS :H KDYH QR FKRLFH EXW WR GHFODUH VXP RXWVLGH WKH ORRS EHFDXVH LW QHHGV WR EH LQLWLDOL]HG DQG LW LV XVHG DIWHU WKH ORRS %XW WKH YDULDEOH QH[W LV XVHG RQO\ LQVLGH WKH ORRS VR LW FDQ EH GHFODUHG LQ WKDW LQQHU VFRSH ,W LV EHVW WR GHFODUH YDULDEOHV LQ WKH LQQHUPRVW VFRSH SRVVLEOH 7KH FXPXODWLYH VXP DOJRULWKP DQG YDULDWLRQV RQ LW ZLOO EH XVHIXO LQ PDQ\ RI WKH SURJUDPPLQJ WDVNV \RX VROYH +RZ ZRXOG \RX GR D FXPXODWLYH SURGXFW IRU H[DPSOH" +HUH LV WKH SVHXGRFRGH product = 1. for (all numbers to multiply) { obtain "next". product *= next. } )HQFHSRVW /RRSV DND ORRS DQG D KDOI $QRWKHU FRPPRQ SURJUDPPLQJ SUREOHP LQYROYHV D SDUWLFXODU NLQG RI ORRS NQRZQ DV D IHQFHSRVW ORRS &RQVLGHU WKH IROORZLQJ SUREOHP <RX ZDQW WR SXW XS D IHQFH WKDW LV \DUGV ORQJ DQG \RX ZDQW WR KDYH D SRVW HYHU\ \DUGV +RZ PDQ\ SRVWV GR \RX QHHG" ,I \RX GR D TXLFN GLYLVLRQ LQ \RXU KHDG \RX PLJKW VD\ WKDW \RX QHHG SRVWV EXW DFWXDOO\ \RX QHHG SRVWV 7KDW V EHFDXVH IHQFHV EHJLQ DQG HQG ZLWK SRVWV ,Q RWKHU ZRUGV WKH IHQFH ORRNV OLNH WKLV 216 %HFDXVH \RX ZDQW SRVWV RQ ERWK WKH IDU OHIW DQG WKH IDU ULJKW \RX FDQ W XVH WKH IROORZLQJ VLPSOH ORRS EHFDXVH LW GRHVQ W SODQW WKH ILQDO SRVW for (the length of the fence) { plant a post. attach some wire. } ,I \RX XVH WKH SUHFHGLQJ ORRS \RX OO JHW D IHQFH WKDW ORRNV OLNH WKLV 6ZLWFKLQJ WKH RUGHU RI WKH WZR RSHUDWLRQV GRHVQ W KHOS EHFDXVH \RX PLVV WKH ILUVW SRVW 7KH SUREOHP ZLWK WKLV ORRS LV WKDW LW SURGXFHV WKH VDPH QXPEHU RI SRVWV DV VHFWLRQV RI ZLUH EXW ZH NQRZ ZH QHHG DQ H[WUD SRVW 7KDW V ZK\ WKLV SUREOHP LV DOVR VRPHWLPHV UHIHUUHG WR DV WKH ORRS DQG D KDOI SUREOHP EHFDXVH ZH ZDQW WR H[HFXWH RQH KDOI RI WKLV ORRS SODQWLQJ D SRVW RQH H[WUD WLPH 2QH VROXWLRQ LV WR SODQW RQH RI WKH SRVWV HLWKHU EHIRUH RU DIWHU WKH ORRS 7KH XVXDO VROXWLRQ LV WR GR LW EHIRUH plant a post. for (the length of the fence) { attach some wire. plant a post. } 1RWLFH WKDW WKH RUGHU RI WKH WZR RSHUDWLRQV LQ WKH ERG\ RI WKH ORRS LV QRZ UHYHUVHG EHFDXVH WKH LQLWLDO SRVW LV SODQWHG EHIRUH \RX HQWHU WKH ORRS $V D VLPSOH H[DPSOH FRQVLGHU WKH SUREOHP RI ZULWLQJ RXW WKH LQWHJHUV EHWZHHQ E\ FRPPDV ,Q RWKHU ZRUGV ZH ZDQW WR JHW WKLV RXWSXW 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 DQG VHSDUDWHG 7KLV LV D FODVVLF IHQFHSRVW SUREOHP EHFDXVH ZH ZDQW WR ZULWH RXW QXPEHUV EXW RQO\ FRPPDV ,Q RXU IHQFHSRVW WHUPLQRORJ\ ZULWLQJ D QXPEHU LV WKH SRVW SDUW RI WKH WDVN DQG ZULWLQJ D FRPPD LV WKH ZLUH SDUW 6R LPSOHPHQWLQJ WKH SVHXGRFRGH DERYH ZH SULQW WKH ILUVW QXPEHU EHIRUH WKH ORRS 217 System.out.print(1); for (int i = 2; i <= 10; i++) { System.out.print(", " + i); } System.out.println(); LI HOVH 6WDWHPHQWV <RX ZLOO RIWHQ ILQG \RXUVHOI ZULWLQJ FRGH WKDW \RX ZDQW WR H[HFXWH VRPH RI WKH WLPH EXW QRW DOO RI WKH WLPH )RU H[DPSOH LI \RX DUH ZULWLQJ D JDPH SOD\LQJ SURJUDP DQG D QHZ KLJK VFRUH KDV EHHQ UHDFKHG \RX PLJKW ZDQW WR SULQW D PHVVDJH DQG UHPHPEHU WKH QHZ KLJK VFRUH <RX FDQ DFFRPSOLVK WKLV E\ SXWWLQJ WKH WZR OLQHV RI FRGH LQVLGH DQ LI VWDWHPHQW if (currentScore > maxScore) { System.out.println("A new high score!"); maxScore = currentScore; } 7KH LGHD LV WKDW ZH VRPHWLPHV ZDQW WR H[HFXWH WKH WZR OLQHV RI FRGH LQVLGH WKH LI EXW QRW DOZD\V 7KH WHVW LQ SDUHQWKHVHV GHWHUPLQHV ZKHWKHU RU QRW WKH VWDWHPHQWV LQVLGH WKH LI DUH H[HFXWHG ,Q RWKHU ZRUGV WKH WHVW GHVFULEHV WKH FRQGLWLRQV XQGHU ZKLFK ZH ZDQW WR H[HFXWH WKH FRGH 7KH JHQHUDO IRUP RI WKH LI VWDWHPHQW LV DV IROORZV if (<test>) { <statement>; <statement>; ... <statement>; } 7KH LI VWDWHPHQW OLNH WKH IRU ORRS LV D FRQWURO VWUXFWXUH 1RWLFH WKDW ZH RQFH DJDLQ VHH D -DYD NH\ZRUG LI IROORZHG E\ SDUHQWKHVHV DQG D VHW RI FXUO\ EUDFHV HQFORVLQJ D VHULHV RI FRQWUROOHG VWDWHPHQWV 7KH GLDJUDP EHORZ LQGLFDWHV WKH IORZ RI FRQWURO IRU WKH VLPSOH LI VWDWHPHQW 7KH FRPSXWHU SHUIRUPV WKH WHVW DQG LI LW HYDOXDWHV WR WUXH WKH FRPSXWHU H[HFXWHV WKH FRQWUROOHG VWDWHPHQWV ,I WKH WHVW HYDOXDWHV WR IDOVH WKH FRPSXWHU VNLSV WKH FRQWUROOHG VWDWHPHQWV 218 :H XVH WKH VLPSOH LI VWDWHPHQW ZKHQ ZH KDYH FRGH WKDW ZH VRPHWLPHV ZDQW WR H[HFXWH DQG VRPHWLPHV ZDQW WR VNLS -DYD KDV D YDULDWLRQ NQRZQ DV WKH LI HOVH VWDWHPHQW WKDW DOORZV XV WR FKRRVH EHWZHHQ WZR DOWHUQDWLYHV 6XSSRVH IRU H[DPSOH WKDW ZH ZDQW WR VHW D YDULDEOH FDOOHG DQVZHU WR WKH VTXDUH URRW RI D QXPEHU answer = Math.sqrt(number); :H KDYH D SRWHQWLDO SUREOHP LQ WKDW ZH GRQ W ZDQW WR DVN IRU WKH VTXDUH URRW LI WKH QXPEHU LV QHJDWLYH :H FRXOG XVH D VLPSOH LI VWDWHPHQW WR DYRLG WKH SUREOHP if (number >= 0) { answer = Math.sqrt(number); } 7KLV ZLOO DYRLG DVNLQJ IRU WKH VTXDUH URRW RI D QHJDWLYH QXPEHU EXW WKHQ ZKDW YDOXH ZLOO DQVZHU KDYH LI QXPEHU LV QHJDWLYH" 7KLV LV D FDVH ZKHUH ZH ZRXOG SUREDEO\ ZDQW WR JLYH D YDOXH WR DQVZHU HLWKHU ZD\ 6XSSRVH ZH ZDQW DQVZHU WR EH ZKHQ QXPEHU LV QHJDWLYH :H FDQ H[SUHVV WKLV SDLU RI DOWHUQDWLYHV ZLWK DQ LI HOVH VWDWHPHQW if (number >= 0) { answer = Math.sqrt(number); } else { answer = -1; } 7KH LGHD EHKLQG WKH LI HOVH LV WKDW ZH FKRRVH EHWZHHQ WZR DOWHUQDWLYHV DQG H[HFXWH RQH RU WKH RWKHU 6R LQ WKH FRGH DERYH ZH NQRZ WKDW DQVZHU ZLOO EH DVVLJQHG D YDOXH RQH ZD\ RU WKH RWKHU 7KH JHQHUDO IRUP RI WKH LI HOVH VWDWHPHQW LV DV IROORZV 219 if (<test>) { <statement>; <statement>; ... <statement>; } else { <statement>; ... <statement>; <statement>; } 7KLV FRQWURO VWUXFWXUH LV XQXVXDO LQ WKDW LW KDV WZR VHWV RI FRQWUROOHG VWDWHPHQWV DQG WZR GLIIHUHQW NH\ZRUGV LI DQG HOVH 7KH GLDJUDP EHORZ LQGLFDWHV WKH IORZ RI FRQWURO 7KH FRPSXWHU SHUIRUPV WKH WHVW DQG GHSHQGLQJ XSRQ ZKHWKHU LW HYDOXDWHV WR WUXH RU IDOVH WKH FRPSXWHU H[HFXWHV RQH RU WKH RWKHU JURXSV RI VWDWHPHQWV 7KXV WKH LI HOVH FRQWUROV WZR GLIIHUHQW VHWV RI VWDWHPHQWV RQH WR EH H[HFXWHG ZKHQ WKH WHVW HYDOXDWHV WR WUXH DQG WKH RWKHU WR EH H[HFXWHG ZKHQ WKH WHVW HYDOXDWHV WR IDOVH $V ZLWK WKH IRU ORRS LI \RX KDYH D VLQJOH VWDWHPHQW WR H[HFXWH WKHQ \RX GRQ W QHHG WR LQFOXGH WKH FXUO\ EUDFHV %XW WKH 6XQ FRQYHQWLRQ LV WR LQFOXGH WKH FXUO\ EUDFHV HYHQ LI \RX GRQ W QHHG WKHP DQG ZH IROORZ WKDW FRQYHQWLRQ LQ WKLV ERRN 5HODWLRQDO 2SHUDWRUV 7KH LI HOVH VWDWHPHQWV DUH FRQWUROOHG E\ D WHVW 6LPSOH WHVWV FRPSDUH WZR H[SUHVVLRQV WR VHH LI WKH\ DUH UHODWHG LQ VRPH ZD\ 6XFK D WHVW LV LWVHOI DQ H[SUHVVLRQ WKDW UHWXUQV HLWKHU WUXH RU IDOVH DQG LV RI WKH IROORZLQJ IRUP <expression> <relational operator> <expression> 220 7R HYDOXDWH VXFK D WHVW \RX ILUVW HYDOXDWH WKH WZR H[SUHVVLRQV DQG WKHQ VHH LI WKH JLYHQ UHODWLRQ KROGV EHWZHHQ WKH YDOXH RQ WKH OHIW DQG WKH YDOXH RQ WKH ULJKW ,I WKH UHODWLRQ GRHV KROG WKH WHVW HYDOXDWHV WR WUXH ,I QRW WKH WHVW HYDOXDWHV WR IDOVH 7KH UHODWLRQDO RSHUDWRUV DUH 5HODWLRQDO 2SHUDWRUV 2SHUDWRU == != < > <= >= 0HDQLQJ ([DPSOH 9DOXH WUXH WUXH IDOVH WUXH IDOVH WUXH 7KLV LV WR GLVWLQJXLVK 2 + 2 == 4 HTXDOV 3.2 != 4.1 QRW HTXDOV 4 < 3 OHVV WKDQ 4 > 3 JUHDWHU WKDQ 2 <= 0 OHVV WKDQ RU HTXDOV JUHDWHU WKDQ RU HTXDOV 2.4 >= 1.6 1RWLFH WKDW WKH WHVW IRU HTXDOLW\ LQYROYHV WZR HTXDOV FKDUDFWHUV LQ D URZ LW IURP WKH DVVLJQPHQW RSHUDWRU %HFDXVH ZH XVH WKH UHODWLRQDO RSHUDWRUV DV D QHZ ZD\ RI IRUPLQJ H[SUHVVLRQV ZH PXVW UHFRQVLGHU SUHFHGHQFH %HORZ LV DQ XSGDWHG YHUVLRQ RI WKH SUHFHGHQFH WDEOH WKDW LQFOXGHV WKHVH QHZ RSHUDWRUV <RX ZLOO VHH WKDW WHFKQLFDOO\ WKH HTXDOLW\ FRPSDULVRQV DUH FRQVLGHUHG DW D VOLJKWO\ GLIIHUHQW OHYHO RI SUHFHGHQFH WKDQ WKH RWKHU UHODWLRQDO RSHUDWRUV EXW ERWK VHWV RI RSHUDWRUV KDYH D ORZHU SUHFHGHQFH WKDQ WKH DULWKPHWLF RSHUDWRUV -DYD 2SHUDWRU 3UHFHGHQFH 'HVFULSWLRQ 2SHUDWRUV ++, --, +, XQDU\ RSHUDWRUV *, /, % PXOWLSOLFDWLYH RSHUDWRUV +, DGGLWLYH RSHUDWRUV <, >, <=, >= UHODWLRQDO RSHUDWRUV ==, != HTXDOLW\ RSHUDWRUV DVVLJQPHQW RSHUDWRUV =, +=, -=, *=, /=, %= 7KH IROORZLQJ H[SUHVVLRQ LV PDGH XS RI WKH FRQVWDQWV HTXDOV 3 + 2 * 2 == 9 DQG WKH RSHUDWLRQV SOXV WLPHV DQG :KLFK RI WKH RSHUDWLRQV LV SHUIRUPHG ILUVW" %HFDXVH WKH UHODWLRQDO RSHUDWRUV KDYH D ORZHU OHYHO RI SUHFHGHQFH WKDQ WKH DULWKPHWLF RSHUDWRUV WKH DQVZHU LV WKDW WKH WLPHV LV SHUIRUPHG ILUVW WKHQ WKH SOXV WKHQ WKH HTXDOV WHVW ,Q RWKHU ZRUGV -DYD ZLOO SHUIRUP DOO RI WKH PDWK ILUVW EHIRUH LW WHVWV IRU RQH RI WKHVH UHODWLRQVKLSV 7KLV SUHFHGHQFH VFKHPH IUHHV \RX IURP SDUHQWKHVL]LQJ WKH OHIW DQG ULJKW VLGHV RI D WHVW XVLQJ D UHODWLRQDO RSHUDWRU 8VLQJ WKHVH SUHFHGHQFH UXOHV WKH H[SUHVVLRQ DERYH LV HYDOXDWHG DV IROORZV 221 3 + 2 * 2 == 9 \---/ 3 + 4 == 9 \-----/ 7 == 9 \---------/ false <RX FDQ SXW DUELWUDU\ H[SUHVVLRQV RQ HLWKHU VLGH RI WKH UHODWLRQDO RSHUDWRU DV ORQJ DV WKH\ DUH RI D FRPSDWLEOH W\SH +HUH LV D WHVW ZLWK FRPSOH[ H[SUHVVLRQV RQ HLWKHU VLGH (2 - 3 * 8) / (435 % (7 * 2)) <= 3.8 - 4.5 / (2.2 * 3.8) 2QH OLPLWDWLRQ RI WKHVH RSHUDWRUV LV WKDW WKH\ VKRXOG RQO\ EH XVHG ZLWK SULPLWLYH GDWD /DWHU LQ WKLV FKDSWHU ZH ZLOO WDON DERXW KRZ WR FRPSDUH REMHFWV IRU HTXDOLW\ DQG LQ D ODWHU FKDSWHU ZH OO GLVFXVV KRZ WR SHUIRUP OHVV WKDQ DQG JUHDWHU WKDQ FRPSDULVRQV RQ REMHFWV &XPXODWLYH 6XP ZLWK LI /HW V QRZ VHH KRZ WKH XVH RI LI HOVH VWDWHPHQWV FDQ DOORZ XV WR FUHDWH VRPH LQWHUHVWLQJ YDULDWLRQV RQ WKH FXPXODWLYH VXP DOJRULWKP 6XSSRVH WKDW ZH ZDQW WR UHDG D VHTXHQFH RI QXPEHUV DQG FRPSXWH WKH DYHUDJH 7KLV VHHPV OLNH D VWUDLJKWIRUZDUG YDULDWLRQ RI RXU FXPXODWLYH VXP FRGH :H FDQ FRPSXWH WKH DYHUDJH DV WKH VXP GLYLGHG E\ WKH QXPEHU RI QXPEHUV DV LQ System.out.println("average = " + sum / totalNumber); 7KHUH LV RQH PLQRU SUREOHP ZLWK WKLV FRGH 6XSSRVH WKDW ZKHQ ZH DVN WKH XVHU KRZ PDQ\ QXPEHUV WR SURFHVV WKH XVHU VD\V WR SURFHVV QXPEHUV 7KDW ZRXOG PHDQ WKDW ZH QHYHU HQWHU RXU FXPXODWLYH VXP ORRS DQG ZH WU\ WR FRPSXWH WKH YDOXH RI GLYLGHG E\ -DYD ZRXOG SULQW RXW WKDW WKH DYHUDJH LV 1D1 7KLV FU\SWLF PHVVDJH LV VKRUW IRU 1RW D 1XPEHU ,W ZRXOG EH EHWWHU WR SULQW RXW VRPH RWKHU NLQG RI PHVVDJH WKDW ZRXOG LQGLFDWH WKDW WKHUH ZHUHQ W DQ\ QXPEHUV WR DYHUDJH :H FDQ XVH DQ LI HOVH IRU WKLV SXUSRVH if (totalNumber <= 0) { System.out.println("No numbers to average"); } else { System.out.println("average = " + sum / totalNumber); } :H FDQ ILW LQ DQRWKHU XVH RI LI VWDWHPHQWV E\ FRXQWLQJ KRZ PDQ\ QHJDWLYH QXPEHUV DUH HQWHUHG E\ WKH XVHU <RX ZLOO RIWHQ ILQG \RXUVHOI ZDQWLQJ WR FRXQW KRZ PDQ\ WLPHV VRPHWKLQJ RFFXUV LQ D SURJUDP 7KLV JRDO LV HDV\ WR DFFRPSOLVK ZLWK DQ LI VWDWHPHQW DQG DQ LQWHJHU YDULDEOH FDOOHG D FRXQWHU <RX VWDUW E\ LQLWLDOL]LQJ WKH FRXQWHU WR int negatives = 0; <RX FDQ XVH DQ\ QDPH \RX ZDQW IRU WKH YDULDEOH +HUH ZH XVHG WKH QDPH QHJDWLYHV EHFDXVH WKDW LV ZKDW ZH UH FRXQWLQJ 7KH RWKHU HVVHQWLDO LQJUHGLHQW LV WR LQFUHPHQW WKH FRXQWHU LQVLGH WKH ORRS LI LW SDVVHV WKH WHVW ZH UH LQWHUHVWHG LQ 222 if (next < 0) { negatives++; } 3XWWLQJ WKLV DOO WRJHWKHU DQG PRGLI\LQJ WKH FRPPHQWV DQG LQWURGXFWLRQ ZH HQG XS ZLWK WKH IROORZLQJ YDULDWLRQ RI WKH FXPXODWLYH VXP SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 // Finds the average of a sequence of numbers as well as reporting // how many of the numbers were negative. import java.util.*; public class ExamineNumbers2 { public static void main(String args) { System.out.println("This program examines a sequence of numbers to"); System.out.println("find the average as well as counting how many"); System.out.println("are negative."); System.out.println(); Scanner console = new Scanner(System.in); System.out.print("How many numbers do you want me to examine? "); int totalNumber = console.nextInt(); int negatives = 0; double sum = 0.0; for (int i = 1; i <= totalNumber; i++) { System.out.print(" #" + i + "? "); double next = console.nextDouble(); sum += next; if (next < 0) { negatives++; } } System.out.println(); if (totalNumber <= 0) { System.out.println("No numbers to average"); } else { System.out.println("average = " + sum / totalNumber); } System.out.println("# of negatives = " + negatives); } } 7KH SURJUDP ZLOO H[HFXWH VRPHWKLQJ OLNH WKLV 223 This program examines a sequence of numbers to find the average as well as counting how many are negative. How many numbers do you want me to examine? 8 #1? 2.5 #2? 9.2 #3? -19.4 #4? 208.2 #5? 42.3 #6? 92.7 #7? -17.4 #8? 8 average = 40.7625 # of negatives = 2 )HQFHSRVW ZLWK LI 0DQ\ RI WKH IHQFHSRVW ORRSV WKDW \RX ZULWH ZLOO UHTXLUH FRQGLWLRQDO H[HFXWLRQ ,Q IDFW WKH IHQFHSRVW SUREOHP LWVHOI FDQ EH VROYHG ZLWK DQ LI VWDWHPHQW 5HPHPEHU WKDW WKH FODVVLF VROXWLRQ WR WKH IHQFHSRVW LV WR KDQGOH WKH ILUVW SRVW EHIRUH WKH ORRS EHJLQV plant a post. for (the length of the fence) { attach some wire. plant a post. } 7KLV VROXWLRQ VROYHV WKH SUREOHP EXW LW FDQ EH FRQIXVLQJ EHFDXVH LQVLGH WKH ORRS ZH GR WKLQJV LQ UHYHUVH RUGHU :LWK DQ LI VWDWHPHQW ZH FDQ NHHS WKH RULJLQDO RUGHU RI WKH VWHSV for (the length of the fence) { plant a post. if (this isn't the last post) { attach some wire. } } 7KLV YDULDWLRQ LVQ W XVHG DV RIWHQ DV WKH FODVVLF VROXWLRQ EHFDXVH LW LQYROYHV D GRXEOH WHVW :H KDYH D ORRS WHVW DQG ZH KDYH D WHVW LQVLGH WKH ORRS 2IWHQ WKHVH DUH QHDUO\ WKH VDPH WHVW VR LW LV LQHIILFLHQW WR WHVW WKH VDPH WKLQJ WZLFH HDFK WLPH WKURXJK WKH ORRS %XW WKHUH ZLOO EH VLWXDWLRQV ZKHUH \RX PLJKW XVH WKLV DSSURDFK )RU H[DPSOH LQ WKH FODVVLF DSSURDFK ZH UHSHDW ZKDWHYHU OLQHV RI FRGH FRUUHVSRQG WR SODQWLQJ D SRVW ,I WKHUH LV D ORW RI FRGH LQYROYHG ZH PLJKW GHFLGH WKDW WKH LI LQVLGH WKH ORRS LV D EHWWHU DSSURDFK HYHQ LI LW OHDGV WR VRPH H[WUD WHVWLQJ $V DQ H[DPSOH OHW V FRQVLGHU ZULWLQJ D PHWKRG FDOOHG PXOWLSULQW WKDW ZLOO SULQW D VWULQJ D SDUWLFXODU QXPEHU RI WLPHV 6XSSRVH WKDW ZH ZDQW WKH RXWSXW RQ D OLQH E\ LWVHOI LQVLGH VTXDUH EUDFNHWV DQG VHSDUDWHG E\ FRPPDV %HORZ DUH WZR H[DPSOH FDOOV multiprint("please", 4); multiprint("beetlejuice", 3); 224 :H H[SHFW WKHVH FDOOV WR SURGXFH WKH IROORZLQJ RXWSXW [please, please, please, please] [beetlejuice, beetlejuice, beetlejuice] ,I ZH GRQ W WKLQN DERXW WKH IHQFHSRVW ZH FDQ ZULWH D VLPSOH ORRS WKDW SULQWV VTXDUH EUDFNHWV RXWVLGH WKH ORRS DQG WKDW SULQWV WKH VWULQJ ZLWK D FRPPD LQVLGH WKH ORRS public static void multiprint(String s, int times) { System.out.print("["); for (int i = 1; i <= times; i++) { System.out.print(s + ", "); } System.out.println("]"); } 8QIRUWXQDWHO\ WKLV FRGH SURGXFHV DQ H[WUDQHRXV FRPPD DIWHU WKH ODVW YDOXH [please, please, please, please, ] [beetlejuice, beetl...
View Full Document

{[ snackBarMessage ]}