导航:首页 > 编程语言 > 24点游戏java

24点游戏java

发布时间:2022-08-08 23:31:25

1. 用C语言程序编写“抢24”游戏,规则就是两个人,第一个人从数字1开始,可以说1或者1和2,第二个人

摘要 您好,从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏。

2. 求高手帮忙做个用java做的24点游戏程序

呵呵,是要算24点结果,还是要一个带界面,游戏步骤的24点游戏?
你就简单一句话,谁知道你要干啥啊~

3. 一个java面试题:计算24点游戏 编程

import java.util.Random;

public class test2
{
public static void main(String[] args)
{
Random random = new Random();
int a[] = new int[4];
for(int i=0; i<4; i++)
{
a[i] = random.nextInt(13)+1;
}
for(int j=0; j<4; j++)
{
System.out.println("第" + j +"个数:" + a[j]);
}
Calcula(a);

}

public static void Calcula(int[] a)
{
int add, sub, multi, div;
add = 0;
sub = 0;
multi = 0;
div = 0;
for(int i=0; i<4; i++)
{
add = add + a[i];
sub = sub - a[i];
multi = multi * a[i];
div = div/a[i];
}
if(add==24)
{
System.out.println(a[0] + "+" + a[1] + "+" + a[2] + "+" + a[3]
+ "=" + add);

}
else if(sub==24)
{
System.out.println(a[0] + "-" + a[1] + "-" + a[2] + "-" + a[3]
+ "=" + sub);
}
else if(multi==24)
{
System.out.println(a[0] + "*" + a[1] + "*" + a[2] + "*" + a[3]
+ "=" + multi);
}
else if(div==24)
{
System.out.println(a[0] + "÷" + a[1] + "÷" + a[2] + "÷" + a[3]
+ "=" + div);
}
else
{
System.out.println("对不起,没有实现24点的数");
}
}
}

编译通过~

4. 用JAVA设计算24点的游戏的随机数字问题

public void randFour(){
int[] a=new int[4];
for(int i=0;i<a.length;i++){
a[i]=(int)( Math.random()*20+1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("第"+(i+1)+"个数:"+a[i]);
}
}

5. 用java语言实现24点纸牌游戏,并且在游戏中可以

看过两天有没时间,有时间可以给你写个控制台版的,带界面的就没啥可能了,还有就是提问之前请用“请”字

6. 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 (n>0) {
// 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.array(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;
}
}
}

