문제

나는 지난 며칠 동안 이것과 싸워왔고 누군가가 지침을 제공할 수 있기를 바라고 있습니다.처음으로 Java에서 배열 작업을 했고, Java를 배운 지 약 2개월 정도 되었습니다.부드럽게 행동하세요.:)

내 생각에는 병렬 배열을 만들고 있는데, 여기서 cardInDeck[][]는 카드 이름(예: Ace, 10, Jack) 및 슈트 이름(예: Hearts, Spades 등)으로 채워집니다.메서드의 출력과 내가 생각하는 대로 배열에 쓰고 있는 내용은 예상한 대로이지만 배열의 내용을 인쇄할 때 몇 가지 비정상적인 결과가 나타납니다.

예상치 못한 결과가 배열을 선언할 때 부적절하게 구성했기 때문에 발생하는지, 신과 자연(또는 적어도 Java)에게 혐오스러운 배열을 만들려고 하는지, 또는 배열이 맞습니다. 하지만 내용을 인쇄하는 방법을 이해할 수 없습니다.

귀하가 제공할 수 있는 도움을 주시면 감사하겠습니다.미리 감사드립니다!

코드와 결과는 아래와 같습니다.

import java.util.*;
import java.lang.*;
//
public class DeckOfCards
{
   public static void main(String[] args)
   {
      int rank;
      int suit;
//
   for(rank=1;rank<14;rank++)
   {
       String tempCardName = Integer.toString(rank);
       String cardName;
       if(rank==1)
       cardName="Ace";
       else
       if(rank==13)
       cardName="King";
       else
       if(rank==12)
       cardName="Queen";
       else
       if(rank==11)
       cardName="Jack";
       else
       cardName=tempCardName;
 //
       for(suit=1;suit<5;suit++)
       {
       String suitName;
       switch(suit)
       {case 1:
   suitName = "Spades";
   break;
   case 2:
   suitName = "Hearts";
   break;
   case 3:
   suitName = "Clubs";
   break;
         case 4:
   suitName = "Diamonds";
   break;
   default:
   suitName = "Error";}
       String tempCardInDeck=(cardName + " of " + suitName);
       System.out.print("method output: " tempCardInDeck);
 //*
       String[][] cardInDeck = new String[][]
          {
             {cardName,suitName}
          };
       System.out.print(" /// to array: " + cardName + " " + suitName);
       System.out.println(" /// from array: " + Arrays.toString( cardInDeck ) );
     }
     }
   }
 }

결과:

