2017-10-07 12 views
0

카드 번호의 첫 번째 숫자에 따라 화면에 카드 유형을 인쇄하는 프로그램을 얻으려고합니다. 프로그램이 잘 컴파일되고 실행되지만 카드 유형 명령문을 화면에 표시하지 않습니다. 나는 이것이 단지 간단한 수정 일 것이라고 확신하지만 그것이 무엇인지를 발견하려고 몇 시간을 보냈다. 내가 여기서 무엇을 놓치고 있니?사례 명세서 코드가 화면에 표시되지 않지만 오류없이 준수합니다.

public static void main (String [] args) { 


    System.out.println ("Please enter your credit card number without spaces."); 
    Scanner keyIn = new Scanner (System.in); 

    long ccNum = keyIn.nextLong(); 
    String cNum = ccNum + "";  


    switch (cNum.charAt (0)) 
     { 
     case 4: 
      System.out.println ("The card is a Visa"); 
      break; 

     case 5: 
      System.out.println ("The card is a MasterCard"); 
      break; 

     case 6: 
      System.out.println ("The card is a Discover Card"); 
      break; 

     case 37: 
      System.out.println ("The card is an American Express Card"); 
      break; 
     } 
} 
+1

참조 http://www.asciitable.com/ 어떤 편지 4, 5, 6, 37 :

스위치 문을 사용하여 솔루션을 살펴보십시오 니가 원하는거야. 미래의 문제 : 디버깅을 배우고,'cNum'이 무엇인지,'cNum.charAt (0)'이 리턴하는 것을 살펴보십시오. – luk2302

+0

이 줄은 long을 문자열로 변환하기위한 것입니다. long ccNum = keyIn.nextLong(); 문자열 cNum = ccNum + ""; –

+0

이것이 작동하지 않는 이유가 있습니까? –

답변

0

카드 번호와 케이스를 비교하려는 경우. cNum.charAt (0))int으로 변환해야합니다. 그런데 약 경우 37 ??

public static void main (String [] args) { 


    System.out.println ("Please enter your credit card number without spaces."); 
    Scanner keyIn = new Scanner (System.in); 

    long ccNum = keyIn.nextLong(); 
    String cNum = ccNum + "";  


    switch (Character.getNumericValue(cNum.charAt (0))) 
     { 
     case 4: 
      System.out.println ("The card is a Visa"); 
      break; 

     case 5: 
      System.out.println ("The card is a MasterCard"); 
      break; 

     case 6: 
      System.out.println ("The card is a Discover Card"); 
      break; 

     case 37: 
      System.out.println ("The card is an American Express Card"); 
      break; 
     } 
} 
+0

이 코드가 사용되는 방식은 사례 3으로 작동합니다. 전체 코드에 다시 입력하면 훌륭하게 작동합니다. –

1

charAtchar을 반환, 당신은 얼굴 값을 나타내는 INT과 비교하고있다. 즉 4 대신 56 대신 '4', '5''6'을 사용해야합니다. 또한 "37"은 두 문자이므로 첫 번째 문자를 평가할 수는 없습니다. 대신 String.startsWith(String)를 사용할 수 있고 경우 - 다른 일련의 조건 :

if (cNum.startsWith("4")) { 
    System.out.println ("The card is a Visa"); 
} else if (cNum.startsWith("5")) { 
    System.out.println ("The card is a MasterCard"); 
} else if (cNum.startsWith("6")) { 
    System.out.println ("The card is a Discover Card"); 
} else if (cNum.startsWith("37")) { 
    System.out.println ("The card is an American Express Card"); 
} 
+0

예. 그게 문제 야. 감사. 나는 그것이 그렇게 단순해야만한다는 것을 알았습니다. –

1

당신은 정수와 문자를 비교합니다. 나는 의심을 참조

public static void main (String [] args) { 

    System.out.println("Please enter your credit card number without spaces."); 
    final Scanner keyIn = new Scanner(System.in); 

    long ccNum = 0L; 

    if (keyIn.hasNext()) { 
     ccNum = keyIn.nextLong(); 
    } 

    final String cNum = "" + ccNum; 

    int firstDigits = Integer.parseInt(cNum.substring(0, 2)); 

    if (firstDigits > 37) { 
     firstDigits /= 10; 
    } 

    switch(firstDigits) { 
     case 4: { 
      System.out.println("The card is a Visa"); 
      break; 
     } 
     case 5: { 
      System.out.println("The card is a MasterCard"); 
      break; 
     } 
     case 6: { 
      System.out.println("The card is a Discover Card"); 
      break; 
     } 
     case 37: { 
      System.out.println("The card is an American Express Card"); 
      break; 
     } 
     default: { 
      System.out.println("Incorrect card number"); 
     } 
    } 
}