//構(gòu)建控制臺的輸入流
創(chuàng)新互聯(lián)建站IDC提供業(yè)務(wù):大邑服務(wù)器托管,成都服務(wù)器租用,大邑服務(wù)器托管,重慶服務(wù)器租用等四川省內(nèi)主機(jī)托管與主機(jī)租用業(yè)務(wù);數(shù)據(jù)中心含:雙線機(jī)房,BGP機(jī)房,電信機(jī)房,移動機(jī)房,聯(lián)通機(jī)房。
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("input:\n");
//用于存儲鍵值對的map
Map String, Integer countMap = new HashMapString, Integer();
String strtemp = "";
String []strarray;
//從控制臺讀取一行
String str = reader.readLine();
reader.close();
int count = 1;
int cur;
//如果沒有輸入
if(str == null || "".equals(str))
System.out.println("there are some erros!");
else{
//在字符串中找到第一個{
cur = str.indexOf('{');
if(cur 0){
System.out.println("do not contain '{'");
}else{
//在字符串中找到最后一個}
strtemp = str.substring(cur + 1);
cur = strtemp.lastIndexOf('}');
if(cur = 0){
strtemp = strtemp.substring(0, cur);
strarray = strtemp.split("\\,");
for(String ts : strarray){//將找到的字符串放入map中
if(countMap.containsKey(ts))
count += countMap.get(ts);
countMap.put(ts, count);
count = 1;
}
//打印字符串鍵對值
for(String ts : strarray){
if(countMap.containsKey(ts)){
System.out.println(ts + ": " + countMap.get(ts));
countMap.remove(ts);
}
}
}else{
System.out.println("do not contain '}' or no values in {}");
}
}
}
在C盤新建文件1.txt,輸入任意字符,如下圖:
編寫java代碼。如下:
import?java.io.BufferedReader;
import?java.io.FileNotFoundException;
import?java.io.FileReader;
import?java.io.IOException;
import?java.util.TreeMap;
public?class?Test?{
//?統(tǒng)計數(shù)字或者字符出現(xiàn)的次數(shù)
public?static?TreeMapCharacter,?Integer?Pross(String?str)?{
char[]?charArray?=?str.toCharArray();
TreeMapCharacter,?Integer?tm?=?new?TreeMapCharacter,?Integer();
for?(int?x?=?0;?x??charArray.length;?x++)?{
if?(!tm.containsKey(charArray[x]))?{
tm.put(charArray[x],?1);
}?else?{
int?count?=?tm.get(charArray[x])?+?1;
tm.put(charArray[x],?count);
}
}
return?tm;
}
public?static?void?main(String[]?args)?{
BufferedReader?br?=?null;
int?line?=?0;
String?str?=?"";
StringBuffer?sb??=?new?StringBuffer();
try?{
br?=?new?BufferedReader(new?FileReader("c:\\1.txt"));
while?((str?=?br.readLine())?!=?null)?{
sb.append(str);
++line;
}
System.out.println("\n文件行數(shù):?"?+?line);
System.out.println("\n文件內(nèi)容:?"?+?sb.toString());
TreeMapCharacter,?Integer?tm?=?Pross(sb.toString());
System.out.println("\n字符統(tǒng)計結(jié)果為:"?+?tm);
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}?finally?{
if?(br?!=?null)?{
try?{
br.close();
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
}
}
}
}運(yùn)行結(jié)果如下圖:
為了讓游戲有參與感,并體現(xiàn)java面對對象的思想,我先創(chuàng)建一個Player選手類,包含選手的名字playerName還有出拳方法guess()。出拳時采用隨機(jī)獲取0、1和2的方式分別代表石頭、剪刀和布,代碼如下:
public class Player {
private String playerName;
public Player(String playerName) {
this.playerName = playerName;
}
public String getPlayerName() {
return playerName;
}
//出拳方法 0-石頭 1-剪刀 2-布
public int guess() {
//隨機(jī)獲取0、1、2
int num = new Random().nextInt(3);
if (num == 0) {
System.out.print("選手" + this.playerName + "出的是石頭 ");
} else if (num == 1) {
System.out.print("選手" + this.playerName + "出的是剪刀 ");
} else if (num == 2) {
System.out.print("選手" + this.playerName + "出的是布 ");
}
return num;
}
}
然后在主類中,首先要輸入對局的總數(shù),然后創(chuàng)建兩名選手進(jìn)行pk,在pk()方法中制定了獲勝規(guī)則,詳見代碼注釋。最終統(tǒng)計并利用BigDecimal計算勝率(BigDecimal可以很完美的解決整數(shù)除法及其四舍五入保留小數(shù)的問題):
public class Main {
public static void main(String[] args) {
System.out.println("請輸入本局局?jǐn)?shù):");
Scanner scanner = new Scanner(System.in);
int sum = scanner.nextInt();
//創(chuàng)建結(jié)果數(shù)組,resultArray[0]代表p1的獲勝局?jǐn)?shù),resultArray[1]代表p2的獲勝局?jǐn)?shù),resultArray[2]代表平局局?jǐn)?shù)
int[] resultArray = new int[3];
//創(chuàng)建兩名選手
Player p1 = new Player("張三");
Player p2 = new Player("李四");
for (int i = 0; i sum; i++) {
//根據(jù)總局?jǐn)?shù)進(jìn)行pk
int result = pk(p1, p2);
if (result == 1) {
resultArray[0]++;
} else if (result == -1) {
resultArray[1]++;
} else {
resultArray[2]++;
}
}
System.out.println("");
System.out.println("最終結(jié)果統(tǒng)計:");
System.out.println("選手[" + p1.getPlayerName() + "]獲勝局?jǐn)?shù)為:" + resultArray[0] + ",勝率為:" +
new BigDecimal(resultArray[0]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP)) + "%");
System.out.println("選手[" + p2.getPlayerName() + "]獲勝局?jǐn)?shù)為:" + resultArray[1] + ",勝率為:" +
new BigDecimal(resultArray[1]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP)) + "%");
System.out.println("平局局?jǐn)?shù)為:" + resultArray[2] + ",平局率為:" +
new BigDecimal(resultArray[2]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP)) + "%");
}
//0-石頭 1-剪刀 2-布
//return 0:平局 1:p1獲勝 -1:p2獲勝
private static int pk(Player p1, Player p2) {
System.out.println("--------------------");
int a = p1.guess();
int b = p2.guess();
System.out.print("\n對局結(jié)果:");
//出拳相同平局
if (a == b) {
System.out.println("平局");
return 0;
}
//p1獲勝條件:p1出石頭時p2出剪刀,p1出剪刀時p2出步,p1出布時p2出石頭
else if ((a == 0 b == 1) || (a == 1 b == 2) || (a == 2 b == 0)) {
System.out.println("選手[" + p1.getPlayerName() + "]獲勝");
return 1;
}
//p2獲勝條件:p1出石頭時p2出布,p1出剪刀時p2出石頭,p1出布時p2出剪刀
else if ((a == 0 b == 2) || (a == 1 b == 0) || (a == 2 b == 1)) {
System.out.println("選手[" + p2.getPlayerName() + "]獲勝");
return -1;
} else {
//因為規(guī)定了隨機(jī)數(shù)產(chǎn)生0、1、2,所以其實不會走到本分支
throw new IllegalArgumentException("本局無效");
}
}
}
對局5局的運(yùn)行結(jié)果:
我這里就只能統(tǒng)計當(dāng)前游戲的數(shù)據(jù)了,如果你想統(tǒng)計多局游戲總的勝率信息,那么需要將每一局的比賽結(jié)果寫到txt文件里,最終根據(jù)txt文件內(nèi)容統(tǒng)計即可。
作為一個學(xué)生,需要增強(qiáng)自己的動手能力哦,不然這樣的設(shè)計會變得毫無用處。下面這個可以學(xué)習(xí)下
網(wǎng)頁鏈接
網(wǎng)頁標(biāo)題:Java統(tǒng)計代碼運(yùn)行圖 java生成統(tǒng)計圖表
網(wǎng)頁網(wǎng)址:http://www.muchs.cn/article18/docsegp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、搜索引擎優(yōu)化、網(wǎng)站排名、動態(tài)網(wǎng)站、域名注冊、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)