2013-11-20 42 views
0

我正在制作一个代码,它从用户那里接收卡片并将它们放入卡片组中,卡片的值取决于数字,例如1然后5将是5的心3然后13将是黑桃王。如果卡插入正确,它应该返回true,否则应该抛出异常,应该检查卡组中的空间,如果没有,应该返回false,最后如果卡已经插入,它应该返回false。错误在评论中。为什么我在Eclispe中遇到这些错误?

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" 
+1

您不能在Java中的方法中使用方法。 –

+1

你的班级名称是什么? – Pshemo

回答

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

你缺少一个public class [ClassName] {}

1

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

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

0
  1. 没有公共类YourClassName {}
  2. 主要方法有其他方法? java中不允许这样做
  3. 您的方法没有返回类型addCards()
  4. inputSuit = 1 || 2 || 3 || 4不正确。您需要分别检查每个数字,==是比较运算符。 =用于分配。
  5. 阅读上写异常catch块here
0

你在你的代码更然后一个错误。

  1. Class定义。
  2. main方法内的另一种方法。
  3. ||运算符的使用无效。检查@Ankit答案。使用=代替===assignment运营商,您需要的是comparison运营商,即==
  4. 真的很差的缩进。
相关问题