为什么我在Eclipse中遇到这些错误?

时间:2013-11-20 17:59:04

标签: java

我正在制作一个代码,该代码从用户那里取出卡片并将它们放入卡片组中,卡片的价值取决于数字,例如1然后5将是5心3然后13将是黑桃王。如果卡正确插入它应该返回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"

5 个答案:

答案 0 :(得分:2)

您正确使用||

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

这样做

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

||用于执行布尔OR,一般用法是

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

同样纠正这个

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

答案 1 :(得分:1)

您错过了public class [ClassName] {}

答案 2 :(得分:1)

我害怕有很多错误。您尚未定义一个类,该类应与文件名对应。您开始一个主方法,但在开始另一个方法之前不要关闭它的括号。你的if语句无效。

也许您应该花些时间学习初学者Java书籍或在线教程,然后在更好地理解Java语法后再回到这段代码。

答案 3 :(得分:0)

  1. 没有公共类YourClassName {}
  2. 主要方法有另一种方法吗?这在java
  3. 中是不允许的
  4. 您的方法addCards()
  5. 没有返回类型
  6. inputSuit = 1 || 2 || 3 || 4是不正确的。您需要单独检查每个数字,==是比较运算符。 =是用于分配。
  7. 阅读编写异常捕获块here

答案 4 :(得分:0)

您的代码中有多个错误。

  1. 未定义Class
  2. main方法中的另一种方法。
  3. 无效使用||运算符。检查@Ankit回答。
  4. 使用=代替===assignment运算符,您需要的是comparison运算符,即==
  5. 压痕真的很差。