我想讓程序根據卡號的第一位數字在屏幕上打印卡片類型。該程序編譯正常並運行,但不會將卡類型語句放到屏幕上。我相信這只是一個簡單的修復,但我花了幾個小時試圖發現它可能是什麼。我在這裏錯過了什麼?案件陳述代碼不顯示在屏幕上,但順從沒有錯誤
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;
}
}
見http://www.asciitable.com/什麼字母4,5,6和37是指,我懷疑是你想要的。對於未來的問題:學習調試,看看'cNum'是什麼以及'cNum.charAt(0)'返回的是什麼。 – luk2302
此行旨在將long轉換爲字符串。 long ccNum = keyIn.nextLong(); String cNum = ccNum +「」; –
有沒有一些原因,這不起作用? –