简体   繁体   English

如何防止代码打印负输出?

[英]How can I prevent the code from printing a negative output?

This is the code I am working on.这是我正在处理的代码。 The problem is when I enter a non hexadecimal value such as "rr" or "z", it prints a negative value.问题是当我输入一个非十六进制值(例如“rr”或“z”)时,它会打印一个负值。 How can I stop it from doing this?我怎样才能阻止它这样做? Also, how can I solve this question?另外,我该如何解决这个问题? When the program starts it asks the user to enter a hexadecimal number of lengths 2 (eg, 1F).当程序启动时,它会要求用户输入长度为 2 的十六进制数(例如,1F)。 The smallest number the user should enter is 90 (decimal equivalent 144) and the largest is FF (decimal equivalent 255).用户应该输入的最小数字是 90(十进制等效 144),最大的是 FF(十进制等效 255)。

import java.util.Scanner;

public class HexaConverter {


        public static int hex2decimal(String s)
        {
                 String digits = "0123456789ABCDEF";
                 s = s.toUpperCase();
                 int val = 0;
                 for (int i = 0; i < s.length(); i++)
                 {
                     char c = s.charAt(i);
                     int d = digits.indexOf(c);
                     val = 16*val + d;
                 }
                 return val;
        }
        //Start of conversion method
         public static void main(String args[])
            {
                String hexadec_num;
                int dec_num, i=1, j;

                int bin_num[] = new int[100];
                Scanner scan = new Scanner(System.in);

                System.out.print("Please enter a Hexadecimal Number: ");
                hexadec_num = scan.nextLine();
                final int MAX_LENGTH = 2;

                      if(String.valueOf(hexadec_num).length() <= MAX_LENGTH) {
                          /* first convert the hexadecimal to decimal */

                          dec_num = hex2decimal(hexadec_num);
                          System.out.print("Equivalent Dec Number is : "+ dec_num);
                          System.out.println();

                          /* now convert the decimal to binary */

                          while(dec_num != 0)
                          {
                              bin_num[i++] = dec_num%2;
                              dec_num = dec_num/2;
                          }

                          System.out.print("Equivalent Binary Number is : ");
                          System.out.print("\n");

                          for(j=i-1; j>0; j--)
                          {
                              System.out.print(bin_num[j]);
                          }
                      } 
                      else 
                      {
                        System.out.println("ERROR: Input number was too long");


                        main(args); // calling to return to the main method
                        }
                      }
                    } 

int d = digits.indexOf(c); will return -1 if the character doesn't exist in the digits string, if you add in a check for this:如果数字字符串中不存在该字符,则将返回 -1,如果您为此添加检查:

int d = digits.indexOf(c);
if (d == -1) return 0;  // CHECK HERE
val = 16*val + d;

Then you can handle the error as you like, in this example I just returned 0然后你可以随意处理错误,在这个例子中我只是返回 0

As for the second part of the question, you can just check the value of the returned result is within the range 144 and 255至于问题的第二部分,您可以检查返回结果的值是否在 144 和 255 范围内

if (144 <= dec_num && dec_num <= 255) return "SUCCESS";
return "FAIL";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM