我想让程序根据卡号的第一位数字在屏幕上打印卡片类型。该程序编译正常并运行,但不会将卡类型语句放到屏幕上。我相信这只是一个简单的修复,但我花了几个小时试图发现它可能是什么。我在这里错过了什么?案件陈述代码不显示在屏幕上,但顺从没有错误
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 +“”; –
有没有一些原因,这不起作用? –