日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java猜单词游戏_Java_初级编程,猜英文单词游戏

發布時間:2023/12/20 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java猜单词游戏_Java_初级编程,猜英文单词游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

大家好,這是我第一次在BlogJava發帖,本人酷愛編程,加入BlogJava社區,就是為了認識結交更多有共同興趣愛好的朋友,交流彼此學習編程的經驗,請大家多多支持我

下面的Java程序是我學習Java初期的一個作業:猜英文單詞游戲,作為我Blog第一篇隨筆,這個程序也可以直接點擊http://www.blogjava.net/Files/hughmay/Assignment_1.zip下載,然后輸入(import)到Java編輯軟件,如eclipse。直接運行該程序。

/*

* Author: ZHUANG JIN YI_91237

* This program is a simple game application (GUI) that allows two players compete with each other

* by guessing a word from a pool of 10 letters. Players can choose to pass to next player or choose to quit to end this game :)

* 2010 Copyright Zhuang Jinyi, Jason.

*/

import java.util.*;

import javax.swing.*;

public class FindYourWords

{

public static void main(String[] args)

{

Scanner console = new Scanner (System.in);

Random generator = new Random();

String [] dict= FileUtil.readDictFromFile("words.txt");

int scorePlayerA = 0;

int scorePlayerB = 0;

int sumScoreA = 0;

int sumScoreB = 0;

int i;

int match = 0;

int match1 = 0;

int match2 = 0;

int match3 = 0;

int[] ranNum = new int[8];

int[] ranNumVowel = new int[2];

String junk;

String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "g", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

String[] vowels = {"a", "e", "i", "o", "u"};

String[] randomLetter = new String[8];

String[] randomVowel = new String[2];

String playerAInput = "";

String playerBInput = "";

String[] point1 = {"E" ,"A" ,"O" ,"T", "I", "N", "R", "S", "L", "U"};

String[] point2 = {"D", "G"};

String[] point3 = {"C", "M","B", "P"};

String[] point4 = {"H","F", "W", "Y"};

String[] point5 = {"K"};

String[] point8 = {"J", "X"};

String[] point10 = {"Q", "Z"};

JOptionPane.showMessageDialog (null, "-----------------------------------------Game: Find Your Words-----------------------------------------\n" + "=======================Ready to Play? Click 'Ok'=======================", "Find Your Words", JOptionPane.INFORMATION_MESSAGE);

try

{

while (!playerAInput.equalsIgnoreCase("!") && !playerBInput.equalsIgnoreCase("!"))

{

if (!playerAInput.equalsIgnoreCase("!") || playerBInput.equalsIgnoreCase("@"))

{

//random letters generation

for (i = 0; i < ranNum.length; i++ )

{

ranNum[i] = generator.nextInt(26);

randomLetter[i] = letters[ranNum[i]];

}

for (i = 0; i < ranNumVowel.length; i++ )

{

ranNum[i] = generator.nextInt(5);

randomVowel[i] = vowels[ranNum[i]];

}

//store random letters & prompt user input

String[] randomQues = {randomLetter[0], randomVowel[1], randomLetter[1], randomVowel[0], randomLetter[2], randomLetter[3], randomLetter[4], randomLetter[5], randomLetter[6], randomLetter[7]};

//GUI dialog box

String randomLetters = "Letters of Player A: " + randomLetter[0] + " " + randomVowel[1] + " " + randomLetter[1] + " " + randomVowel[0] + " " + randomLetter[2] + " " + randomLetter[3] + " " + ?randomLetter[4] + " " + randomLetter[5] + " " + randomLetter[6] + " " + randomLetter[7];

playerAInput = JOptionPane.showInputDialog (randomLetters + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");

String[] playerA = new String[playerAInput.length()];

playerA = playerAInput.split("");

/*

for (i = 0; i < playerA.length; i++)

{

System.out.println(playerA[i]);

}

System.out.println(playerA.length);

*/

while (!playerAInput.equalsIgnoreCase("@") && !playerAInput.equalsIgnoreCase("!"))

{

//checking validity part

scorePlayerA = 0;

match = 0;

match1 = 0;

for (i = 0; i < dict.length; i++)

{

if (dict[i].equalsIgnoreCase(playerAInput))

{

match += 1;

}

else

{

match += 0;

}

}

if (match >= 1)

{

for (i = 0; i < playerA.length; i++)

{

for (int m = 0; m < randomQues.length; m++)

{

if (playerA[i].equalsIgnoreCase(randomQues[m]))

{

match1 += 1;

break;

}

}

}

if (match1 >= (playerA.length - 1))

{

//Giving points to players

for (i = 0; i < playerA.length; i++)

{

for (int m = 0; m < point1.length; m++)

{

if (point1[m].equalsIgnoreCase(playerA[i]))

{

scorePlayerA += 1;

}

}

}

for (i = 0; i < playerA.length; i++)

{

for (int m = 0; m < point2.length; m++)

{

if (point2[m].equalsIgnoreCase(playerA[i]))

{

scorePlayerA += 2;

}

}

}

for (i = 0; i < playerA.length; i++)

{

for (int m = 0; m < point3.length; m++)

{

if (point3[m].equalsIgnoreCase(playerA[i]))

{

scorePlayerA += 3;

}

}

}

for (i = 0; i < playerA.length; i++)

{

for (int m = 0; m < point4.length; m++)

{

if (point4[m].equalsIgnoreCase(playerA[i]))

{

scorePlayerA += 4;

}

}

}

for (i = 0; i < playerA.length; i++)

{

for (int m = 0; m < point5.length; m++)

{

if (point5[m].equalsIgnoreCase(playerA[i]))

{

scorePlayerA += 5;

}

}

}

for (i = 0; i < playerA.length; i++)

{

for (int m = 0; m < point8.length; m++)

{

if (point8[m].equalsIgnoreCase(playerA[i]))

{

scorePlayerA += 8;

}

}

}

for (i = 0; i < playerA.length; i++)

{

for (int m = 0; m < point10.length; m++)

{

if (point10[m].equalsIgnoreCase(playerA[i]))

{

scorePlayerA += 10;

}

}

}

sumScoreA += scorePlayerA;

/*

System.out.println("Total score for word: " + scorePlayerA);

System.out.println("Total score for game: " + sumScoreA);

*/

String result = "Total score for word: " + scorePlayerA + "\n" + "Total score for game: " + sumScoreA;

JOptionPane.showMessageDialog(null, result, "This Round Result :)", JOptionPane.INFORMATION_MESSAGE);

break;

}

else

{

JOptionPane.showMessageDialog(null, "Error : A valid word is formed but one or more letter(s) used are not yours.", "Error :(", JOptionPane.QUESTION_MESSAGE);

playerAInput = JOptionPane.showInputDialog (randomLetters + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");

playerA = playerAInput.split("");

}

}

else

{

JOptionPane.showMessageDialog(null, "Error : An invalid word is formed.", "Error :(", JOptionPane.ERROR_MESSAGE);

playerAInput = JOptionPane.showInputDialog (randomLetters + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");

playerA = playerAInput.split("");

}

}

}

if (!playerAInput.equalsIgnoreCase("!") || playerAInput.equalsIgnoreCase("@"))

{

//random letters generation

for (i = 0; i < ranNum.length; i++ )

{

ranNum[i] = generator.nextInt(26);

randomLetter[i] = letters[ranNum[i]];

}

for (i = 0; i < ranNumVowel.length; i++ )

{

ranNum[i] = generator.nextInt(5);

randomVowel[i] = vowels[ranNum[i]];

}

//store random letters & prompt user input

String[] randomQues1 = {randomLetter[0], randomVowel[1], randomLetter[1], randomVowel[0], randomLetter[2], randomLetter[3], randomLetter[4], randomLetter[5], randomLetter[6], randomLetter[7]};

//GUI dialog box

String randomLetters1 = "Letters of Player B: " + randomLetter[0] + " " + randomVowel[1] + " " + randomLetter[1] + " " + randomVowel[0] + " " + randomLetter[2] + " " + randomLetter[3] + " " + ?randomLetter[4] + " " + randomLetter[5] + " " + randomLetter[6] + " " + randomLetter[7];

playerBInput = JOptionPane.showInputDialog (randomLetters1 + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");

String[] playerB = new String[playerBInput.length()];

playerB = playerBInput.split("");

while (!playerBInput.equalsIgnoreCase("@") && !playerBInput.equalsIgnoreCase("!"))

{

//checking validity part

scorePlayerB = 0;

match2 = 0;

match3 = 0;

for (i = 0; i < dict.length; i++)

{

if (dict[i].equalsIgnoreCase(playerBInput))

{

match2 += 1;

}

else

{

match2 += 0;

}

}

if (match2 >= 1)

{

for (i = 0; i < playerB.length; i++)

{

for (int m = 0; m < randomQues1.length ; m++)

{

if (playerB[i].equalsIgnoreCase(randomQues1[m]))

{

match3 += 1;

break;

}

}

}

if (match3 >= (playerB.length - 1))

{

//Giving points to players

for (i = 0; i < playerB.length; i++)

{

for (int m = 0; m < point1.length; m++)

{

if (point1[m].equalsIgnoreCase(playerB[i]))

{

scorePlayerB += 1;

}

}

}

for (i = 0; i < playerB.length; i++)

{

for (int m = 0; m < point2.length; m++)

{

if (point2[m].equalsIgnoreCase(playerB[i]))

{

scorePlayerB += 2;

}

}

}

for (i = 0; i < playerB.length; i++)

{

for (int m = 0; m < point3.length; m++)

{

if (point3[m].equalsIgnoreCase(playerB[i]))

{

scorePlayerB += 3;

}

}

}

for (i = 0; i < playerB.length; i++)

{

for (int m = 0; m < point4.length; m++)

{

if (point4[m].equalsIgnoreCase(playerB[i]))

{

scorePlayerB += 4;

}

}

}

for (i = 0; i < playerB.length; i++)

{

for (int m = 0; m < point5.length; m++)

{

if (point5[m].equalsIgnoreCase(playerB[i]))

{

scorePlayerB += 5;

}

}

}

for (i = 0; i < playerB.length; i++)

{

for (int m = 0; m < point8.length; m++)

{

if (point8[m].equalsIgnoreCase(playerB[i]))

{

scorePlayerB += 8;

}

}

}

for (i = 0; i < playerB.length; i++)

{

for (int m = 0; m < point10.length; m++)

{

if (point10[m].equalsIgnoreCase(playerB[i]))

{

scorePlayerB += 10;

}

}

}

sumScoreB += scorePlayerB;

String result1 = "Total score for word: " + scorePlayerB + "\n" + "Total score for game: " + sumScoreB;

JOptionPane.showMessageDialog(null, result1, "This Round Result :)", JOptionPane.INFORMATION_MESSAGE);

break;

}

else

{

JOptionPane.showMessageDialog(null, "Error : A valid word is formed but one or more letter(s) used are not yours.", "Error :(", JOptionPane.QUESTION_MESSAGE);

playerBInput = JOptionPane.showInputDialog (randomLetters1 + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");

playerB = playerBInput.split("");

}

}

else

{

JOptionPane.showMessageDialog(null, "Error : An invalid word is formed.", "Error :(", JOptionPane.ERROR_MESSAGE);

playerBInput = JOptionPane.showInputDialog (randomLetters1 + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");

playerB = playerBInput.split("");

}

}

}

}

//Calculate and compare the final result

if (sumScoreA > sumScoreB)

{

String finalResult = "Total score for Player A: " + sumScoreA + "\n" + "Total score for Player B: " + sumScoreB + "\n" + "Player A wins!";

JOptionPane.showMessageDialog (null, "(: *********************Final Result********************* :)\n" + finalResult, "Final Result :)", JOptionPane.INFORMATION_MESSAGE);

}

else if (sumScoreA < sumScoreB)

{

String finalResult = "Total score for Player A: " + sumScoreA + "\n" + "Total score for Player B: " + sumScoreB + "\n" + "Player B wins!";

JOptionPane.showMessageDialog (null, "(: *********************Final Result********************* :)\n" + finalResult, "Final Result :)", JOptionPane.INFORMATION_MESSAGE);

}

else if (sumScoreA == sumScoreB)

{

String finalResult = "Total score for Player A: " + sumScoreA + "\n" + "Total score for Player B: " + sumScoreB + "\n" + "The result is a draw!";

JOptionPane.showMessageDialog (null, "(: *********************Final Result********************* :)\n" + finalResult, "Final Result :)", JOptionPane.INFORMATION_MESSAGE);

}

}

catch (Exception e)

{

junk = console.nextLine();

String errorMessage = "Error, " + junk + " is not acceptable!" + "\n" + "Sorry, Game Over" + "\n" + "Please run this program again :)";

JOptionPane.showMessageDialog (null, errorMessage, "Error Message :(", JOptionPane.INFORMATION_MESSAGE);

}

}

}

PS: good night :)

總結

以上是生活随笔為你收集整理的java猜单词游戏_Java_初级编程,猜英文单词游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。