当前位置: 首页 > 知识库问答 >
问题:

我需要帮助修复Java程序

南门峰
2023-03-14

/**程序可以将十进制转换为二进制并报告是否使用了非法字符*程序不能将二进制转换为十进制*/import java.util.scanner;

/***这个类包含一个完整的程序,只有一个main()方法,用于*将非负十进制整数(即以10为基数的整数)转换为*正二进制整数(即以2为基数的整数)。要*转换的值是从命令行读入的。*/public class BaseConversions2{public static void main(String[]args){

    //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个答案

訾旭
2023-03-14

为和指定Display:内联css属性

标签。然后缩小图像大小。你会得到你所需要的。也就是说,图像大小比线高。

 类似资料:
  • 很抱歉打扰你们,我是编程新手,一直在这个程序上有问题。 谢了! *********编辑***************我从BMI中删除了int值,但有损转换错误仍然存在。有什么办法解决这个问题吗?

  • 存储在阵列中的数据: 错误输出/回溯 运行文件('C:/Users/Aidan/.spyder-py3/temp.py',wdir='C:/Users/Aidan/.spyder-py3')回溯(最后一次最近调用): 文件“”,第1行,在运行文件中('C:/Users/Aidan/.spyder-py3/temp.py',wdir='C:/Users/Aidan/.spyder-py3') 文件“

  • 我正在上Java课程的第三周。我正在做一个下星期要交的课堂作业。使用控制台作为输出,我可以毫无问题地完成分配,这是可以接受的。然而,教授也建议我们研究JTextArea,并考虑将其用于我们的程序输出。 我从一个教程中找到了一些代码,并且能够至少得到一个文本块来显示我要显示的第一行文本。但是在我编写实际程序时,我需要随着程序的进展继续向文本块添加额外的行。

  • 我的java课有一个实验室。我拥有一切,除了不能得到正常工作的平均方法。每当我运行程序时,平均值都是从随机值中计算出来的,而不是更新的值。 程序测试 未排序的数组类(我之前忘记附加)

  • 首先,我是C、C++、C#、Android和Swift的开发人员,但我绝对没有JavaScript、PHP或Web开发经验。 即只接受整数值的输入。 这是刀片代码:

  • 我想对我的文件内容进行排序。我的文件内容是学生姓名,他们的学生编号,他们的班级,他们的成绩。这些数据由“;”分隔。首先,我需要计算平均值和字母等级。我已经计算过了,但是我需要将所有内容写入另一个文件,顺序必须是最高等级到最低等级。我该怎么办?