method output: Ace of Spades /// to array: Ace Spades /// from array: [[Ljava.lang.String;@9ffb18]
method output: Ace of Hearts /// to array: Ace Hearts /// from array: [[Ljava.lang.String;@11de914]
method output: Ace of Clubs /// to array: Ace Clubs /// from array: [[Ljava.lang.String;@b1406b]
method output: Ace of Diamonds /// to array: Ace Diamonds /// from array: [[Ljava.lang.String;@fc5408]
method output: 2 of Spades /// to array: 2 Spades /// from array: [[Ljava.lang.String;@1f102c1]
method output: 2 of Hearts /// to array: 2 Hearts /// from array: [[Ljava.lang.String;@1ec0130]
method output: 2 of Clubs /// to array: 2 Clubs /// from array: [[Ljava.lang.String;@1420fea]
method output: 2 of Diamonds /// to array: 2 Diamonds /// from array: [[Ljava.lang.String;@230be4]
method output: 3 of Spades /// to array: 3 Spades /// from array: [[Ljava.lang.String;@e1e567]
method output: 3 of Hearts /// to array: 3 Hearts /// from array: [[Ljava.lang.String;@9bfee2]
method output: 3 of Clubs /// to array: 3 Clubs /// from array: [[Ljava.lang.String;@17aaeec]
method output: 3 of Diamonds /// to array: 3 Diamonds /// from array: [[Ljava.lang.String;@1721a26]
method output: 4 of Spades /// to array: 4 Spades /// from array: [[Ljava.lang.String;@12db7c]
method output: 4 of Hearts /// to array: 4 Hearts /// from array: [[Ljava.lang.String;@7c28c]
method output: 4 of Clubs /// to array: 4 Clubs /// from array: [[Ljava.lang.String;@17588d5]
method output: 4 of Diamonds /// to array: 4 Diamonds /// from array: [[Ljava.lang.String;@16a7c99]
method output: 5 of Spades /// to array: 5 Spades /// from array: [[Ljava.lang.String;@1a5d08]
method output: 5 of Hearts /// to array: 5 Hearts /// from array: [[Ljava.lang.String;@d1c9b5]
method output: 5 of Clubs /// to array: 5 Clubs /// from array: [[Ljava.lang.String;@111c3f0]
method output: 5 of Diamonds /// to array: 5 Diamonds /// from array: [[Ljava.lang.String;@156f14c]
method output: 6 of Spades /// to array: 6 Spades /// from array: [[Ljava.lang.String;@fbd1fc]
method output: 6 of Hearts /// to array: 6 Hearts /// from array: [[Ljava.lang.String;@973678]
method output: 6 of Clubs /// to array: 6 Clubs /// from array: [[Ljava.lang.String;@1791620]
method output: 6 of Diamonds /// to array: 6 Diamonds /// from array: [[Ljava.lang.String;@9ba632]
method output: 7 of Spades /// to array: 7 Spades /// from array: [[Ljava.lang.String;@bc5245]
method output: 7 of Hearts /// to array: 7 Hearts /// from array: [[Ljava.lang.String;@1bd523d]
method output: 7 of Clubs /// to array: 7 Clubs /// from array: [[Ljava.lang.String;@6250d2]
method output: 7 of Diamonds /// to array: 7 Diamonds /// from array: [[Ljava.lang.String;@a8198c]
method output: 8 of Spades /// to array: 8 Spades /// from array: [[Ljava.lang.String;@25753d]
method output: 8 of Hearts /// to array: 8 Hearts /// from array: [[Ljava.lang.String;@1341183]
method output: 8 of Clubs /// to array: 8 Clubs /// from array: [[Ljava.lang.String;@169cccc]
method output: 8 of Diamonds /// to array: 8 Diamonds /// from array: [[Ljava.lang.String;@10469e8]
method output: 9 of Spades /// to array: 9 Spades /// from array: [[Ljava.lang.String;@c4fedd]
method output: 9 of Hearts /// to array: 9 Hearts /// from array: [[Ljava.lang.String;@138847d]
method output: 9 of Clubs /// to array: 9 Clubs /// from array: [[Ljava.lang.String;@1826ac5]
method output: 9 of Diamonds /// to array: 9 Diamonds /// from array: [[Ljava.lang.String;@12fb063]
method output: 10 of Spades /// to array: 10 Spades /// from array: [[Ljava.lang.String;@1e55d39]
method output: 10 of Hearts /// to array: 10 Hearts /// from array: [[Ljava.lang.String;@14b525c]
method output: 10 of Clubs /// to array: 10 Clubs /// from array: [[Ljava.lang.String;@c4c05]
method output: 10 of Diamonds /// to array: 10 Diamonds /// from array: [[Ljava.lang.String;@1530551]
method output: Jack of Spades /// to array: Jack Spades /// from array: [[Ljava.lang.String;@18235a1]
method output: Jack of Hearts /// to array: Jack Hearts /// from array: [[Ljava.lang.String;@18ee2ee]
method output: Jack of Clubs /// to array: Jack Clubs /// from array: [[Ljava.lang.String;@1d48043]
method output: Jack of Diamonds /// to array: Jack Diamonds /// from array: [[Ljava.lang.String;@30cd64]
method output: Queen of Spades /// to array: Queen Spades /// from array: [[Ljava.lang.String;@1fc9fee]
method output: Queen of Hearts /// to array: Queen Hearts /// from array: [[Ljava.lang.String;@67f797]
method output: Queen of Clubs /// to array: Queen Clubs /// from array: [[Ljava.lang.String;@1b01949]
method output: Queen of Diamonds /// to array: Queen Diamonds /// from array: [[Ljava.lang.String;@4c2849]
method output: King of Spades /// to array: King Spades /// from array: [[Ljava.lang.String;@1e8f2a0]
method output: King of Hearts /// to array: King Hearts /// from array: [[Ljava.lang.String;@90f19c]
method output: King of Clubs /// to array: King Clubs /// from array: [[Ljava.lang.String;@1e67280]
method output: King of Diamonds /// to array: King Diamonds /// from array: [[Ljava.lang.String;@675039]
도움이 되었습니까?

해결책

출력에 표시되는 내용은 기본 구현입니다. toString.에서 문서 ~을 위한 Arrays:

지정된 배열 내용의 문자열 표현을 반환합니다.배열에 다른 배열이 요소로 포함되어 있는 경우 Object에서 상속된 Object.toString() 메서드에 의해 문자열로 변환됩니다. 이 메서드는 내용이 아닌 ID를 설명합니다.

기본 버전이기 때문에 물체, 당신이 얻게 될 것은...

Object 클래스의 toString 메소드는 객체가 인스턴스인 클래스 이름, 기호 문자 '@' 및 객체 해시 코드의 부호 없는 16진수 표현으로 구성된 문자열을 반환합니다.

다른 답변에서 제안한 바와 같이, Arrays.deepToString 당신이 원하는 것이되어야합니다.그것은 실제로 나에게 새로운 것이므로 직접 시험해 보겠습니다.

다른 팁

카드 덱을 구현하는 또 다른 방법은 Enums를 사용하는 것입니다. 이는 컴파일러가 당신을 위해 유형 검사를 시행 할 수있게 해줍니다."nofollow"> 카드 , deck 이 링크에서 디스플레이

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top