java - 添加 Stack.pop() 的输出

标签 java

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/

相关文章:

java - 在 Spring Boot 中通过 @Value 检索 application.properties 值

java - 发送前先阅读回复

java - H2 内存 DB : Set Timezone with JDBC? Java 单元测试

java - 如何使应用程序同时兼容 Java 1.5 和 1.6?

java - 在 Java FX 工作线程中不断更新 UI

java - 如何以编程方式检查 JVM 上是否启用了 FIPS

java - Guava null 测试器不断抛出错误

java - 如何更改 JAX-WS webservice 的地址位置

java - Ant 构建不包含 Android 支持库 jar

java - 使用预定义 key 锁定的竞争条件解决方案