Java 甲板交易

Java 甲板交易,java,methods,Java,Methods,我正在尝试为类编写代码,该类执行以下操作: 询问要使用多少甲板 每次按enter键时,程序从卡组生成一张未使用的卡 当没有更多的卡可以交易时,程序会通知用户 程序允许用户再次播放 方法被广泛使用 到目前为止,我的代码如下所示: import java.util.*; public class IA3 { @SuppressWarnings({ }) public static void play () { } @SuppressWarnings("resource") public sta

我正在尝试为类编写代码,该类执行以下操作:

  • 询问要使用多少甲板
  • 每次按enter键时,程序从卡组生成一张未使用的卡
  • 当没有更多的卡可以交易时,程序会通知用户
  • 程序允许用户再次播放
  • 方法被广泛使用
到目前为止,我的代码如下所示:

import java.util.*;
public class IA3 {

@SuppressWarnings({ })
public static void play () {
}
@SuppressWarnings("resource")
public static void main(String[] args) {
    boolean draw = true;
    boolean pa = true;
    Scanner console = new Scanner(System.in);
    // TODO Auto-generated method stub
    System.out.println("Hello! Please input the number of decks you would like to use:");
    int decks = console.nextInt();

    int t = decks * 52;
    System.out.println("Your total amount of cards to use are " +t);
    int a = 0;
    do {
        int[] deck = new int[t];
        //declaration of suits and ranks
        String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
        String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

        //Initializing of the cards
        for (int i = 0; i < t; i++) {
        deck[i] = i;
                }

            System.out.println("Press enter to draw a random card : "); //allows player to prompt the system for the next card

            String input = console.nextLine();
            a++;
            if (input.equals("")) {
                // Shuffles the cards
                for (int i = 0; i < 1; i++) {
                  int index = (int)(Math.random() * t);
                  int temp = deck[i];
                  deck[i] = deck[index];
                  deck[index] = temp;
                }

                      for (int i = 0; i < 1; i++) {
                      String suit = suits[deck[i] / 13];
                      String rank = ranks[deck[i] % 13];
                      System.out.println(rank + " of " + suit);
                      }
             if (a>t) {
                 System.out.println ("No more cards available");
                 break;
             }
                      System.out.println("Draw another card? (Yes or No) : ");
                      String again = console.nextLine();
                      if (again.equals("Yes")) {
                          continue;
                      }
                      if (again.equals("No")) {
                          draw = false;
                          System.out.println("Game over!");
                      }
                        System.out.println("Play again? (Yes or No) : ");
                          String plag = console.nextLine();
                          if (plag.equals("Yes")) {
                              continue;
                          }
                          if (plag.equals("No")) {
                              pa = false;
                              System.out.println("Thank you for playing!");
                              break;
                          } while (pa == true);
                  }
        } while (draw == true);
    }
}
for (int i = 0; i < 1; i++) {
 // code
}
import java.util.*;
公共类IA3{
@抑制警告({})
公共静态无效播放(){
}
@抑制警告(“资源”)
公共静态void main(字符串[]args){
布尔绘制=真;
布尔pa=真;
扫描仪控制台=新扫描仪(System.in);
//TODO自动生成的方法存根
System.out.println(“您好!请输入您想要使用的甲板数量:”);
int decks=console.nextInt();
int t=甲板*52;
System.out.println(“您要使用的卡总量为”+t);
int a=0;
做{
int[]甲板=新int[t];
//诉讼及职级的宣布
字符串[]套装={“黑桃”、“红桃”、“钻石”、“梅花”};
字符串[]排名={“王牌”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“杰克”、“女王”、“国王”};
//卡片的初始化
for(int i=0;it){
System.out.println(“没有更多可用卡”);
打破
}
System.out.println(“抽另一张卡?(是或否):”;
字符串再次=console.nextLine();
如果(同样,等于(“是”)){
继续;
}
如果(同样,等于(“否”)){
绘制=假;
System.out.println(“游戏结束!”);
}
System.out.println(“再次播放”(是或否):”;
字符串plag=console.nextLine();
如果(永久等于(“是”)){
继续;
}
如果(永久等于(“否”)){
pa=假;
System.out.println(“谢谢你玩!”);
打破
}而(pa==true);
}
}while(draw==true);
}
}

当我尝试运行超过1套时会出现问题,有时会立即失败,有时可能运行4个交易然后失败。我似乎也无法让它再次运行;只要我输入“是”,它就会终止,而不是再次播放。正如你所看到的,我在那里没有方法(我对方法非常迷恋)。任何帮助都将不胜感激,谢谢

您应该发布完整的错误消息。没有它,很难猜测发生了什么。我至少看到一个数组索引可以超出边界的地方:

String suit = suits[deck[i] / 13];
如果您有超过1个甲板,则此分割的结果将大于3。您可以使用以下方法修复它:

String suit = suits[(deck[i] / 13) % 4];

通过这种方式,您可以确保不只是使用索引为[0,1,2,3]的元素。

还不能发表评论,因此这里有一些想法可以让代码更容易理解,特别是当您的问题需要“任何帮助”时

删除如下所示的循环:

import java.util.*;
public class IA3 {

@SuppressWarnings({ })
public static void play () {
}
@SuppressWarnings("resource")
public static void main(String[] args) {
    boolean draw = true;
    boolean pa = true;
    Scanner console = new Scanner(System.in);
    // TODO Auto-generated method stub
    System.out.println("Hello! Please input the number of decks you would like to use:");
    int decks = console.nextInt();

    int t = decks * 52;
    System.out.println("Your total amount of cards to use are " +t);
    int a = 0;
    do {
        int[] deck = new int[t];
        //declaration of suits and ranks
        String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
        String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

        //Initializing of the cards
        for (int i = 0; i < t; i++) {
        deck[i] = i;
                }

            System.out.println("Press enter to draw a random card : "); //allows player to prompt the system for the next card

            String input = console.nextLine();
            a++;
            if (input.equals("")) {
                // Shuffles the cards
                for (int i = 0; i < 1; i++) {
                  int index = (int)(Math.random() * t);
                  int temp = deck[i];
                  deck[i] = deck[index];
                  deck[index] = temp;
                }

                      for (int i = 0; i < 1; i++) {
                      String suit = suits[deck[i] / 13];
                      String rank = ranks[deck[i] % 13];
                      System.out.println(rank + " of " + suit);
                      }
             if (a>t) {
                 System.out.println ("No more cards available");
                 break;
             }
                      System.out.println("Draw another card? (Yes or No) : ");
                      String again = console.nextLine();
                      if (again.equals("Yes")) {
                          continue;
                      }
                      if (again.equals("No")) {
                          draw = false;
                          System.out.println("Game over!");
                      }
                        System.out.println("Play again? (Yes or No) : ");
                          String plag = console.nextLine();
                          if (plag.equals("Yes")) {
                              continue;
                          }
                          if (plag.equals("No")) {
                              pa = false;
                              System.out.println("Thank you for playing!");
                              break;
                          } while (pa == true);
                  }
        } while (draw == true);
    }
}
for (int i = 0; i < 1; i++) {
 // code
}
for(int i=0;i<1;i++){
//代码
}
上面的for循环将i设置为0,然后执行内部代码,然后增加i(因此i现在等于1),然后检查i是否小于1(这不是因为它现在等于1),因此它不会再次执行内部代码。换句话说,这种类型的for循环是不必要的,因为它只能执行其内部代码一次。应移除环绕的for循环,并保持内部代码完好无损

将数据组存储为ArrayList而不是数组可能更简单。这样做的好处是,您可以利用诸如.remove(int)和.size()之类的操作,而不必担心洗牌(您可以随机删除一个条目并打印出来)

希望这能为你指明正确的方向。坚持下去


这里有一个关于创建方法的有用教程,我通过谷歌搜索“java方法”找到了它

那部分起作用了;非常感谢。您是否知道如何输入方法,并使play-reach函数正常工作?请正确格式化您的代码。很难阅读。对不起,我对整个编码过程都很在行。如果您使用的是eclipse,请搜索web“如何在(您的文本编辑器或IDE)中格式化代码”以重新格式化代码,请参阅:(然后更新您的帖子,使其更具可读性)