如何分隔数字中的每个数字?

时间:2015-03-17 03:44:36

标签: java for-loop

我正在做关于制作程序的家庭作业,该程序允许用户进入低值和高值范围,并且该程序假设找到所有数字,这些数字是其立方数字的总和(IE 153 = 1 ^ 3 + 5 ^ 3 + 3 ^ 3)并且必须使用for循环。我一直在想弄清楚如何解决这个问题,我显然需要将每个数字立方体的每个数字分开,将它们加在一起,然后检查它是否等于值,如果不重复,那么&#39 ;据我所知,明智的帮助将受到赞赏。

/* 
*   problem #17 on page 367
*   Program Description - finding numbers that equal the sum of their cubed numbers
*   Author: Jonathan Wilson
*   Date:   2/24/2015
*   Filename:   cube.java
*/


//import statements
import java.util.*;     //for scanner class 


// class beginning
class Cube {
    public static void main(String[] args ) { 
        //Declare variables area

        int low;
        int high;
        int value;
        Scanner scan = new Scanner (System.in); 


        //Program beginning messages to user here
        System.out.println("Welcome to my special number program!");
        System.out.println("Enter a low and high value range");
        System.out.println("and this program will find all the numbers that equal the sum of their cubed digits");

        //Collect inputs from user or read in data here


        System.out.println("Enter in the low starting range ");
        low=scan.nextInt();  //storing low range as low
        System.out.println();  //break
        System.out.println("Enter in the high ending range ");
        high=scan.nextInt();  //storing the high range as high
        System.out.println();  //break


        // A check to see that the range entered is a legal range, if so then it moves on to print out range and goes
        // into main code

        while (high < low){//checks to see if the inputs are valid and that the user doesn't try to do an inverse range
            System.out.println();
            System.out.println("Incorrect Range try again!");
            System.out.
            low = scan.nextInt();
            System.out.print(Low);
            System.out.println();//break
            System.out.print("Please enter the higher number in the range... ");
            high = scan.nextInt();

            //Echo input values back to user here
            System.out.println("Okay, so the range of numbers to look through are "+Low+ " to "+High+" lets start!");

            // Main code and calculations to do
            for (count= low; count <= high; count ++){


                //Output results here




                //End program message
                System.out.println();
                System.out.println("Hope you enjoyed using this program!");
            }// end main method 

        }
    }   
}// end class

1 个答案:

答案 0 :(得分:0)

您可以使用%或mod工具隔离数字。然后,要转到下一个数字,您可以将数字除以10,从而使最后一位数字消失。此过程可用于查看数字的数字。

//import statements
import java.util.*; //for scanner class 
// class beginning
public class Test {
    public static void main(String[] args) {
        // Declare variables area

        int low;
        int high;
        int value;
        Scanner scan = new Scanner(System.in);

        // Program beginning messages to user here
        System.out.println("Welcome to my special number program!");
        System.out.println("Enter a low and high value range");
        System.out
                .println("and this program will find all the numbers that equal the sum of their cubed digits");

        // Collect inputs from user or read in data here

        System.out.println("Enter in the low starting range ");
        low = scan.nextInt();// storing low range as low
        System.out.println();// break
        System.out.println("Enter in the high ending range ");
        high = scan.nextInt();// storing the high range as high
        System.out.println();// break

        // A check to see that the range entered is a legal range, if so then it
        // moves on to print out range and goes
        // into main code

        while (high < low) {// checks to see if the inputs are valid and that
                            // the user doesn't try to do an inverse range
            System.out.println();
            System.out.println("Incorrect Range try again!");
            low = scan.nextInt();
            System.out.print(low);
            System.out.println();// break
            System.out.print("Please enter the higher number in the range... ");
            high = scan.nextInt();
        }
        // Echo input values back to user here
        System.out.println("Okay, so the range of numbers to look through are "
                + low + " to " + high + " lets start!");

        // Main code and calculations to do
        for (int count = low; count <= high; count++) {
            int sum=0;
            int original = count;
            while(count>0){
                sum+=(count%10)*(count%10)*(count%10);
                count/=10;
            }
            if(sum==original){
                System.out.println(original+" works as a number!");
            }
            count = original;
        }


        // Output results here

        // End program message
        System.out.println();
        System.out.println("Hope you enjoyed using this program!");
    }// end main method
}// end class