使用布尔函数检测连续数 公共类连续检查器 { 公共静态void main(字符串[]args) { java.util.Scanner keyboardReader=新的java.util.Scanner(System.in); int number=keyboardReader.nextInt(); int w; int x; int-y; intz; //w=第四位,x=第三位,y=第二位,z=第一位 w=(数字-(数字%1000))/1000; x=((数字-(数字%100))%1000)/10; y=((数字-(数字%10))%100)*10; z=(1000)*(数字%10); 布尔等位数字连接; IsoneDigitConcertive=(number

使用布尔函数检测连续数 公共类连续检查器 { 公共静态void main(字符串[]args) { java.util.Scanner keyboardReader=新的java.util.Scanner(System.in); int number=keyboardReader.nextInt(); int w; int x; int-y; intz; //w=第四位,x=第三位,y=第二位,z=第一位 w=(数字-(数字%1000))/1000; x=((数字-(数字%100))%1000)/10; y=((数字-(数字%10))%100)*10; z=(1000)*(数字%10); 布尔等位数字连接; IsoneDigitConcertive=(number,java,boolean,Java,Boolean,TryIstwodigitConcertive=(w==0)和&(x==0)和&(y您有两个问题: 1-正如埃米尔所说: public class ConsecutiveChecker { public static void main( String[] args ) { java.util.Scanner keyboardReader = new java.util.Scanner(System.in); int number = keyboardRead

Try
IstwodigitConcertive=(w==0)和&(x==0)和&(y您有两个问题:

1-正如埃米尔所说:

public class ConsecutiveChecker
{
     public static void main( String[] args )
    { 
    java.util.Scanner keyboardReader = new java.util.Scanner(System.in);
    int number = keyboardReader.nextInt();

    int w;
    int x;
    int y;
    int z;
    // w= fourth digit, x= third digit, y= second digit, z= first digit
    w = (number - (number % 1000)) / 1000;
    x = ((number - (number % 100)) % 1000) / 10;
    y = ((number - (number % 10)) % 100) * 10;
    z = (1000)*(number % 10);

    boolean isOneDigitConsecutive;
    isOneDigitConsecutive = (number <= 9);

    boolean isTwoDigitConsecutive;
    isTwoDigitConsecutive = (w = 0) && (x = 0) && (y <= 9) && (y - z ==1);

    boolean isConsecutive = (isOneDigitConsecutive || isTwoDigitConsecutive || isThreeDigitConsecutive || isFourDigitConsecutive)
    System.out.println(); 
}
另外,别忘了初始化变量IsThreeDigitConcertive和IsFourDigitConcertive。我理解您的担忧。 与C语言不同,java只接受带有这些运算符的布尔值

在C中,非零为真,零为假

(w=0)将返回整数0,而(w==0)将返回 true/false是布尔值


虽然您可能通过写入w=0而不是w==0而产生了逻辑错误,但C编译器在获取值时不会生成编译错误。而java需要布尔值。因此它将显示错误。

除了这里所说的一切,您的代码可以更干净、更高效。
  • 在一个单独的方法中执行“连续性”测试。它更好,以后可以在代码中重用。
  • 在可能的情况下,始终使用循环、数组和其他工具来避免重复类似的代码。在这种情况下,您基本上希望测试两个后代数字,一个对另一个,所以在循环中进行测试。
  • 检查输出、捕获(和提示)异常以及注释代码总是好的

    w = (number - (number % 1000)) / 1000;
    x = ((number - (number % 100)) % 1000) / 100;
    y = ((number - (number % 10)) % 100) / 10;
    z = (number % 10);
    
    公共类连续检查器{
    /**
    *如果数字连续增加,则返回true。
    *
    *@param num
    *@返回
    */
    私有静态布尔值是连续的(int num){
    如果(num<0 | | num>9999)抛出新的运行时异常(“输入超出范围(0-9999)”;
    //我们检查数字是否从右到左向下连续
    //(这与从左到右连续向上相同,但更简单
    //而且计算效率更高)。
    while(num>0){
    int leftDigit=num%10;
    int secondFromLeftDigit=(num/10)%10;
    如果(leftDigit
    您需要
    (w==0)
    (x==0)
    还有一些未定义的变量,如IsThreeDigitContinuous
    w = (number - (number % 1000)) / 1000;
    x = ((number - (number % 100)) % 1000) / 100;
    y = ((number - (number % 10)) % 100) / 10;
    z = (number % 10);
    
    public class ConsecutiveChecker {
        /**
         * return true if number digits are consecutively increasing.
         *
         * @param num
         * @return
         */
        private static boolean isConsecutive(int num) {
            if(num < 0 || num > 9999) throw new RuntimeException("Input out of range (0-9999)");
            // We check if the digits are downward consecutive from right to left
            // (which is same as upward consecutive from left to right, but easier
            // and more efficient to compute).
            while (num > 0) {
                int leftDigit = num%10;
                int secondFromLeftDigit = (num/10)%10;
                if (leftDigit < secondFromLeftDigit) return false;
                num = num/10;
            }
            // if number is now 0, it's consecutive:
            return true;
        }
    
        public static void main(String[] args) {
            try {
                java.util.Scanner keyboardReader = new java.util.Scanner(System.in);
                int number = keyboardReader.nextInt();
    
                System.out.println(
                                "Input number is " + 
                                (isConsecutive(number) ? "" : "not ") + 
                                "consecutive"
                        );
            } catch (Exception e) {
                System.out.println("Somthing is wrong with the input: ");
                e.printStackTrace();
            }
        }
    }