我最近开始研究个人项目,以测试我在Java中的(糟糕)技能。这是一款名为“Go Fish!”的基本Java Card游戏。我目前只使用基本的控制台输入/输出来制作游戏。但每当我编译类MainClass时,编译器都会给我“未检查或不安全”操作警告。我一直在互联网上寻找解决方案,并尝试了很多,但它一直给我这个错误。
关于如何摆脱这个问题的任何想法?
代码:Class MainClass
import java.util.*;
public class MainClass
{
//Playing states
public static final int PLAYING = 0;
public static final int OVER = 1;
//Chance states
public static final int PLAY = 0;
public static final int COMP = 1;
//Win conditions.
public static final int PLAY_WIN = 0;
public static final int COMP_WIN = 1;
//Values used in the game, VERY IMPORTANT
public static int currentState, currentPlayer;
public static int winner;
public static ArrayList<String> Player_Cards;
public static ArrayList<String> Comp_Cards;
public static ArrayList<String> Deck_Cards;
public MainClass() {
}
public static void main(String[] args) {
//Start Game. This action is carried out EVERYTIME the game is started.
initGame(); //Serves function for "Initializing" the game i.e. sorting cards, distributing, identifying.
}
public static void initGame() {
//Goto Shuffler, get a shuffled deck, return here.
Shuffler shuffle = new Shuffler();
Deck_Cards = new ArrayList<String>(shuffle.doShuffle()); //For safe usage.
Player_Cards = new ArrayList<String>();
Comp_Cards = new ArrayList<String>();
//Give player cards
for(int i = 0; i < 5; i++) {
int c = Deck_Cards.size() - 1 - i;
Player_Cards.add(Deck_Cards.get(c));
Deck_Cards.remove(c);
}
//Give computer cards
for(int i = 0; i < 5; i++) {
Comp_Cards.add(Deck_Cards.get(i));
Deck_Cards.remove(i);
}
System.out.println("Darp");
}
}
代码:Class Shuffler:
import java.util.*;
public class Shuffler
{
public enum Suits {HEARTS, DIAMONDS, CLUBS, SPADES};
public enum Ranks {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
public Shuffler() {
}
public static ArrayList doShuffle() {
//Initialize cards
int count = 0;
ArrayList<String> deck = new ArrayList<String>();
for(int i = 0; i < 4; i++) {
for(int y = 0; y < 13; y++) {
deck.add("Card is " + Ranks.values()[y] + " of " + Suits.values()[i]);
count++;
}
}
Collections.shuffle(deck);
return deck;
}
}
其他信息:
我在这个项目中使用BlueJ
答案 0 :(得分:1)
这是因为您为ArrayList
使用原始doShuffle
。本地变量已正确参数化,因此将<String>
添加到返回类型应该可以解决问题。
答案 1 :(得分:1)
您正在使用doShuffle
方法的原始类型,因此编译器会抱怨缺少类型安全性。取代
public static ArrayList doShuffle() {
与
public static List<String> doShuffle() {
使用Shuffler
的实例访问此方法,以便可以省略static
关键字
答案 2 :(得分:0)
这是因为doShuffle方法返回一个通用的ArrayList。
在main中,您调用该方法将结果放在ArrayList中,以便编译器建议您取消选中此转换。
您可以修改返回ArrayList的方法,也可以在main中使用通用的ArraList。
PS。第一个答案!的xD