7. 24点速算游戏 Java 代码

  1. importjava.util.ArrayList;
  2. importjava.util.Arrays;
  3. importjava.util.Collection;
  4. importjava.util.HashSet;
  5. importjava.util.List;
  6. importjava.util.Set;
  7. /**
  8. *用给定的4个整数通过加减乘除运算得到24点,如果有多种情况,则全部输出,如果不能得到24点,输出提示<br>
  9. *
  10. *@思路:将指定的4个数字进行全排列,将运算符‘+’、‘-’、‘*’、‘/’取3个进行所有情况排列,
  11. *然后将所有的数字排列与所有的运算符排列按顺序计算,
  12. *如果最后计算结果等于想要的结果值比如24,则为符合条件的运算,
  13. *将所有符合条件的数字排列和运算符排列存储起来,并在最后打印输出所有可能的情况
  14. *
  15. *@authorchenjie
  16. *
  17. */
  18. publicclassTwentyFourPoint{
  19. publicstaticvoidmain(String[]args){
  20. try{
  21. Set<String>set=caculate(newint[]{18,18,6,12},24);
  22. printlnResultSet(set);
  23. }catch(Exceptione){
  24. //e.printStackTrace();开发期间方便查找错误,测试通过后就无需打印错误信息了
  25. System.err.println(e.getMessage());
  26. }
  27. }
  28. /**
  29. *打印结果集
  30. *
  31. *@paramresultSet
  32. *结果集
  33. */
  34. (Collection<String>resultSet){
  35. for(Stringstr:resultSet){
  36. System.out.println(str);
  37. }
  38. }
  39. /**
  40. *得到给定整形数组的全排列情况
  41. *
  42. *@paramnumbers
  43. *给定的整形数组
  44. *@return全排列数组
  45. */
  46. privatestaticint[][]arrangeAllNumbers(int[]numbers){
  47. List<int[]>list=newArrayList<int[]>();
  48. allSort(numbers,0,numbers.length-1,list);
  49. int[][]resultSet=newint[list.size()][list.get(0).length];
  50. resultSet=list.toArray(resultSet);
  51. returnresultSet;
  52. }
  53. /**
  54. *得到给定的操作中出现的所有操作符排列情况
  55. *
  56. *@paramoperators
  57. *出现的操作符数组
  58. *@paramnumber
  59. *每组操作符的数量
  60. *@return所有操作符排列数组
  61. */
  62. privatestaticchar[][]arrangeAllOperators(char[]operators,intnumber){
  63. intsetSize=(int)Math.pow(operators.length,number);
  64. intindex=0;
  65. char[][]resultSet=newchar[setSize][number];
  66. for(inti=0;i<operators.length;i++){
  67. for(intj=0;j<operators.length;j++){
  68. for(intk=0;k<operators.length;k++){
  69. resultSet[index][0]=operators[i];
  70. resultSet[index][1]=operators[j];
  71. resultSet[index][2]=operators[k];
  72. index++;
  73. }
  74. }
  75. }
  76. returnresultSet;
  77. }
  78. /**
  79. *根据给定的一组整数,通过加减乘除运算,得到想要的结果,如果可以得到结果,则返回所有可能的结果的运算形式。
  80. *返回的运算形式,均按从左到右的顺序计算,并不是遵循四则运算法则,比如:<br>
  81. *输出的结果形式为:<br>
  82. *1*8-6*12=24<br>
  83. *表示的运算顺序是:<br>
  84. *1:1*8=8,<br>
  85. *2:8-6=2,<br>
  86. *3:2*12=24<br>
  87. *而不是按照四则运算法则计算:<br>
  88. *1:1*8=8,<br>
  89. *2:6*12=72,<br>
  90. *3:8*72=576<br>
  91. *
  92. *
  93. *@paramnumbers
  94. *给定进行运算的一组整数,4个数为一组
  95. *@paramtargetNumber
  96. *想要得到的结果
  97. *@return所有可能得到想要的结果的所有运算形式的字符串形式集合
  98. *@throwsException
  99. *如果不能得到想要的结果,则抛出该异常,表明根据指定的一组数字通过一系列的加减乘除不能得到想要的结果
  100. */
  101. publicstaticSet<String>caculate(int[]numbers,inttargetNumber)
  102. throwsException{
  103. Set<String>resultSet=newHashSet<String>();//这里用Set而不是用List,主要是因为当给定的一组数字中如果有重复数字的话,同一结果会被出现多次,如果用List存放的话,会将重复的结果都存放起来,而Set会自动消除重复值
  104. char[][]operatorsArrangement=arrangeAllOperators(newchar[]{'+',
  105. '-','*','/'},3);
  106. int[][]numbersArrangement=arrangeAllNumbers(numbers);
  107. for(int[]nums:numbersArrangement)
  108. for(char[]operators:operatorsArrangement){
  109. intresult=0;
  110. try{
  111. result=caculate(nums,operators);
  112. }catch(Exceptione){//出现非精确计算
  113. continue;
  114. }
  115. if(result==targetNumber)
  116. resultSet.add(buildString(nums,operators,targetNumber));//如果计算后的结果等于想要的结果,就存放到集合中
  117. }
  118. if(resultSet.isEmpty())
  119. thrownewException("给定的数字:"+Arrays.toString(numbers)
  120. +"不能通过加减乘除运算得到结果:"+targetNumber);
  121. returnresultSet;
  122. }
  123. /**
  124. *将一组整型数字以给定的操作符按顺序拼接为一个完整的表达式字符串
  125. *
  126. *@paramnums
  127. *一组整型数字
  128. *@paramoperators
  129. *一组操作符
  130. *@paramtarget
  131. *目标值
  132. *@return拼接好的表达式字符串
  133. */
  134. (int[]nums,char[]operators,inttarget){
  135. Stringstr=String.valueOf(nums[0]);
  136. for(inti=0;i<operators.length;i++){
  137. str=str+''+operators[i]+''+nums[i+1];
  138. }
  139. str=str+"="+target;
  140. returnstr;
  141. }
  142. /**
  143. *将给定的一组数字以给定的操作符按顺序进行运算,如:intresult=caculate(newint[]{3,4,5,8},new
  144. *char[]{'+','-','*'});
  145. *
  146. *@paramnums
  147. *一组数字
  148. *@paramoperators
  149. *一组运算符,数量为数字的个数减1
  150. *@return最后的计算结果
  151. *@throwsException
  152. *当计算结果不精确时,抛出该异常,主要是针对除法运算,例如18/8=2,诸如这样不精确计算将抛出该异常
  153. */
  154. privatestaticintcaculate(int[]nums,char[]operators)throwsException{
  155. intresult=0;
  156. for(inti=0;i<operators.length;i++){
  157. if(i==0){
  158. result=caculate(nums[i],nums[i+1],operators[i]);
  159. }else{
  160. result=caculate(result,nums[i+1],operators[i]);
  161. }
  162. }
  163. returnresult;
  164. }
  165. /**
  166. *根据指定操作符将两个给定的数字进行计算
  167. *
  168. *@paramnum1
  169. *数字1
  170. *@paramnum2
  171. *数字2
  172. *@paramoperator
  173. *操作符,只能从“+、-、*、/”4个操作符中取值
  174. *@return计算结果
  175. *@throwsException
  176. *当计算结果不精确时,抛出该异常,主要是针对除法运算,例如18/8=2,诸如这样不精确计算将抛出该异常
  177. */
  178. privatestaticintcaculate(intnum1,intnum2,charoperator)
  179. throwsException{
  180. doubleresult=0;
  181. switch(operator){//根据操作符做相应的计算操作
  182. case'+':
  183. result=num1+num2;
  184. break;
  185. case'-':
  186. result=num1-num2;
  187. break;
  188. case'*':
  189. result=num1*num2;
  190. break;
  191. case'/':
  192. result=(double)num1/(double)num2;
  193. break;
  194. }
  195. if(!check(result))
  196. thrownewException("不精确的计算数字");
  197. return(int)result;
  198. }
  199. /**
  200. *检查指定的浮点数是否可以直接转换为整型数字而不损失精度
  201. *
  202. *@paramresult
  203. *要检查的浮点数
  204. *@return如果可以进行无损转换,返回true,否则返回false
  205. */
  206. privatestaticbooleancheck(doubleresult){
  207. Stringstr=String.valueOf(result);
  208. intpointIndex=str.indexOf(".");//小数点的下标值
  209. Stringfraction=str.substring(pointIndex+1);
  210. returnfraction.equals("0")?true:false;//通过判断小数点后是否只有一个0来确定是否可以无损转换为整型数值
  211. }
  212. /**
  213. *对传入的整型数组buf进行全排列
  214. *
  215. *@parambuf
  216. *要进行全排列的整型数组
  217. *@paramstart
  218. *开始的下标值
  219. *@paramend
  220. *结束下标值
  221. *@paramlist
  222. *保存最后全排列结果的集合
  223. */
  224. privatestaticvoidallSort(int[]buf,intstart,intend,List<int[]>list){
  225. if(start==end){//当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
  226. int[]a=newint[buf.length];
  227. System.array(buf,0,a,0,a.length);
  228. list.add(a);
  229. }else{//多个字母全排列
  230. for(inti=start;i<=end;i++){
  231. inttemp=buf[start];//交换数组第一个元素与后续的元素
  232. buf[start]=buf[i];
  233. buf[i]=temp;
  234. allSort(buf,start+1,end,list);//后续元素递归全排列
  235. temp=buf[start];//将交换后的数组还原
  236. buf[start]=buf[i];
  237. buf[i]=temp;
  238. }
  239. }
  240. }
  241. }

