import java.util.Random;
import java.util.Stack;
public class Blackjack {
public static void main(String[] args) {
int cardValue; /* card value is from 2 to 11 */
Stack Player1 = new Stack();
Stack Addition = new Stack();
Random r = new Random();
int i = 2 + r.nextInt(11);
System.out.println("Welcome to Mitchell's blackjack program!");
for (int a = 1; a <= 2; a++) { /* Start's the game by assigning 2 cards each, to the players */
Player1.push(i);
}
while (!Player1.empty()) {
System.out.print("You get a " + Player1.pop());
System.out.print("and");
int sum = 0;
for (int n = 0; n < Player1.size(); n++) {
sum = sum + Player1.pop();
System.out.print("Your total is " + sum);
}
}
}
}
所以我刚开始学习 java,我正在努力完成这个 BlackJack project但是,当我尝试使用 javac 进行编译时,对于“sum = sum + Player1.pop();”,二元运算符“+”的输出是错误的操作数类型
我在上面编码中使用的解决方案来自 here
最佳答案
Player1.pop()
返回一个 Object
,因为您使用了 Stack
而没有提供类型。你不能做 int + Object
。如果您需要在 Stack 中存储 int
,只需使用泛型并执行
Stack<Integer> Player1 = new Stac<Integer>k();
Stack<Integer> Addition = new Stack<Integer>();
还有你的
System.out.print("Your total is " + sum);
应该在 for 之外,否则你会得到一笔临时款项
关于java - 添加 Stack.pop() 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31051662/