Java 如何将数字中的每个数字分开?
我正在做家庭作业,制作一个程序,允许用户输入一个低值和高值范围,该程序应该找到所有的数字,这些数字是它们的立方数字之和(即153=1^3+5^3+3^3),必须使用for循环。我一直在想怎么做,我显然需要把数字的每一个数字分开,把每一个数字的立方体放在一起,把它们加在一起,然后检查它是否等于这个值,如果不重复的话,但这是我所得到的计划方面的帮助,我将不胜感激Java 如何将数字中的每个数字分开?,java,for-loop,Java,For Loop,我正在做家庭作业,制作一个程序,允许用户输入一个低值和高值范围,该程序应该找到所有的数字,这些数字是它们的立方数字之和(即153=1^3+5^3+3^3),必须使用for循环。我一直在想怎么做,我显然需要把数字的每一个数字分开,把每一个数字的立方体放在一起,把它们加在一起,然后检查它是否等于这个值,如果不重复的话,但这是我所得到的计划方面的帮助,我将不胜感激 /* * problem #17 on page 367 * Program Description - finding num
/*
* 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
/*
*第367页第17题
*程序说明-查找等于其立方数之和的数
*作者:乔纳森·威尔逊
*日期:2015年2月24日
*文件名:cube.java
*/
//导入语句
导入java.util.*//扫描器类
//开课
类立方体{
公共静态void main(字符串[]args){
//声明变量区域
int低;
int高;
int值;
扫描仪扫描=新扫描仪(System.in);
//在此处向用户发送程序开始消息
System.out.println(“欢迎使用我的特殊号码程序!”);
System.out.println(“输入低值和高值范围”);
System.out.println(“此程序将查找所有等于其立方数字之和的数字”);
//在此处收集用户输入或读取数据
System.out.println(“进入低启动范围”);
low=scan.nextInt();//将下限存储为low
System.out.println();//中断
System.out.println(“输入高端范围”);
high=scan.nextInt();//将高范围存储为high
System.out.println();//中断
//检查输入的范围是否为合法范围,如果是,则转到打印范围并继续
//输入主代码
while(high 对于(count=low;count您可以使用%或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
//导入语句
导入扫描器类的java.util.*;//文件
//开课
公开课考试{
公共静态void main(字符串[]args){
//声明变量区域
int低;
int高;
int值;
扫描仪扫描=新扫描仪(System.in);
//在此处向用户发送程序开始消息
System.out.println(“欢迎使用我的特殊号码程序!”);
System.out.println(“输入低值和高值范围”);
系统输出
.println(“此程序将查找所有等于其立方数字之和的数字”);
//在此处收集用户输入或读取数据
System.out.println(“进入低启动范围”);
low=scan.nextInt();//将下限存储为low
System.out.println();//中断
System.out.println(“输入高端范围”);
high=scan.nextInt();//将高范围存储为high
System.out.println();//中断
//检查输入的范围是否为合法范围,如果是,则检查是否为合法范围
//继续打印输出范围并继续
//输入主代码
while(高<低){//检查输入是否有效,并且
//用户不尝试进行反向范围
System.out.println();
System.out.println(“范围不正确,请重试!”);
low=scan.nextInt();
系统输出打印(低);
System.out.println();//中断
System.out.print(“请输入范围内较高的数字…”);
高=扫描.nextInt();
}
//在此处将输入值回显给用户
System.out.println(“好的,那么要查看的数字范围是”
+低+“到”+高+“让我们开始吧!”);
//主要代码和要做的计算
对于(整数计数=低;计数0){
总和+=(计数%10)*(计数%10)*(计数%10);
计数/=10;
}
如果(总和==原始值){
System.out.println(原件+“作为一个数字工作!”);
}
计数=原件;
}
//在此处输出结果
//结束程序消息
System.out.println();
println(“希望您喜欢使用这个程序!”);
}//端主方法
}//末级
你可以看看哪个可以让你得到一个数字的各个数字。谷歌“从数字java中提取数字”知道如何从一个数字中得到每个数字。你能给一个更有意义的标题吗?除非你的故事对这个问题有任何重要性,否则我认为没有人有兴趣知道。请保持你的问题清晰简洁。顺便说一句,对于一个懂Python的人来说,源代码缩进很差是令人难以置信的…:(当我清理您的问题时,发现您的程序甚至无法编译…当您试图寻求其他人的帮助时,您能否更小心一些?您是否碰巧运行了此代码?它看起来是无限的。抱歉,忘记检查代码,但是,我发现了错误。在if(sum==original)之后,我需要将count设置为original。