卡片组包含1-13个13个字符,其中卡片上显示的值代表以下数值:
'A' - 1
'2' - 2
'3' - 3
'4' - 4
'5' - 5
'6' - 6
'7' - 7
'8' - 8
'9' - 9
'T' - 10
'J' - 11
'Q' - 12
'K' - 13
该计划应删除所有' K'卡和任何两个连续的数字总计为13.
我已经编写了一个程序来删除' K'总计13的数字,但我无法弄清楚如何提供用户输入的卡片(使用扫描仪类)。我知道扫描仪类接受输入,但我如何与动态输入的现有数组进行比较。请帮忙
这是我的代码:
import java.util.Scanner;
import java.util.*;
public class CircleGame
{
static Scanner sc = new Scanner(System.in);
static int[] deckArray = {'A',2,3,4,5,6,7,8,9,'T','J','Q','K'};
public int cardsLeft(int[] deck, int sum)
{
for(int i=0;i<deck.length;i++)
{
//char c = deck.charAt(i);
if(deckArray[i] == 'A')
deckArray[i] = 1;
if(deckArray[i] == 'T')
deckArray[i] = 10;
if(deckArray[i] == 'J')
deckArray[i] = 11;
if(deckArray[i] == 'Q')
deckArray[i] = 12;
if(deckArray[i] == 'K')
deckArray[i] = -1;
for(int j=0;j<deckArray.length;j++){
for(int k=j+1;k<deckArray.length;k++){
if(deckArray[j]+deckArray[k] == sum){
deckArray[j] = -1;
deckArray[k] = -1;
}
}
}
}
int count = 0;
for(int aDeckArray:deckArray)
{
if (aDeckArray != -1) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
CircleGame c = new CircleGame();
c.cardsLeft(deckArray,13);
}
}
答案 0 :(得分:1)
如果我理解你,你想让用户提供一个套牌。
天真的方法是这样做:
Scanner sc = new Scanner(new InputStreamReader(System.in));
// building of a list containing all possible cards. This way, you have a link between the symbol and the number
List<String> table = new ArrayList<String>();
table.add("A");
for (int i = 2; i <= 9; i++) {
table.add(String.valueOf(i));
}
table.add("T");
table.add("J");
table.add("Q");
table.add("K");
int[] deck = new int[13];
for (int i = 0; i < 13; i++) {
String input = sc.nextLine();
// if the given symbol is invalid, try again
if (!table.contains(input)) {
System.out.println("Invalid card");
i--;
} else {
deck[i] = table.indexOf(input); // if "A"-> 1, if "4"-> 4, ...
}
}
// your internal logic to check if it is valid
for (int i = 0; i < l; i++) {
if (deck[i] != -1) {
System.out.println(String.valueOf(deck[i]));
}
}