gpt4 book ai didi

java - 使用用户输入的数字重复 for 循环

转载 作者:行者123 更新时间:2023-12-02 04:14:20 24 4
gpt4 key购买 nike

有一个程序,当按下回车按钮时,它能够从标准扑克牌中发牌。一切正常,但我需要添加功能来询问用户从一开始他们想要使用多少套牌。我有一个扫描仪,它接受该输入并将其设置为变量。

Scanner scanner = new Scanner(System.in);
int numberOfDecks = scanner.nextInt();

然后这就是我用每张卡填充 ArrayList 的方式

          for (int i = 0; i < deck.length; i++) {
deck[i] = faces[i % 13] + suit[i/13];
}

我实现此功能的想法是将 for 循环嵌套在另一个循环中,该循环将运行循环与 numberOfDecks 的值一样多的次数。

for (int count = 0; count <= numberOfDecks; count++){
for (int i = 0; i < deck.length; i++) {
deck[i] = faces[i % 13] + suit[i/13];
} //Creates array with all possible cards in standard deck of cards
}

我尝试这样做,但由于某种原因,无论 numberOfDecks 的值如何,count 永远不会解析为大于 0。然后,我得到一个大小正确的 ArrayList,但前 52 个条目之外的每个条目都是空白,因为循环不会超过一次。有人能看到我做错了什么吗?

编辑:为了澄清起见,这里是整个程序。

public class Card {

public static void main(String[] args) {
System.out.println("How many decks would you like to use?");
Scanner scanner = new Scanner(System.in);
int numberOfDecks = scanner.nextInt();
String[] suit = {" of Diamonds", " of Spades", " of Hearts", " of Clubs"}; //Array of suits
String[] faces = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};//Array of face values
String[] deck = new String[52 * numberOfDecks];//Array of actual deck
boolean deckComplete = false;//Boolean for finished deck
int[] random = new int[52]; //Array with all possible numbers between 1-52

for (int x = 0; x<random.length; x++) {
random[x] = x;
} //Fills array with all possible numbers between 1-52

Random rndNum = new Random();

for (int count = 0; count <= numberOfDecks; count++){
for (int i = 0; i < deck.length; i++) {
deck[i] = faces[i % 13] + suit[i/13];
} //Creates array with all poassible cards in standard deck of cards
}
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(deck)); //Converts above array into ArrayList

while (deckComplete == false) {
for (int i = 52; i >= 1; i--) {
String readString = scanner.nextLine();
int randomNumber = rndNum.nextInt(i);
if (readString.equals("")) {
System.out.println(arrayList.get(random[randomNumber]));
arrayList.remove(random[randomNumber]);
if (i == 1) {
deckComplete = true;
System.out.println("You are out of cards!");
} //Deals out random card from deck and removes each one used
}
}

}
}
}

最佳答案

看看我对代码的评论(我已经删除了你的评论)。我认为这就是你的问题所在,

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
public class Card {

public static void main(String[] args) {

System.out.println("How many decks would you like to use?");
Scanner scanner = new Scanner(System.in);
int numberOfDecks = scanner.nextInt();
String[] suit = {" of Diamonds", " of Spades", " of Hearts", " of Clubs"};
String[] faces = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
String[] deck = new String[52 * numberOfDecks];
boolean deckComplete = false;
//random must have the correct number of elements
int[] random = new int[52 * numberOfDecks];

for (int x = 0; x<random.length; x++) {
random[x] = x;
}

Random rndNum = new Random();

int i = 0;

for (int count = 0; count <= numberOfDecks; count++){

for (i = i; i < deck.length; i++) {

System.out.println("i is " + i);
//you were doing the same for suit (i/13), but it should be as below
deck[i] = faces[i % 13] + suit[i % 4];
}

//otherwise you are overiding the same values over and over again for each loop
i = i+52;
}

ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(deck));

while (deckComplete == false) {

//depending of number of decks user selects it has more than 52 cards
for (int j = deck.length; j >= 1; j--) {

//so the user know what to do
System.out.println("Just hit enter to contine");
String readString = scanner.nextLine();
int randomNumber = rndNum.nextInt(j);

if (readString.equals("")) {

System.out.println(arrayList.get(random[randomNumber]));
arrayList.remove(random[randomNumber]);

if (j == 1) {

deckComplete = true;
System.out.println("You are out of cards!");
}
}
}
}
}
}

关于java - 使用用户输入的数字重复 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33469206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com