저는이 작은 프로그램을 명령 줄 인수를 사용하여 만들었습니다. 저는 작업의 90 %를 수행했습니다. 하지만 사용자가 aswell 숫자 이외의 값을 입력 할 수 있도록 노력하고 있습니다 ..어디에서/내 프로그램에서 숫자가 아닌 값을 허용 할 수 있습니까?
사용자 입력/출력 예를
입력 :
$ java d1 4eb:16 10110110:2 407:8 2048:10
출력 :
4eb base 16 is 1259 base 10 10110110 base 2 is 182 base 10 407 base 8 is 263 base 10 2048 base 10 is 2048 base 10
글자가 있기 때문에 첫 번째 입력에만 문제가 발생하며 숫자 예외가 발생했습니다. 또는. 어떤 도움도 좋을 것입니다. 나는 답이 아니라 옳은 방향으로 도움을 원합니다. 고맙습니다!
public class homework{
public static void main (String[] args){
int answer1=0,check1=0,check2=0,x=0, val=0,rad=0; //holds integer values user gives and check for : handler, answer etc
do{ //will continue to loop if no : inputted
for (x=0;x<args.length;x++){
check1=args[x].indexOf(":"); //checks input1 for the :
if(check1==-1){System.out.println("No Colon Found in "+args[x]+".");check1=0;}
else{
String numbers [] = args[x].split(":"); //splits the string at :
val = Integer.parseInt(numbers[0]); //parses [0] to int and assigns to val
rad = Integer.parseInt(numbers[1]); //parses [1] to int and assigns to rad
if(val==0||rad==0){System.out.println("The argument "+args[x]+" could not be converted.");check2=0;}
else{
for (int i = 0; val > Math.pow(rad, i); i++){
int digit = (val/(int) Math.pow(10, i)) % 10;
int digitValue = (int) (digit * Math.pow(rad, i));
answer1 += digitValue;}
answer1 = Integer.parseInt(numbers[0], rad); //finds the answer in base10.
System.out.println(val+" base "+rad+" is "+answer1+" base 10."); //gives user the results
}}}}while(check1==-1); }} //if user forgot : loop