java - 需要帮助修复 java 程序

标签 java arrays

/* * 程序可以将十进制转换为二进制并报告非法字符 * 用过的 *程序无法将二进制转换为十进制 */ 导入java.util.Scanner;

/** * 这个类包含一个完整的程序,只有一个main()方法,用于 * 将非负十进制整数(即以 10 为基数的整数)转换为 * 正二进制整数(即以 2 为基数的整数)。的值(value)观 * 转换后从命令行读入。 */ 公共(public)类 BaseConversions2 { 公共(public)静态无效主(字符串[]参数) {

    //prints the intro to the program if the user does not give any
    //numbers
    if (args.length == 0)
    {
        printOpening();
        printIntro();

        return;
    }

    //method to check and convert numbers given by the user
    runCovert(args);
}

private static void runCovert(String[] args)
{
    for (int i = 0; i < args.length; ++i)
    {
        if (args[i].startsWith("d"))
        {

            decimalToBinary(args);
            ++i;

        }

        else
        {

            binaryToDecimal(args);
            ++i;
        }

    }
}

//method to check if the decimal number the user gave is valid
//if it is convert it
//if not print an error
//method to check if the binary number the user gave is valid
//if it is convert it
//if not print an error
public static void binaryToDecimal(String[] args)
{

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

        String value = args[i].replaceAll("b", "");
//                String test = value.replaceAll("\\s+", "");

        if (value.matches("[0-1]+"))
        {
            int test2 = Integer.parseInt(value, 2);
            if (test2 >= 0 && test2 <= 11111111111111l)
            {
                System.out.println("The binary value " + args[i]
                    + " is equivalent to the decimal value "
                    + test2);
                pause();
                System.out.println("");
                ++i;
            }
            else
            {
                System.out.println("Error: "
                    + "The decimal value " + args[i]
                    + " is out of range");

                pause();

            }

        }
        else
        {
            System.out.println("Error: The decimal value "
                + args[i]
                + " has an illegal character in it, ex -+/");

            pause();

        }

    }
}

//method that runs the two conversion methods above
private static void decimalToBinary(String[] args)
{

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

        String value = args[i].replaceAll("d", "");
//                String test = value.replaceAll("\\s+", "");

        if (value.matches("[0-9]+"))
        {
            int test2 = Integer.valueOf(value);
            if (test2 >= 0 && test2 <= 65535)
            {
                System.out.println("The decimal value " + args[i]
                    + " is equivalent to the binary value "
                    + Integer.toBinaryString(test2));
                pause();
                System.out.println("");

            }
            else
            {
                System.out.println("Error: "
                    + "The decimal value " + args[i]
                    + " is out of range, "
                    + "or does not start with the "
                    + "letter \"d");

                pause();

            }

        }
        else
        {
            System.out.println("Error: The decimal value "
                + args[i]
                + " has an illegal character in it, ex -+/");

            pause();

        }

    }
}

private static void printIntro()
{

    System.out.print("This program allows the user to convert "
        + "either decimal integer values to their\n"
        + "equivalent binary values, or vice versa. "
        + "In either case, it then displays, on\n"
        + "the standard output, a sentence containing "
        + "both the original and the converted\n"
        + "values. Values to be converted are entered on "
        + "the command line, separated by a\n"
        + "blank space. Decimal values must have a d "
        + "as their first character and binary\n"
        + "values must have a b as their first character.");
    System.out.println("");
    System.out.println("");
    System.out.print("The program may convert any number "
        + "of values of either type on any given run.\n"
        + "A value is valid only if it contains just "
        + "those digits allowed by its base,\n"
        + "and lies within the permitted range. "
        + "Our maximum values are: 1111111111111111\n"
        + "for binary, and 65535 for decimal. As numbers, "
        + "these maximum values are equal.\n"
        + "The minimum value in both cases is 0.");
    System.out.println("");
    System.out.println("");
    System.out.println("If any given value does not satisfy the "
        + "necessary criteria, an error must be\n"
        + "reported and the value must be ignored by "
        + "the program, which simply carries\n"
        + "on after reporting the error. In addition, "
        + "if the initial character of a value\n"
        + "is neither b nor d, the program also reports "
        + "the error and carries on.");
    System.out.println("");
    System.out.println("");
    System.out.println("\t\t\t\t\t\t\t\tScreen 1 of 1");

    pause();
}

//method to print the opening screen
private static void printOpening()
{



    pause();
    System.out.println("");
    System.out.println("");
}

//method to pause the program
private static void pause()
{
    Scanner kbd = new Scanner(System.in);

    System.out.print("\n\n\n\nPress Enter to continue ...");
    kbd.nextLine();

}

}

我需要帮助弄清楚为什么我的代码不起作用,基本上我的代码采用二进制数并将其转换为十进制数,反之亦然,但是当我同时输入二进制数和十进制数时,它会说 1其中有一个非法字符,我需要弄清楚为什么要这样做。

我知道这是很多代码,但是非常感谢您的帮助,因为我现在真的陷入困境

最佳答案

为 和

标记分配 display:inline css 属性。然后减小图像大小。您将得到您需要的内容。这是因为图像大小比行高更大。

关于java - 需要帮助修复 java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42622406/

相关文章:

javascript - 从数组中删除数据与其他数组进行比较

C++ 将两个 int 数组连接成一个更大的数组

Javascript按数组对对象数组进行排序

java - Maven 和 GitHub 的版本编号/发布机制

java - 使用ASM注入(inject)字节码时出现StringIndexOutOfBoundsException错误

java - Eclipse IFile(获取java类方法)

java - 无法在其Adapter中调用Fragment的功能,Android应用程序开发

java - Java Mail 中的内联链接在邮件客户端中不起作用

javascript - 如何使用 jQuery 将值插入到特定索引处的二维数组中

c++ - 为什么在二维数组参数中使用不带括号的引用运算符时会出现错误 "declaration as array of references"?