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?
Your problem is here:
String[] player1cards = deckcards[0];
deckcards[0]
is an element of an array of type String
. So it is a String variable. But you are trying to assign it to String[] player1cards
which is an array of strings which is not allowed in Java.
This is your error:
String[] player1cards = deckcards[0];
If you want to copy the first value of deckscards
into the first value of player1cards
, then one alternative is:
String[] player1cards = deckcards;
player1cards[0] = deckcards[0];
According to your code, you are saying that the whole String[]
is equal to the first value of the other array. That cannot be. If you want to equal two arrays, then:
String [] automobile = {"toyota","zion"};
String[] car = {"nissan"};
automobile = car;
Hope this helps.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.