Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java程序根据条件打印范围内的数字_Java_Loops_Range - Fatal编程技术网

Java程序根据条件打印范围内的数字

Java程序根据条件打印范围内的数字,java,loops,range,Java,Loops,Range,我对循环练习有点迷茫,我必须打印符合以下条件的范围内的数字: 一个数字的每个数字必须与他左边的数字之和相同。例如: 112将被打印,因为它的所有组成部分都是他左边数字的总和 101将不会被打印,因为0不是他左边的数字之和 如果总和大于9,则将丢弃十个: 将印刷5500件 我有以下代码: public class Program { /** * Prints the list of numbers from desde to hasta such that the ri

我对循环练习有点迷茫,我必须打印符合以下条件的范围内的数字:

  • 一个数字的每个数字必须与他左边的数字之和相同。例如:

    • 112将被打印,因为它的所有组成部分都是他左边数字的总和

    • 101将不会被打印,因为0不是他左边的数字之和

  • 如果总和大于9,则将丢弃十个

    • 将印刷5500件
  • 我有以下代码:

    public class Program {
    
        /**
         * Prints the list of numbers from desde to hasta such that the righmost
         * digit of the sum of previous digits equals current digit, and that
         * happens for all digits
         * 
         * Prints a 2nd line with as many underscores as characters contains the 1st
         * line
         * 
         * If desde < 10 o the parameters make no sense, prints Err __
         * 
         * 
         * @param desde
         *            int from (included)
         * @param hasta
         *            int to (included)
         * @return void
         */
        public static void sumDigitsConsecutius(int desde, int hasta) {
    
            int suma = 0;
            int numeroSuma = 0;
    
            for (int i = desde; i <= hasta; i++) {
    
                int longitud = lenght(i);
                int posicion = longitud - 1;
                int contador = 0;
    
                for (double numComparar = posicioNumber(i, posicion); posicion > 0; posicion--) {
    
                    numeroSuma = posicioNumber(i, lenght(i));
                    suma = suma + numeroSuma;
                    if (suma != numComparar) {
                        break;
                    } else {
    
                        numeroSuma = posicioNumber(i, (lenght(i) - 1));
                        contador++;
                    }
                }
    
                if (contador == longitud - 1) {
                    System.out.println(i);
                }
    
            }
    
        }
    
        /**
         * With this method i can calculate the length of a number
         */
    
        public static int lenght(int numero) {
    
            int contador = 0;
    
            while (numero > 0) {
                contador++;
                numero = numero / 10;
            }
            return contador;
    
        }
    
        public static int posicioNumber(int numero, int posicio) {
    
            // Digit of n in position p
            int x;
            // Remove right digits from position p.
            x = numero / (int) Math.pow(10, posicio - 1);
            // Get the last digit.
            x = x % 10;
            return x;
    
        }
    
        public static void main(String[] args) {
            sumDigitsConsecutius(80, 120);
        }
    }
    
    公共类程序{
    /**
    *打印从desde到hasta的数字列表,以便
    *前面的数字之和等于当前数字,并且
    *发生在所有数字上
    * 
    *打印第二行,下划线的数量与包含第一行的字符的数量相同
    *线
    * 
    *如果desde<10 o参数没有意义,则打印错误__
    * 
    * 
    *@param desde
    *int from(包括在内)
    *@param hasta
    *整数到(包括在内)
    *@返回无效
    */
    公共静态无效SumdigitsConcertius(int desde,int hasta){
    int-suma=0;
    整数=0;
    对于(int i=desde;i 0;posicion--){
    numeroSuma=位置编号(i,长度(i));
    suma=suma+数值;
    if(suma!=numComparar){
    打破
    }否则{
    numeroSuma=位置数(i,(长度(i)-1));
    contador++;
    }
    }
    if(contador==longitud-1){
    系统输出打印LN(i);
    }
    }
    }
    /**
    *用这个方法我可以计算一个数字的长度
    */
    公共静态整数长度(整数){
    int contador=0;
    而(数字>0){
    contador++;
    numero=numero/10;
    }
    返回康塔多;
    }
    公共静态int-posicio编号(int-numero,int-posicio){
    //p位n的位数
    int x;
    //从位置p删除右数字。
    x=数值/(整数)数学功率(10,位置-1);
    //获取最后一个数字。
    x=x%10;
    返回x;
    }
    公共静态void main(字符串[]args){
    SumdigitsConcerutius(80120);
    }
    }
    
    我创建了另外两个方法,第一个计算数字的长度,第二个根据位置计算数字的值


    有什么想法吗?

    我仔细阅读了您的代码,找出了逻辑上的一些问题,我已经纠正了这些问题。请阅读下面的代码,检查您是否理解这些更改。(请记住,因为您的变量不是英语的,所以很难理解您的逻辑)。下面的代码将给出您所需的结果

    public class Program {
    
    /**
     * Prints the list of numbers from desde to hasta such that the righmost
     * digit of the sum of previous digits equals current digit, and that
     * happens for all digits
     * 
     * Prints a 2nd line with as many underscores as characters contains the 1st
     * line
     * 
     * If desde < 10 o the parameters make no sense, prints Err __
     * 
     * 
     * @param desde
     *            int from (included)
     * @param hasta
     *            int to (included)
     * @return void
     */
    public static void sumDigitsConsecutius(int desde, int hasta) {
    
        int suma = 0;
        int numeroSuma = 0;
    
        for (int i = desde; i <= hasta; i++) {
    
            int longitud = lenght(i);
            int contador = 0;
    
            suma = 0;
            for (int posicion = longitud; posicion > 0; posicion--) {
    
    
                numeroSuma = posicioNumber(i, posicion);
                if (posicion !=longitud && suma != numeroSuma) {
                    break;
                } else {
                    contador++;
                }
                suma = suma + numeroSuma;
    
                //If the sum greater than 9 then position discarded 
                if(suma>=10){
                    suma=suma%10;
                }
            }
    
            if (contador == longitud ) {
                System.out.println(i);
            }
    
        }
    
    }
    
    /**
     * With this method i can calculate the length of a number
     */
    
    public static int lenght(int numero) {
    
        int contador = 0;
    
        while (numero > 0) {
            contador++;
            numero = numero / 10;
        }
        return contador;
    
    }
    
    public static int posicioNumber(int numero, int posicio) {
    
        // Digit of n in position p
        int x;
        // Remove right digits from position p.
        x = numero / (int) Math.pow(10, posicio - 1);
        // Get the last digit.
        x = x % 10;
        return x;
    
    }
    
    public static void main(String[] args) {
        sumDigitsConsecutius(1, 1000);
    }
    
    
    }
    

    这不是一个可以让人帮你做作业的网站。请问一个具体的问题,我只是想知道为什么这个程序不打印任何东西,我不知道它在哪里失败。当你的代码不是英文的时候,阅读它真的很困难。你应该看看你的语言是否有一个SE站点。你通过调试程序来检查你的逻辑了吗?你能检查提供的答案吗
    1
    2
    3
    4
    5
    6
    7
    8
    9
    11
    22
    33
    44
    55
    66
    77
    88
    99
    112
    224
    336
    448
    550
    662
    774
    886
    998