8. 想用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点是可以交换顺序的,如果须要可以再写一个全排列的算法。

9. JAVA扑克牌凑24点的游戏

要高手才能改

10. 求JAVA 24点游戏算法,界面和发牌器已弄好,求算法

package;

importjava.util.Scanner;

publicclassGame21
{
publicstaticvoidmain(String[]args)
{
System.out.println("游戏规则:");
System.out.println("开始游戏后屏幕上将出现一个随机数字");
System.out.println("按a可以随机增加一个1-10范围内的一个数字");
System.out.println("按s则揭晓你和电脑对决的结果!");
System.out.println("如果你的数字大于21点,则游戏直接结束!");
intrand=(int)(Math.random()*10+1);
intsum1=rand,sum2=0,which=0;//0电脑,1我
System.out.println("系统先给出1个数字:"+rand);
System.out.print("轮到你了:");
Scannerscanner=newScanner(System.in);
while(true)
{
Stringinput="";
if(which==0)
{
input=scanner.next();
}
if(which==0&&"a".equals(input))
{
which=1;
intr=(int)(Math.random()*10+1);
sum2+=r;
System.out.println("您抽到的数字是:"+r);
if(sum2>21)
{
System.out.println("你的点数为"+sum2+",超过了最大限制21点,游戏提前结束!");
break;
}
System.out.print("该电脑了:a ");
}
elseif(which==1)
{
which=0;
intr=(int)(Math.random()*10+1);
sum1+=r;
System.out.println("电脑抽到的数字是:"+r);
if(sum1>21)
{
System.out.println("电脑点数为"+sum1+",超过了最大限制21点,游戏提前结束!");
break;
}
System.out.print("轮到你了:");
}
elseif(which==0&&"s".equals(input))
{
System.out.println("电脑共有:"+sum1+"点");
System.out.println("你共有:"+sum2+"点");
if(sum1>sum2)
{
System.out.println("YOULOSE!!!");
}
elseif(sum1<sum2)
{
System.out.println("YOUWIN!!!");
}
else
{
System.out.println("DRAWGAME!!!");
}
System.out.print("轮到你了:");
which=0;
}
}
scanner.close();
}
}

阅读全文

与24点游戏java相关的资料

热点内容
电脑怎么找到云服务器 浏览:871
微信怎么发应用app 浏览:776
花生壳dns服务器地址 浏览:648
squad服务器一般什么时候人多 浏览:479
程序员战门课 浏览:474
config保存服务器地址 浏览:317
预订网吧座位的app叫什么 浏览:416
香港服务器主机地址 浏览:640
网店美工pdf 浏览:447
一堆文件夹怎么弄出来 浏览:743
博途如何编译硬件 浏览:418
fortran程序pdf 浏览:504
电池消耗算法 浏览:394
服务器中断连接怎么处理 浏览:222
上世纪互联网不发达程序员很难 浏览:841
语音识别android开源 浏览:762
地埋式垃圾压缩中转站 浏览:902
apachehttpdlinux 浏览:944
快递员中通app预付款是什么 浏览:843
java路径转义 浏览:857