2017年4月12日 星期三

[ITSA Basic] 題目40. ISBN驗證

[ITSA Basic] 題目40. ISBN驗證

import java.io.*;
import java.lang.NumberFormatException;

public class ISBN
{
public static void main(String[] args) throws IOException , NumberFormatException
{

String isbn ;                               //使用者輸入需要判斷的ISBN
int first = 0;                               //第一次累加每一次運算的值
int firstarr[] = new int[10];       //把first的值放入此陣列
int second = 0;                          //第二次累加每一次運算的值
int secondarr[] = new int[10];  //把second的值放入此陣列

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//讀檔
isbn = br.readLine();
String[] isbnSpite = isbn.split(" ");
                //用split切割String,這邊是遇到空白就切割

                //把String 轉 int
int afterConvert = 0; //從String轉成int後的值
for(int i = 0;i<isbnSpite.length;i++)
{
try
{
afterConvert = Integer.parseInt(isbnSpite[i]);
}
catch(Exception e)
{
                                //如果String不是數字,例如:X。這裡讓他等於(int)10
afterConvert = 10;
}

                        //做第一次累加
first = first + afterConvert;
firstarr[i] = first;
}

                //做第二次累加
for(int j = 0;j<secondarr.length;j++)
{
second = second + firstarr[j];
secondarr[j] = second;
}
                //如果被11整除,則YES,否則NO
if(secondarr[9] % 11 == 0)
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}

沒有留言:

張貼留言