文字列から int に変換するのに助けが必要です。検索しても、この特定の質問に役立つものは見つかりませんでした。
これは私がこれまでに得たものです..(コードダウン)
- カードは 6 桁の長さでなければなりません (例: 123456) - 了解しました
- カード番号の各桁を分離しました (一、十、百...)
- 文字列を使用して、カードの最初の 5 桁 (左から) を結合しました。
最初の 5 桁を結合した後 (例: 1+2+3+4+5=12345)、この数字を 7 に MOD する必要があります (12345%7)
しかし、それは私に次のエラーを与え続けます-
入力文字列の java.lang.number.formatexception: fiveDigits:(java.lang.number.formatexception 内)
それを修正する方法に関するガイドラインは大歓迎です。ありがとう。
if(creditCard<=99999||creditCard>=1000000)
System.out.println("Your credit card is not valid. You cannot buy the ticket");
else
{
ones = creditCard%10;
tens = (creditCard%100)/10;
hundreds = (creditCard%1000)/100;
thousands = (creditCard%10000)/1000;
tensOfThousands = (creditCard%100000)/10000;
hunOfThousands = (creditCard%1000000)/100000;
String fiveDigits = "" + hunOfThousands+tensOfThousands+thousands+hundreds+tens;
int firstFiveDigits = Integer.parseInt("fiveDigits");
int remainder = firstFiveDigits%7;
if(remainder==ones)
System.out.println("Your credit card is valid. Bon Voyage!");
else
System.out.println("Your credit card is not valid. You cannot buy the ticket");