【问题标题】:need help fixing a java program需要帮助修复 Java 程序
【发布时间】:2017-03-06 09:52:56
【问题描述】:

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

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

    //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();

}

}

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

我知道这是很多代码,但我真的很感激帮助,因为我现在真的卡住了

【问题讨论】:

  • 将其更改为display: block;,它应该可以工作。或者将margin-right 添加到图像中。
  • 好的,那是一个相当激烈的编辑。有太多错误了。你有一个空的h2。你有内联 CSS。您不需要 jsfiddle 中的全部 htmlhead 内容。 &lt;br&gt;&lt;br&gt; 到处都有,当这是你应该用 CSS 边距做的事情时......我也需要大幅改变我的答案,不确定我现在是否有时间。我现在将删除我的答案。
  • 是的,我知道这里有很多问题,hover 我正在使用模板制作这个网站,我一直在与其他样式表进行斗争以改变事情,但有时它不会让我做简单的事情

标签: java arrays


【解决方案1】:

为两者分配 display:inline css 属性

标签。然后缩小图像大小。你会得到你需要的。那是因为图像大小比 ehat 线高。

【讨论】:

    【解决方案2】:

    pimg 元素之外添加一个div。或者,只需将 contact 类添加到您的 p 元素。

    .contact {
         display: inline !important;
         margin-left: 5px !important;
    	}
    &lt;img src="images/soyuz1.jpg" alt="" width="25px" align="left"&gt;&lt;p class="contact"&gt;The Soyuz TMA-M is the latest version in the series of Russian Soyuz Spacecraft that are currently used to Transport Crew to and from the International Space Station.&lt;/p&gt;

    【讨论】:

      【解决方案3】:

      如果没有必要,只需删除.contact,或在&lt;p&gt; 中添加一个类。您的代码说更改&lt;p&gt; 的所有实例,它是.contact 的子级,但我看不到任何具有.contact 类的元素,这就是它不影响&lt;p&gt; 的原因

      编辑

      感谢 domdom 添加了新的 css,并删除了 !important

      img {
        margin-right: 5px;
      }
      <img src="images/soyuz1.jpg" alt="" width="25px" align="left">
      <p>
        The Soyuz TMA-M is the latest version in the series of Russian Soyuz Spacecraft that are currently used to Transport Crew to and from the International Space Station.
      </p>

      【讨论】:

      • 实际上,您是否运行了自己的代码?它显示了 OP 想要避免的确切行为:行的开头没有对齐。将margin-right 添加到图像中会起作用。此外,当您使用它时,您可以删除不必要的!important
      • 我真的不明白OP想要什么,我只是编辑了我看到的错误。忘记删除!important
      • 另外,我猜我们都错过了图像的align 属性。无论如何,现在有一个contact 类,OP 现在用(很多)更多代码更新了他的问题。
      【解决方案4】:
      You to use this code
      
      <p><img src="images/soyuz1.jpg" alt="" width="25px" align="left" style="margin-right:20px;">The Soyuz TMA-M is the latest version in the series of Russian Soyuz Spacecraft that are currently used to Transport Crew to and from the International Space Station.</p>
      

      【讨论】:

      • 没有格式化、没有小提琴、内联样式和align 属性......对于仍在学习 HTML/CSS 的人(可能)来说并不是一个有用的答案。另外,OP 从一开始就彻底改变了他的问题,你可能想再看看。
      猜你喜欢
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      相关资源
      最近更新 更多