2016-05-01 46 views
1

目前,我正在嘗試編寫一個程序,它會要求用戶輸入一個基於14的號碼,並且會發出一個基於14的號碼。提示如下... 「外來物種使用14位編號系統,它們的十位數字0到9與我們的 十進制系統相同,它們使用A,J,Q和K來表示十進制10 ,分別爲11,12和13,他們分別用 聘請你編寫一個Java程序來做他們兩個數字的總和 程序應該提示用戶輸入兩個14位數字,然後顯示這兩個數字的總和 。輸出也應該是基於14的(他們不願意學習我們的十進制系統!) 例如,如果輸入是17和96,輸出應該是AK。「JAVA如何獲得基於14的值的總和?

當我輸入17和96時,它射出AK,這是我想要的。當我輸入1Z之類的東西時,會彈出「您的第一個/第二個輸入無效」,這也是預期的。但是當我輸入類似1A或j1的東西時,它會給我同樣的錯誤「你的第一個/第二個輸入無效」,儘管它應該通過。我覺得我在validateinput方法中做了一些錯誤,但我不太確定。任何幫助將不勝感激。謝謝! 謝謝,

這裏

import java.util.Scanner; 

public class H5_hieu { 

    public static void main(String[] args) 
    { 
     System.out.println("Please enter a 14 base value: "); 
     Scanner input = new Scanner(System.in); 
     String first = input.nextLine(); 
     String second = input.nextLine(); 
     boolean isFirstValid = validateInputs(first); 
     boolean isSecondValid = validateInputs(second); 
     while (!isFirstValid || !isSecondValid){ 
      if (!isFirstValid){ 
      System.out.println("Your first input is invalid"); 
      } 
      if (!isSecondValid){ 
      System.out.println("Your second input is invalid"); 
      } 
      System.out.println("Please enter a 14 base value: "); 

      first = input.nextLine(); 
      second = input.nextLine(); 
      isFirstValid = validateInputs(first); 
      isSecondValid = validateInputs(second); 
     } 

     int firstInDecimal = convertFrom14To10(first.toUpperCase()); 
     int secondInDecimal = convertFrom14To10(second.toUpperCase()); 
     System.out.println(convertFrom10To14(firstInDecimal + secondInDecimal)); 




    } 

    public static boolean validateInputs(String input) { 
     for (int i = 0;i < input.length(); i++){ 
     char currentChar = input.charAt(i); 
     if (!Character.isDigit(currentChar) && (currentChar != 'A' || currentChar != 'J' || currentChar != 'Q' || currentChar != 'K' || currentChar != 'a' || currentChar != 'j' || currentChar != 'q' || currentChar != 'k')) { 
      return false; 
     } 
     } 
     return true; 
    } 



    public static String convertFrom10To14(int input){ 
     boolean done = false; 
     int quotient = input; 
     int remainder = 0; 
     String result = ""; 
     while (!done) { 
     remainder = quotient % 14; 

     quotient = quotient/14; 


     //System.out.println("quotient: " + quotient + " remainder: " + convertDigit(remainder)); 
     result = convertDigit(remainder) + result ; 

     if (quotient == 0) 
      done = true; 
     } 
     return result; 
    } 

    public static int convertFrom14To10(String input) { 

     int length = input.length(); 
     int result = 0; 

     for(int i = 0; i < length; i++){ 

     char currentChar = input.charAt(i); 
     //System.out.println("Character at index " + i + " : " + currentChar); 
     int valueOfChar = getCoeff(currentChar); 
     // System.out.println("Decimal value of currentChar: " + valueOfChar); 
     int power = length - 1 - i; 
     //System.out.println("Power: " + power); 
     result = result + (valueOfChar * (int)Math.pow(14, power)); 
     //System.out.println(); 
     } 
    // System.out.println("Decimal of " + input + " is: " + result + "\n"); 
     return result; 
    } 

    public static int getCoeff(char character) { 
     if (character == 'A'){ 
     return 10; 
     } else if (character == 'J'){ 
     return 11; 
     } else if (character == 'Q'){ 
      return 12; 
     } else if (character == 'K'){ 
      return 13; 
     } else { 
      return Integer.parseInt(String.valueOf(character)); 
     } 

    } 
    public static String convertDigit(int number) 
    { 
    if (number == 10){ 
     return "A"; 
    } else if (number == 11){ 
     return "J"; 
     } else if (number == 12){ 
      return "Q"; 
     } else if (number == 13){ 
      return "K"; 
      } else { 
       return String.valueOf(number); 
       } 
    } 


    } 
+0

你的問題是什麼。你如何實現'convertFrom14To10'和'convertFrom10To14'?請上傳相同的 – Blip

+0

將'(currentChar!='A'|| currentChar!='J'|| ...)'改爲'(currentChar!='A'&& currentChar!='J'&& ...) '。 – saka1029

回答

1

輸入代碼你validateInput()方法確實是問題。 「||」運算符是邏輯或,這意味着任何輸入一個字母將失敗。我想你想要的「& &」運算符,這是一個邏輯和。

+1

這是有道理的,它做到了!感謝一幫幫忙! –