我刚刚开始一场战争纸牌游戏。我制作了一副牌,然后需要重新洗牌。无论如何,我将需要.length以后更正。因此,当我使用长度时,当我计算它应该是52时它得到160作为答案。这是代码。
import java.util.List;
public class wargame {
public static void main(String args[]){
funtionsforwargame funtionsforwargame=new funtionsforwargame();
funtionsforwargame.instructions();
List<String> randomized_deck=funtionsforwargame.get_deck();
String[] deck_convert = randomized_deck.toArray(new String[0]);
String deck=java.util.Arrays.toString(deck_convert);
System.out.println(deck);
System.out.println(deck.length());
}
}
此外,get_deck方法创建一个deck作为数组,然后转换为列表字符串并返回它,因为它需要被洗牌。所以我然后把那个返回的列表转换回一个数组,然后确保它给了我甲板,而不是像ghty @ 67843那样的东西。因此,如果有人知道为什么它给我160而不是52,那将是有帮助的。这是get_deck方法,以防你不相信我在列表/数组中有52个。
public class funtionsforwargame {
public void instructions(){
System.out.println("Welcome to War!");
System.out.println("The object of the game is to collect all cards from the other person.");
System.out.println("Basically, all you do is watch and press any key to go to the next round.");
System.out.println("Good luck!!!");
}
public List<String> get_deck(){
String new_deck[]={"A","A","A","A","2","2","2","2","3","3","3","3","4","4","4","4","5","5","5","5","6","6","6","6","7","7","7","7","8","8","8","8","9","9","9","9","10","10","10","10","J","J","J","J","Q","Q","Q","Q","K","K","K","K"};
List<String> list_convert = Arrays.asList(new_deck);
Collections.shuffle(list_convert);
return list_convert;
}
}
提前致谢。
答案 0 :(得分:3)
int[] x = {1,2,3};
和
String y = Arrays.toString(x);
完全不同的东西?
当你做
时System.out.println(deck);
System.out.println(deck.length());
长度计算逗号,空格和[],为什么它们与 randomized_deck
中的52张卡相同你根本就不习惯使用数组,在你的情况下,列表还不错......
只是做
List<String> randomized_deck = funtionsforwargame.get_deck();
System.out.println(randomized_deck);
要分割,您可以使用List#subList(....);
答案 1 :(得分:2)
当你这样做时:
String deck=java.util.Arrays.toString(deck_convert);
52
号变为160
,因为卡10 为2
位数。
您应该打印String[] deck_convert
而不是Arrays.toString(deck_convert)
。
public class wargame {
public static void main(String args[]) {
funtionsforwargame funtionsforwargame = new funtionsforwargame();
funtionsforwargame.instructions();
List<String> randomized_deck = funtionsforwargame.get_deck();
String[] deck_convert = randomized_deck.toArray(new String[0]);
for(String card : deck_convert)
System.out.print(card + ",");
System.out.println(deck_convert.length);
}
}
答案 2 :(得分:1)
这是真的,你可以在这个地方检查:
public class wargame {
public static void main(String args[]) {
funtionsforwargame funtionsforwargame = new funtionsforwargame();
funtionsforwargame.instructions();
List<String> randomized_deck = funtionsforwargame.get_deck();
String[] deck_convert = randomized_deck.toArray(new String[0]);
String deck = java.util.Arrays.toString(deck_convert);
System.out.println(deck_convert.length);
System.out.println(deck);
System.out.println(deck.length());
}
}
<强>输出:强>
52
[10, 2, 8, 3, A, 7, 2, 6, 8, 6, 4, K, 3, 9, 10, 3, 5, J, Q, 7, 5, 8, K, J, J, 3, A, 7, 7, 9, 2, 6, K, 4, Q, 9, A, Q, 10, A, 9, 5, K, Q, 5, 6, 8, 10, J, 4, 4, 2]
160
在第一种情况下,length
为您提供数组元素的数量。在另一个中,您将获得已转换字符串的实际长度。
我们可以通过以下方式轻松检查:
public static void main(String args[]) {
funtionsforwargame funtionsforwargame = new funtionsforwargame();
funtionsforwargame.instructions();
List<String> randomized_deck = funtionsforwargame.get_deck();
String[] deck_convert = randomized_deck.toArray(new String[0]);
String deck = java.util.Arrays.toString(deck_convert);
System.out.println(deck_convert.length);
System.out.println(deck);
System.out.println(deck.charAt(4)); // here we take one symbol
System.out.println(deck.length());
}
<强>输出:强>
52
[A, 7, 2, A, 2, 2, 5, A, 3, 8, 6, Q, 10, 4, K, 4, J, 3, 5, 7, 8, Q, 7, 5, 10, K, 10, 4, 9, 6, Q, 8, K, 7, 6, 9, J, J, 10, K, 5, 2, 8, 6, J, Q, 3, 4, 9, A, 3, 9]
7
160
我们可以看到命令System.out.println(deck.charAt(4));
给了我们**7**
。