java - 为什么我在 Eclipse 中收到这些错误?

标签 java

我正在编写一个代码,从用户那里获取卡片并将其放入一副牌中,卡片的值(value)取决于数字,例如1 然后 5 将是红心 5,3 然后 13 将是黑桃 K。如果卡插入正确,它应该返回 true,如果没有插入,它应该抛出异常,它应该检查牌组中的空间,如果没有,它应该返回 false,最后,如果卡已经插入,它应该返回 false。错误在评论中。

import java.util.Scanner;


public static void main(String[] args){ //red underline error on void saying "syntax error token", under the second bracket of String[] and on the last bracket after args.

private static addCards(){

String suit[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
String value[] = {"ZZZZ", "Ace", "2", "3", "4", "5", "6", 
                  "7", "8", "9", "10", "Jack", "Queen", "King"};


String[] card = new String[52];  
String[] newSuit = new String[4];

Scanner input = new Scanner(System.in); //it says it expects a { instead of the semicolon here.

for (int i = 0; i < 52; i++){
    System.out.println("Please enter a suit");
    int inputSuit = input.nextInt();

    check = false;
    if(inputSuit = 1 || 2 || 3 || 4 ){
        check = true;
    }

    System.out.println("Please enter a card");
    int inputValue = input.nextInt();

    check1 = false;
    if(inputValue = 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13){
        check1 = true;
    }

    try{
        check = true;
        check1 = true;
    }
    catch(card exception){
        ex.printStackTrace();
          System.exit(1);
    }

    switch (inputSuit) {
        case 0: newSuit[i] = suit[0]; break;
        case 1: newSuit[i] = suit[1]; break;
        case 2: newSuit[i] = suit[2]; break;
        case 3: newSuit[i] = suit[3]; break;
    }

    switch (inputValue) {
        case 1: card[i] = value[1]; break;
        case 2: card[i] = value[2]; break;
        case 3: card[i] = value[3]; break;
        case 4: card[i] = value[4]; break;
        case 5: card[i] = value[5]; break;
        case 6: card[i] = value[6]; break;
        case 7: card[i] = value[7]; break;
        case 8: card[i] = value[8]; break;
        case 9: card[i] = value[9]; break;
        case 10: card[i] = value[10]; break;
        case 11: card[i] = value[11]; break;
        case 12: card[i] = value[12]; break;
        case 13: card[i] = value[13]; break;
    }



    boolean isFull = true;
    for(String s : card) {
        if(s == null) {
            isFull = false;
            break;
        }
    }

}
} //"multiple markers in this line"

最佳答案

您错误地使用了||

    if(inputSuit = 1 || 2 || 3 || 4 ){

这样做

    if(inputSuit == 1 || inputSuit == 2 || inputSuit == 3 || inputSuit == 4 ){

|| 用于进行 boolean 逻辑或,一般用法是

((Boolean Expression 1) || (Boolean Expression 2) || (Boolean Expression 3)...)

同样纠正这个问题

if(inputValue = 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13){

关于java - 为什么我在 Eclipse 中收到这些错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20103424/

相关文章:

java - 创建集群需要 InstanceProfile

Java程序在数组列表中查找众数

java - 为什么 3 种算法的平均时间是负数?

Java SimpleDateFormat 接受无效日期。使用格式 'MM/dd/yyyy' 解析 '01/01/2021anything'

java - OKTA 多重身份验证 + Java + RestAssured :/login/step-up/redirect always returns 403 through RESTAssured

java - java中的通用参数类型匹配

java - 如何读取文本文件并将其保存到数组中?

java - 如何在 WSS4J 2.2.3 中启用 RSA15 传输算法?

java - 没有任何注释和xml文件的Spring XD模块

java - 为什么 Closure-maven-plugin 没有选择正确的 Java 版本?