关于java:提示用户重新输入整数,直到输入二进制数 | 珊瑚贝

Prompting user to re-enter integers until binary number is entered


我是 JAVA 新手(顺便说一下计算机编程)。以下程序检查输入是否为二进制。它应该提示用户重新输入整数,直到输入二进制数。但是这个程序做的恰恰相反。
如果输入是二进制的,它要求我重新输入整数,并且当输入非二进制时程序终止。
我需要认真的帮助。这是我的输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    public static void main(String[] args) {

    int value, userValue;
    int binaryDigit = 0, notBinaryDigit = 0;

    Scanner scan = new Scanner(System.in);

    while (true) {
        System.out.println(“Please enter positive integers:”);

        userValue = scan.nextInt();
        value = userValue;

        while (userValue > 0) {
            if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
                binaryDigit++;
            } else {
                notBinaryDigit++;
            }

            userValue = userValue / 10;

        }

        if (notBinaryDigit > 0) {
            System.out.println(value +” is a not a Binary Number.”);
            break;
        } else {
            System.out.println(value +” is  a  Binary Number.”);

        }

    }
}

  • if (!((userValue % 10 == 0) || (userValue % 10 == 1)) — 否定语句。
  • 您是否在 IDE 调试器中单步执行了代码?那是开始的地方。如果您不知道如何使用调试器,请立即开始学习。如果您要进行任何编码,这是一项必需的技能。
  • 我不知道您打算将二进制值用于什么,但您应该考虑到二进制数可以以一个或多个零开头。如果将值存储在 int 中,则会删除前导零。还要考虑当用户输入一个非常大的二进制数时会发生什么,该二进制数在解析为 int 时超过了 int 可以表示的最大值。还要考虑到您没有处理用户输入文本而不是数字的情况。最后,通常您应该通过创建易于理解的小方法将逻辑分解为单元,这将简化代码。


答案可能为时已晚。但是如果使用该方法,代码可以大大简化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner;

public class MainClass {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println(“Please enter positive integers:”);
            int userValue = scan.nextInt();
            if (isBinary(userValue)) {
                System.out.println(userValue +” is a Binary Number.”);
                break;
            } else {
                System.out.println(userValue +” is  a not Binary Number.”);
            }
        }
    }

    public static boolean isBinary(int input) {
        while (input > 0) {
            if ((input % 10 != 0) && (input % 10 != 1)) {
                return false;
            }
            input = input / 10;
        }
        return true;
    }
}


更改您的代码

从此

1
2
3
4
5
6
7
  if (notBinaryDigit > 0) {
        System.out.println(value +” is a not a Binary Number.”);
        break;
    } else {
        System.out.println(value +” is  a  Binary Number.”);

    }

到这个

1
2
3
4
5
6
7
8
 if (notBinaryDigit > 0) {
        System.out.println(value +” is a not a Binary Number.”);
        notBinaryDigit–;
    } if(binaryDigit >0 {
        System.out.println(value +” is  a  Binary Number.”);
 binaryDigit–;
  break;
    }

它会询问值并判断它是否是二进制的,如果是二进制则终止

  • 谢谢你的帮助!


由于 Scanner 类而出现错误。
您可以运行您的程序并通过像这样删除 Scanner 类来检查您的逻辑。

公共类测试{

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void main(String []args){

    int value, userValue=34;
    int binaryDigit = 0, notBinaryDigit = 0;
    value = userValue;

    while (userValue > 0) {
        if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
                binaryDigit++;
            } else {
                notBinaryDigit++;
            }

            userValue = userValue / 10;

        }

        if (notBinaryDigit > 0) {
            System.out.println(value +” is a not a Binary Number.”);

        } else {
            System.out.println(value +” is  a  Binary Number.”);

        }
}

}

  • 由于 Scanner 类没有错误,您必须先导入它
  • 大哥,我已经导入了,但是由于ide的配置,有一段时间就来了。首先,检查真正的问题是什么,然后做任何你想做的事情。
  • ide config 导致您的计算机出现问题,与上述问题无关


来源:https://www.codenong.com/47245239/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8793.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?