java程序設(shè)計課程實驗報告材料實驗09_第1頁
java程序設(shè)計課程實驗報告材料實驗09_第2頁
java程序設(shè)計課程實驗報告材料實驗09_第3頁
java程序設(shè)計課程實驗報告材料實驗09_第4頁
java程序設(shè)計課程實驗報告材料實驗09_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、實用標準文案JavaJava開發(fā)技術(shù)實驗報告實驗序號:實驗09實驗項目名稱:條件和循環(huán)語句(二)學號姓名專業(yè)、班實驗地實1-316指導教實驗時間2012-點師11-7實驗目的及要求 、?拳握條件語句switch的使用方法?拳握循環(huán)語句for的使用方法二、實驗設(shè)備(環(huán)境)及要求PC 機,windows xp ,軟件環(huán)境(jdkl.6 , tomcat web 服務器 f Eclipse )?硬件要求:CPU PII以上,64M內(nèi)存,100M硬盤空間。?軟件要求:Windows98/Me/XP/NT/2000 , IE 5 以上。?開發(fā)環(huán)境:JDK1.6.0_10三、實驗內(nèi)容與步驟Rock.Roc

2、k. Paper,Paper, ScissorsScissors程序Rock.java是石頭剪子布游戲的一個框架。將該程序保存至本地目錄,按照提示補充程序語 句。該程序允許用戶輸入一個項目,計算機隨機產(chǎn)生一個項目,對兩個項目對比,給出勝負結(jié)果。 用戶可以輸入R, R S或者r, p, s來表示石頭,布,剪刀三個項目。用戶輸入的項目保存在字符串 變呈中,以便于大小寫的轉(zhuǎn)換。使用一個switch語句將一個隨機整數(shù)轉(zhuǎn)換為游戲中計算機給出 的項目。源代碼如下:將彩文檔.實用標準文案import java.util.Random;import java.util.Scanner;public class

3、 Rock public static void main(String args)/Users play R,RorSString pers on Play;/Computers play RzBorS String computerPlay = null; tonumberusedint computerlnt;/Randomlygenerateddetermine/computers playSeanner scan = new Scanner(System.in);Random generator = new Random();System.out.println(Enter your

4、 play: R; P or S);String pl = sean.next();pers on Play = pl.tollpperCase(); computed nt = gen erator.nextl nt(3);switch(computerlnt)case 0 :computerPlay = R;break;case 1:computerPlay = P;break;精彩文檔.實用標準文案case 2 :computerPlay = S;break;default:break;System.out.println(Computer play is + computerPlay)

5、;if (personPlay.equals(computerPlay)System.out.println(Its a tie!);else if(personPlay.equals(R)& computerPlay.equals(S) System.out.println(Rock crushes scissors. You win!);else if(personPlay.equals(R)& computerPlay.equals(P)System.out.println(Rock crushes paper. You win!);else if(personPlay.equals(S

6、)& computerPlay.equals(R)System.out.println(scissors crushes Rock. You win!);else if(personPlay.equals(S) & computerPlay.equals(P)System.out.println(scissors crushes paper. You lose!);&elseif(pers on Play.equals(R) computerPlay.equals(S) Youcrushesscissors System.out.println(Rocklose!);&if(pers on P

7、lay.equals(P)else精彩文檔.實用標準文案computerPlay.equals(R)Youcrushes RockSystem.out.println(paperloser!);UsingUsing thethe CoinCoin ClassClass程序,將其拷貝至本地目錄,編寫一段程序,連續(xù)投擲的Coin.java參考教材中例5.4所示。Runs.javaHEAD的投擲次數(shù)的最大值。程序框架如100硬幣次,查找并記錄連續(xù)為具體步驟 如下:對象;創(chuàng)建一個coinl.方法查看結(jié)果是否為HEADSflip方法投擲硬幣,使用getFace在循環(huán)語句中,2.使用的次 數(shù),并記錄其中的

8、最大值。記錄當前連續(xù)投擲結(jié)果為HEADS在循環(huán)結(jié)束后打印最大值結(jié)果。3.CoinjavaCoinjava的源代碼如下 :public class Coin public final int HEADS = 0;public final int TATLS = 1;private int face;/sets up the coin by flipping it initially.public Coin()精彩文檔.實用標準文案flip();/Flips the coin by randomly choosing a facepublic void flipOface = (in t)(Mat

9、h.randomO*2);/Return true if the current face of the coin is heads public boolean isHeadsOreturn (face = HEADS);/Returns the current face of the coin as a string public String toString()String faceName;if(face = HEADS)faceName = Heads;else faceName = Tails;return faceName;的源代碼如下:Runs.java蒂彩文檔.實用標準文案

10、public class Runs public static void main (String args)/number of coin flipsfinal int FUPS =100;/length of the current run of HEADS int currentRun = 0;/length of the maximum run so farint maxRun 二 0;for(int i = 0; i =2¤tRunmaxRun)maxRun = currentRu n;/Flip the coin & print the result/Update the run information/Print the resultsSystem.out.println(maxRun);精彩文檔.實用標準文案四、實驗結(jié)果與數(shù)據(jù)處理Rock, P叩er, Scissors的實驗結(jié)果如下圖:EnterEnter yooxyoox play:play: R Rr r P P o=o= 3 3P POemputerOemputer playplay 1313 R Rp

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論