【问题标题】:JAVA How to get the summation of 14-based values?JAVA如何获得基于14的值的总和?
【发布时间】:2016-08-26 00:55:33
【问题描述】:

目前,我正在尝试编写一个程序,它会要求用户输入一个基于 14 的数字,然后它会输出一个基于 14 的数字。提示如下... “一个外来物种使用基于 14 的编号系统。它们的十位数字,0 到 9,与我们的相同 十进制。他们用 A、J、Q 和 K 分别表示十进制 10、11、12 和 13。他们 雇你写一个Java程序来做他们两个数字的总和。 程序应该提示用户输入两个基于 14 的数字,然后显示这些数字的总和 两个数字。输出也应该是基于 14 的。 (他们不愿意学习我们的十进制系统!) 例如,如果输入是 17 和 96,则输出应该是 AK。"

当我输入 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);
               }
    }


  }

【问题讨论】:

  • 你的问题是什么。你是如何实现convertFrom14To10convertFrom10To14 的?请上传相同的
  • (currentChar != 'A' || currentChar != 'J' || ... ) 更改为(currentChar != 'A' &amp;&amp; currentChar != 'J' &amp;&amp; ... )

标签: java if-statement for-loop while-loop static-methods


【解决方案1】:

您的 validateInput() 方法确实是问题所在。 “||”运算符是逻辑或,这意味着带有字母的 any 输入将失败。我认为您想要“&&”运算符,这是一个逻辑与。

【讨论】:

  • 这是有道理的,它成功了!非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-12-28
  • 2019-09-23
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多