When I run:
import java.util.Scanner;
public class War {
public static void main(String[] args) {
String[] deckcards=deck();
String[] player1cards=player1Cards(deckcards);
}
public static String[] deck(){
String[] Cards = {"spades1","spades2","spades3", "spades4", "spades5", "spades6", "spades7", "spades8", "spades9", "spades10", "spadesJ", "spadesQ", "spadesK", "spadesA", "clubs1", "clubs2", "clubs3", "clubs4", "clubs5", "clubs6", "clubs7", "clubs8", "clubs9", "clubs10", "clubsJ", "clubsQ", "clubsK", "clubsA", };
return Cards;
}
public static String [] player1Cards(String[] deckcards){
String[] player1cards = deckcards[0];
return player1cards;
}
I get two errors.
One error tells me
Type mismatch cannot convert from String to String[]
The other error tells me there is a line breakpoint error on:
String[] player1cards = deckcards[0];
How can I fix this?
An element of a String[]
is a String
, not a String[]
, but saying that doesn't fix your problem.
Issues of design and style aside, I think your intention is:
public class War {
public static void main(String[] args) {
String[] deckcards=deck();
// shuffle deck
List<String> list = new ArrayList<>(Arrays.asList(deckcards));
Collections.shuffle(list);
deckcards = list.toArray(new String[]{});
// deal from deck
String[] player1cards=player1Cards(deckcards, 0, 5);
}
public static String[] deck(){
String[] Cards = {"spades1","spades2","spades3", "spades4", "spades5", "spades6", "spades7", "spades8", "spades9", "spades10", "spadesJ", "spadesQ", "spadesK", "spadesA", "clubs1", "clubs2", "clubs3", "clubs4", "clubs5", "clubs6", "clubs7", "clubs8", "clubs9", "clubs10", "clubsJ", "clubsQ", "clubsK", "clubsA", };
return Cards;
}
public static String [] player1Cards(String[] deckcards, int from, int quantity){
String[] player1cards = Arrays.copyOfRange(deckcards, from, from + quantity);
return player1cards;
}
}
Disclaimer: untested code (thumbed on iPhone) and nothing done to fix design problems, but you will have something that works.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments