java零基礎入門第五天 綜合運用 戰艦遊戲 獲取隨機數 強轉類型

第五天,也就是書中的第五章,這一章內容是編寫程序,在瞭解前四章的基礎知識,現在是要把知識開始運用,在以後的工作和學習中,你會碰到各種各樣的需求,那麼碰到客戶的需求你該怎麼把他轉換成我們實現的代碼呢?第五章中就有教我們設計思路,首先知道下需求,我們是要設計一個類似戰艦攻擊的遊戲

上面是流程圖 下一頁類的設計思路,首先這章我們先開發給簡單版本。

在書中,教我們先寫僞代碼,測試碼,真實碼,第一步的僞代碼就不是代碼,可以是文字,寫出你需要的類,變量,方法,詳細內容如下圖

一 :在我的設計中,我覺得這個至少需要兩個類,一個是戰艦的類,裏面應該保護了戰艦的位置(locationCells),擊中次數兩個變量(numOfHits ),以及設置戰艦位置(setLocationCells),檢查戰艦是否被擊中(checkYourself)兩個方法。一個是執行類,包含了main方法,方法中調用給戰艦賦值位置,然後輸入猜的值,調用是否擊中的方法。上面所寫的就是僞代碼

class SimpleDotCom{//戰艦類
	int[] locationCells;//定義戰艦位置 以數組形式
	int numOfHits = 0;// 定義擊中次數
	public void setLocationCells(int[] locs){//戰艦位置的set方法
		locationCells = locs;//
	}

	public String checkYourself(String stringGuess){//檢查是否被擊中的方法
		int guess = Integer.parseInt(stringGuess);//將傳進來的實參轉換爲int類型 並賦值給guess
		String result = "miss";//定義局部變量 結果爲miss
		for(int cell : locationCells){// for循環 判斷戰艦位置數組中每一個值 有沒有跟guess相同
			if(guess == cell){//如果有相同
				result = "hit";//結果 改爲hit
				numOfHits++;//實例變量 擊中次數加1
				break;//
			}
		}
		if(numOfHits == locationCells.length){//當擊中次 與戰艦總長度相等時
			result = "kill";//將結果變成擊殺
			
		}
		System.out.println(locationCells.length);//輸出 戰艦長度
		System.out.println(result);//輸出結果
		return result;// 返回結果 因爲checkYourself這個方法是有返回的 所以得返回值
	}
}

上面是定義好的戰艦類

public class SimpleDotComTestDrive1{//執行類
	public static void main (String[] args){//主函數
		SimpleDotCom dot = new SimpleDotCom();//定義個 戰艦對象
		int[] locations = {2,3,4};//給數組location 賦值 假數據
		dot.setLocationCells(locations);//給戰艦位置賦值 location 假數據
		String userGuess = "2";// 定義猜的數據
		String result = dot.checkYourself(userGuess);//調用檢查是否擊中的方法,並將假數據傳過去
	}

}

二:上面執行類和戰艦類,數據都是寫死的假數據,書中稱爲測試碼

執行結果如下圖

三:真實碼跟測試碼還有些區別,戰艦的位置,跟用戶猜的數據,這兩個數據應該都是隨機和用戶自己猜的,戰艦位置隨機好解決,用戶猜的數據怎麼來呢?我們應該還要定義個用戶猜的類

import java.io.*;//引用包
class GameHelper{//定義個用戶輸入的類
	public String getUserInput(String promt){//獲取用戶輸入值 int類型
		String inputLine = null;//定義個變量
		System.out.print(promt + " ");//輸出實參

		try{//異常捕捉 不做擴展 能用就好
			BufferedReader is = new BufferedReader(//定義一個讀取BufferedReader 對象
			new InputStreamReader(System.in));//
			inputLine = is.readLine();//獲取屏幕上數據 賦值給inputLine 
			if(inputLine.length() == 0 )return null;//
		}catch(IOException e){//
			System.out.println("IOException : " + e);//
		}//
		return inputLine;	
	}//
	
}

完整的代碼

import java.io.*;
public class SimpleDotComTestDrive2{//執行類
	public static void main (String[] args){
		System.out.println("深海中有一艘戰艦 請輸入1-20數字 擊中三次就能擊沉戰艦");
		int numOfGuesses = 0;//記錄玩家猜測次數的變量
		GameHelper helper = new GameHelper();//玩家輸入的類

		SimpleDotCom theDotCom = new SimpleDotCom();//新建一個戰艦的類
		int randomNum = (int)(Math.random()*20);//用隨機數產生戰艦第一個位置
		System.out.println("隨機數是" + randomNum);
		int[] locations = {randomNum,randomNum+1,randomNum+2};//用隨機數產生的三個數組成一個數組 作爲戰艦
		theDotCom.setLocationCells(locations);//給戰艦賦值
		boolean isAlive = true;//一個用於判斷是否擊沉戰艦的標識符
		
		while(isAlive == true){//只要戰艦還存在 就一直執行判斷
			String guess = helper.getUserInput("請輸入數字 : ");//獲取玩家輸入字符串
			String result = theDotCom.checkYourself(guess);//檢查玩家的猜測 並將方法返回的結果保存到result內
			numOfGuesses++;
			if(result.equals("擊沉")){//判斷是否擊沉 如果擊沉改變標識符 輸出使用了幾次擊沉
				isAlive = false;
				System.out.println("你使用了" + numOfGuesses + " 次擊沉戰艦");
			}
		}
		
	}

}
class SimpleDotCom{

	int[] locationCells;
	int numOfHits = 0;

	public void setLocationCells(int[] locs){
		locationCells = locs;
	}

	public String checkYourself(String stringGuess){
		int guess = Integer.parseInt(stringGuess);
		String result = "未擊中";
		for(int cell : locationCells){
			if(guess == cell){
				result = "擊中";
				numOfHits++;
				break;
			}
		}
		if(numOfHits == locationCells.length){
			result = "擊沉";
			
		}
		System.out.println("你輸入數字的結果是 : "+result);
		return result;
	}
}

class GameHelper{
	public String getUserInput(String promt){
		String inputLine = null;
		System.out.println(promt + " 5555");

		try{
			BufferedReader is = new BufferedReader(
			new InputStreamReader(System.in));
			inputLine = is.readLine();
			if(inputLine.length() == 0 )return null;
		}catch(IOException e){
			System.out.println("IOException : " + e);
		}
		return inputLine;	
	}
	
}

運行結果

總結下 今天的主要是設計思路,新的知識點不多,有一個獲取隨機數,一個強轉類型,一個引用第三方的包

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章