本文目錄一覽:
- 1、求JAVA 24點遊戲演算法,界面和發牌器已弄好,求演算法
- 2、想用java寫個24點的遊戲、不懂、急求教、
- 3、24點速算遊戲 Java 代碼
- 4、java算24點代碼:輸入4個數算24點,能夠在命令提示符下就可以運行。100多
求JAVA 24點遊戲演算法,界面和發牌器已弄好,求演算法
package zhidao;
import java.util.Scanner;
public class Game21
{
public static void main ( String[] args )
{
System.out.println (“遊戲規則:”);
System.out.println (“開始遊戲後屏幕上將出現一個隨機數字”);
System.out.println (“按a可以隨機增加一個1-10範圍內的一個數字”);
System.out.println (“按s則揭曉你和電腦對決的結果!”);
System.out.println (“如果你的數字大於21點,則遊戲直接結束!”);
int rand = (int) ( Math.random () * 10 + 1 );
int sum1 = rand, sum2 = 0, which = 0; // 0電腦,1我
System.out.println (“系統先給出1個數字: ” + rand);
System.out.print (“輪到你了: “);
Scanner scanner = new Scanner (System.in);
while (true)
{
String input = “”;
if (which == 0)
{
input = scanner.next ();
}
if (which == 0 “a”.equals (input))
{
which = 1;
int r = (int) ( Math.random () * 10 + 1 );
sum2 += r;
System.out.println (“您抽到的數字是: ” + r);
if (sum2 21)
{
System.out.println (“你的點數為 ” + sum2 + ” , 超過了最大限制 21 點,遊戲提前結束!”);
break;
}
System.out.print (“該電腦了: a\n”);
}
else if (which == 1)
{
which = 0;
int r = (int) ( Math.random () * 10 + 1 );
sum1 += r;
System.out.println (“電腦抽到的數字是: ” + r);
if (sum1 21)
{
System.out.println (“電腦點數為 ” + sum1 + ” , 超過了最大限制 21 點,遊戲提前結束!”);
break;
}
System.out.print (“輪到你了: “);
}
else if (which == 0 “s”.equals (input))
{
System.out.println (“電腦共有: ” + sum1 + ” 點”);
System.out.println (“你共有: ” + sum2 + ” 點”);
if (sum1 sum2)
{
System.out.println (“YOU LOSE !!!”);
}
else if (sum1 sum2)
{
System.out.println (“YOU WIN !!!”);
}
else
{
System.out.println (“DRAW GAME !!!”);
}
System.out.print (“輪到你了: “);
which = 0;
}
}
scanner.close ();
}
}
想用java寫個24點的遊戲、不懂、急求教、
給你兩點提示吧
1)四個數有效的運算順序一共5種,如,(1#2)#(3#4),((1#2)#3)#4為其中的兩種。
2)將四則運算用函數完成,定義eval(int lhs, int rhs, int op),lhs、rhs 為左右操作數,op為操作符,這樣窮舉的時候可以將op從1取到4來完成。
PS:一般玩的24點是可以交換順序的,如果須要可以再寫一個全排列的演算法。
24點速算遊戲 Java 代碼
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 用給定的4個整數通過加減乘除運算得到24點,如果有多種情況,則全部輸出,如果不能得到24點,輸出提示br
*
* @思路:將指定的4個數字進行全排列,將運算符『+』、『-』、『*』、『/』取3個進行所有情況排列,
* 然後將所有的數字排列與所有的運算符排列按順序計算,
* 如果最後計算結果等於想要的結果值比如24,則為符合條件的運算,
* 將所有符合條件的數字排列和運算符排列存儲起來,並在最後列印輸出所有可能的情況
*
* @author chenjie
*
*/
public class TwentyFourPoint {
public static void main(String[] args) {
try {
SetString set = caculate(new int[] { 18, 18, 6, 12 }, 24);
printlnResultSet(set);
} catch (Exception e) {
// e.printStackTrace();開發期間方便查找錯誤,測試通過後就無需列印錯誤信息了
System.err.println(e.getMessage());
}
}
/**
* 列印結果集
*
* @param resultSet
* 結果集
*/
private static void printlnResultSet(CollectionString resultSet) {
for (String str : resultSet) {
System.out.println(str);
}
}
/**
* 得到給定整形數組的全排列情況
*
* @param numbers
* 給定的整形數組
* @return 全排列數組
*/
private static int[][] arrangeAllNumbers(int[] numbers) {
Listint[] list = new ArrayListint[]();
allSort(numbers, 0, numbers.length – 1, list);
int[][] resultSet = new int[list.size()][list.get(0).length];
resultSet = list.toArray(resultSet);
return resultSet;
}
/**
* 得到給定的操作中出現的所有操作符排列情況
*
* @param operators
* 出現的操作符數組
* @param number
* 每組操作符的數量
* @return 所有操作符排列數組
*/
private static char[][] arrangeAllOperators(char[] operators, int number) {
int setSize = (int) Math.pow(operators.length, number);
int index = 0;
char[][] resultSet = new char[setSize][number];
for (int i = 0; i operators.length; i++) {
for (int j = 0; j operators.length; j++) {
for (int k = 0; k operators.length; k++) {
resultSet[index][0] = operators[i];
resultSet[index][1] = operators[j];
resultSet[index][2] = operators[k];
index++;
}
}
}
return resultSet;
}
/**
* 根據給定的一組整數,通過加減乘除運算,得到想要的結果,如果可以得到結果,則返回所有可能的結果的運算形式。
* 返回的運算形式,均按從左到右的順序計算,並不是遵循四則運演算法則,比如:br
* 輸出的結果形式為:br
* 1 * 8 – 6 * 12 = 24 br
* 表示的運算順序是:br
* 1:1 * 8 = 8,br
* 2:8 – 6 = 2,br
* 3:2 * 12 = 24br
* 而不是按照四則運演算法則計算:br
* 1:1 * 8 = 8,br
* 2:6 * 12 = 72,br
* 3:8 * 72 = 576br
*
*
* @param numbers
* 給定進行運算的一組整數,4個數為一組
* @param targetNumber
* 想要得到的結果
* @return 所有可能得到想要的結果的所有運算形式的字元串形式集合
* @throws Exception
* 如果不能得到想要的結果,則拋出該異常,表明根據指定的一組數字通過一系列的加減乘除不能得到想要的結果
*/
public static SetString caculate(int[] numbers, int targetNumber)
throws Exception {
SetString resultSet = new HashSetString();// 這裡用Set而不是用List,主要是因為當給定的一組數字中如果有重複數字的話,同一結果會被出現多次,如果用List存放的話,會將重複的結果都存放起來,而Set會自動消除重複值
char[][] operatorsArrangement = arrangeAllOperators(new char[] { ‘+’,
‘-‘, ‘*’, ‘/’ }, 3);
int[][] numbersArrangement = arrangeAllNumbers(numbers);
for (int[] nums : numbersArrangement)
for (char[] operators : operatorsArrangement) {
int result = 0;
try {
result = caculate(nums, operators);
} catch (Exception e) {// 出現非精確計算
continue;
}
if (result == targetNumber)
resultSet.add(buildString(nums, operators, targetNumber));// 如果計算後的結果等於想要的結果,就存放到集合中
}
if (resultSet.isEmpty())
throw new Exception(“給定的數字:” + Arrays.toString(numbers)
+ “不能通過加減乘除運算得到結果:” + targetNumber);
return resultSet;
}
/**
* 將一組整型數字以給定的操作符按順序拼接為一個完整的表達式字元串
*
* @param nums
* 一組整型數字
* @param operators
* 一組操作符
* @param target
* 目標值
* @return 拼接好的表達式字元串
*/
private static String buildString(int[] nums, char[] operators, int target) {
String str = String.valueOf(nums[0]);
for (int i = 0; i operators.length; i++) {
str = str + ‘ ‘ + operators[i] + ‘ ‘ + nums[i + 1];
}
str = str + ” = ” + target;
return str;
}
/**
* 將給定的一組數字以給定的操作符按順序進行運算,如:int result = caculate(new int[]{3,4,5,8}, new
* char[]{‘+’,’-‘,’*’});
*
* @param nums
* 一組數字
* @param operators
* 一組運算符,數量為數字的個數減1
* @return 最後的計算結果
* @throws Exception
* 當計算結果不精確時,拋出該異常,主要是針對除法運算,例如18 / 8 = 2,諸如這樣不精確計算將拋出該異常
*/
private static int caculate(int[] nums, char[] operators) throws Exception {
int result = 0;
for (int i = 0; i operators.length; i++) {
if (i == 0) {
result = caculate(nums[i], nums[i + 1], operators[i]);
} else {
result = caculate(result, nums[i + 1], operators[i]);
}
}
return result;
}
/**
* 根據指定操作符將兩個給定的數字進行計算
*
* @param num1
* 數字1
* @param num2
* 數字2
* @param operator
* 操作符,只能從「+、-、*、/」4個操作符中取值
* @return 計算結果
* @throws Exception
* 當計算結果不精確時,拋出該異常,主要是針對除法運算,例如18 / 8 = 2,諸如這樣不精確計算將拋出該異常
*/
private static int caculate(int num1, int num2, char operator)
throws Exception {
double result = 0;
switch (operator) {// 根據操作符做相應的計算操作
case ‘+’:
result = num1 + num2;
break;
case ‘-‘:
result = num1 – num2;
break;
case ‘*’:
result = num1 * num2;
break;
case ‘/’:
result = (double) num1 / (double) num2;
break;
}
if (!check(result))
throw new Exception(“不精確的計算數字”);
return (int) result;
}
/**
* 檢查指定的浮點數是否可以直接轉換為整型數字而不損失精度
*
* @param result
* 要檢查的浮點數
* @return 如果可以進行無損轉換,返回true,否則返回false
*/
private static boolean check(double result) {
String str = String.valueOf(result);
int pointIndex = str.indexOf(“.”);// 小數點的下標值
String fraction = str.substring(pointIndex + 1);
return fraction.equals(“0”) ? true : false;// 通過判斷小數點後是否只有一個0來確定是否可以無損轉換為整型數值
}
/**
* 對傳入的整型數組buf進行全排列
*
* @param buf
* 要進行全排列的整型數組
* @param start
* 開始的下標值
* @param end
* 結束下標值
* @param list
* 保存最後全排列結果的集合
*/
private static void allSort(int[] buf, int start, int end, Listint[] list) {
if (start == end) {// 當只要求對數組中一個字母進行全排列時,只要就按該數組輸出即可
int[] a = new int[buf.length];
System.arraycopy(buf, 0, a, 0, a.length);
list.add(a);
} else {// 多個字母全排列
for (int i = start; i = end; i++) {
int temp = buf;// 交換數組第一個元素與後續的元素
buf = buf[i];
buf[i] = temp;
allSort(buf, start + 1, end, list);// 後續元素遞歸全排列
temp = buf;// 將交換後的數組還原
buf = buf[i];
buf[i] = temp;
}
}
}
}
java算24點代碼:輸入4個數算24點,能夠在命令提示符下就可以運行。100多
import java.util.Scanner;
/** 給定4個數字計算24 */
public class Core {
private double expressionResult = 24;
// private int maxLine=10;
private boolean error = true;
private double numbers[] = new double[4];
public Object resultReturn;
/**
* 該對象擁有3個私有變數 expressionResult,所需結果 maxLine,輸出結果每頁行數 error,是否出錯
* numbers[4],記錄用來運算的4個數
*
* 其次,該對象擁有以下方法供外部調用 setNumbers(double[] 運算的數) 輸入用來運算的數,4個時才能計算,無返回
* setMaxLine(int 行數) 輸入每頁的行數,無返回 getMaxLine() 返回每頁的行數,類型為int
* setExpressionResult(double 所需結果) 輸入所需結果,無返回 getExpressionResult()
* 返回所需結果,類型為double getExpression() 返回可得出所需結果的表達式,類型為字元串數組
*
* 最後,私有方法均為計算與表達式轉換部分
*/
// 測試使用
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[4];
System.out.print(“輸入第一個數:”);
arr[0] = scanner.nextInt();
System.out.print(“輸入第二個數:”);
arr[1] = scanner.nextInt();
System.out.print(“輸入第三個數:”);
arr[2] = scanner.nextInt();
System.out.print(“輸入第四個數:”);
arr[3] = scanner.nextInt();
Core s = new Core();
s.setNumbers(arr);
String[] output = s.getExpression();
for (int i = 0; i output.length; i++) {
System.out.println(output[i]);
}
}
/** 設定被計算的四個數,由於是數組,所以具有容錯功能(不為4個數) */
public void setNumbers(double[] n) {
if (n.length == 4) {
error = false;
numbers = n;
} else
error = true;
}
public void setNumbers(int[] n) {
if (n.length == 4) {
error = false;
for (int i = 0; i 4; i++) {
numbers[i] = n[i];
}
} else
error = true;
}
/** 設定每頁顯示的行數 */
// public void setMaxLine(int n) {
// if (n0) {
// maxLine=n;
// }
// }
// /** 返回每頁顯示的行數 */
// public int getMaxLine() {
// return maxLine;
// }
/** 設定需要得到的結果 */
public void setExpressionResult(double n) {
expressionResult = n;
}
/** 返回所需結果 */
public double expressionResult() {
return expressionResult;
}
/** 返回符合條件的表達式 */
public String[] getExpression() {
if (!error) {
String[] expression = calculate(numbers);
return expression;
} else
return new String[] { “出錯了,輸入有誤” };
}
/** cal24(),輸出結果為24的表達式 */
private String[] calculate(double[] n) {
if (n.length != 4)
return new String[] { “Error” };
double[] n1 = new double[3];
double[] n2 = new double[2];
String[] resultString = new String[1024]; // 最多1000組解,暫時未溢出
int count = 0;
boolean isRepeat = false;
for (int t1 = 0; t1 6; t1++) {
for (int c1 = 0; c1 6; c1++) {
for (int t2 = 0; t2 3; t2++) {
for (int c2 = 0; c2 6; c2++) {
for (int c3 = 0; c3 6; c3++) {
if ((c1 / 3 == c2 / 3 (c1 % 3) * (c2 % 3) != 0)
|| (c2 / 3 == c3 / 3 (c2 % 3) * (c3 % 3) != 0)
|| (c1 / 3 == c3 / 3
(c1 % 3) * (c3 % 3) != 0 t2 == 2)) {
// 去除連減連除的解,因為x/(y/z)=x*z/y
continue;
}
n1 = cal1(n, t1, c1);
n2 = cal2(n1, t2, c2);
double result = cal(n2[0], n2[1], c3);
if ((result – expressionResult) 0.00000001
(expressionResult – result) 0.00000001) {
resultString[count] = calString(n, t1, c1, t2,
c2, c3)
+ “=” + (int) expressionResult;
for (int i = 0; i count; i++) {
isRepeat = false;
if (resultString[i]
.equals(resultString[count])) { // 去除完全重複的解
isRepeat = true;
break; // 提前退出循環
}
}
if (c1 == c2 c2 == c3 c1 % 3 == 0
t1 + t2 != 0) { // 連加連乘
isRepeat = true;
}
if (!isRepeat) {
count++;
}
}
}
}
}
}
}
if (count == 0)
return new String[] { “該組數無解” };
String[] resultReturn = new String[count];
System.arraycopy(resultString, 0, resultReturn, 0, count);
return resultReturn;
}
/** cal1(),將4個數計算一次後返回3個數 */
private double[] cal1(double[] n, int t, int c) { // t為原來的t1,c為原來的c1
double[] m = new double[3];
switch (t) {
case 0:
m[1] = n[2];
m[2] = n[3];
m[0] = cal(n[0], n[1], c);
break;
case 1:
m[1] = n[1];
m[2] = n[3];
m[0] = cal(n[0], n[2], c);
break;
case 2:
m[1] = n[1];
m[2] = n[2];
m[0] = cal(n[0], n[3], c);
break;
case 3:
m[1] = n[0];
m[2] = n[3];
m[0] = cal(n[1], n[2], c);
break;
case 4:
m[1] = n[0];
m[2] = n[2];
m[0] = cal(n[1], n[3], c);
break;
default:
m[1] = n[0];
m[2] = n[1];
m[0] = cal(n[2], n[3], c);
}
return m;
}
/** cal2(),將3個數計算一次後返回2個數 */
private double[] cal2(double[] n, int t, int c) { // t為原來的t2,c為原來的c2
double[] m = new double[2];
switch (t) {
case 0:
m[1] = n[2];
m[0] = cal(n[0], n[1], c);
break;
case 1:
m[1] = n[1];
m[0] = cal(n[0], n[2], c);
break;
default:
m[1] = n[0];
m[0] = cal(n[1], n[2], c);
}
return m;
}
/** cal(),將2個數計算後返回結果 */
private double cal(double n1, double n2, int c) { // n1,n2為運算數,c為運算類型
switch (c) {
case 0:
return n1 + n2;
case 1:
return n1 – n2;
case 2:
return n2 – n1;
case 3:
return n1 * n2;
case 4:
if (n2 == 0)
return 9999; // 使計算結果必不為24
else
return n1 / n2;
default:
if (n1 == 0)
return 9999; // 同上
else
return n2 / n1;
}
}
/** calString(),輸出表達式 */
private String calString(double[] n, int t1, int c1, int t2, int c2, int c3) {
String[] nString = new String[4];
switch (t1) {
case 0:
nString[0] = calString2(“” + (int) n[0], “” + (int) n[1], c1);
nString[1] = “” + (int) n[2];
nString[2] = “” + (int) n[3];
break;
case 1:
nString[0] = calString2(“” + (int) n[0], “” + (int) n[2], c1);
nString[1] = “” + (int) n[1];
nString[2] = “” + (int) n[3];
break;
case 2:
nString[0] = calString2(“” + (int) n[0], “” + (int) n[3], c1);
nString[1] = “” + (int) n[1];
nString[2] = “” + (int) n[2];
break;
case 3:
nString[0] = calString2(“” + (int) n[1], “” + (int) n[2], c1);
nString[1] = “” + (int) n[0];
nString[2] = “” + (int) n[3];
break;
case 4:
nString[0] = calString2(“” + (int) n[1], “” + (int) n[3], c1);
nString[1] = “” + (int) n[0];
nString[2] = “” + (int) n[2];
break;
default:
nString[0] = calString2(“” + (int) n[2], “” + (int) n[3], c1);
nString[1] = “” + (int) n[0];
nString[2] = “” + (int) n[1];
}
if ((c2 / 3 c1 / 3 (t2 != 2 || c2 / 3 == c3 / 3))
|| ((c3 / 3 c1 / 3 + c2 / 3) t2 == 2)
|| (c3 == 1 c1 / 3 == 0)) // 特定情況下加上一個括弧*****************************
nString[0] = ‘(‘ + nString[0] + ‘)’;
switch (t2) {
case 0:
nString[0] = calString2(nString[0], “” + nString[1], c2);
nString[1] = nString[2];
break;
case 1:
nString[0] = calString2(nString[0], nString[2], c2);
break;
default:
nString[3] = nString[0];
nString[0] = calString2(nString[1], nString[2], c2);
nString[1] = nString[3];
}
if (c3 / 3 c2 / 3 || (c3 == 2 nString[0].indexOf(‘+’) = 0)) // 特定情況下加上一個括弧*****************************
nString[0] = ‘(‘ + nString[0] + ‘)’;
return calString2(nString[0], nString[1], c3);
}
/** calString(),根據符號輸出一部運算表達式 */
private String calString2(String n1, String n2, int c) {
switch (c) {
case 0:
return n1 + ‘+’ + n2;
case 1:
return n1 + ‘-‘ + n2;
case 2:
return n2 + ‘-‘ + n1;
case 3:
return n1 + ‘*’ + n2;
case 4:
return n1 + ‘/’ + n2;
default:
return n2 + ‘/’ + n1;
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/180295.html