2017-10-07 21 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(); String 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

charAt返回char,和你比较它与它的面值代表INT。即,代替4,56,应该使用'4','5''6'。另外,请注意“37”是两个字符,因此您不能只评估第一个字符。相反,你可以使用String.startsWith(String)等一系列的if-else条件:

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

您与整数比较字符。

看看与switch语句中的解决方案:

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"); 
     } 
    } 
}