简体   繁体   中英

How can I print my desired card game output?

I am asked to write an A to Z card game program that prints the desired number of input a user might want. The program needs to have deletion for example the cards are: 1 A -> 2 B -> 3 C. if the user inputs -3 C which means deletion of 3 C the program need to output 2 A -> 2 B.

SAMPLE INPUT
Cards to play: 5  
1 A
2 B 
3 C 
1 A
-3 C

Output
1 A 
2 B 
3 C
1 A
-3 C

cards left: 
2 A -> 2 B

this is what I did so far:

Scanner sc = new Scanner(System.in);  
System.out.print("Input card: ");        
String[] string = new String [sc.nextInt()];      
sc.nextLine();
for (int i = 0; i < string.length; i++) 
{  
    string[i] = sc.nextLine(); 
    String[] splitString = string[i].split(" "); 
    String part1 = string[0];
    String part2 = string[1];
    int X = Integer.parseInt(part1);
    char y = part2.charAt(0);
    int Y = y - 'A';
    string[Y] += X;
}
System.out.println("Cards left: ");
for(String str: string) 
{  
    System.out.println(str + " " i + 'A');
}

I think I need to get the 0 index value so that was the conversion (string to int, string to char to int) are for (which I think doesn't work also). and it seems like it can only read 1 input and I can't seem to print out the desired output I want. Badly need help

Seems to me that you have been confused between your different object.

You should do :

String[] splitString = string[i].split(" "); 
String part1 = splitString[0];
String part2 = splitString[1];

Pro tips : Use variable names that means something to prevent this kind of error in the future.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM