Java中把一个long类型的数的每一位相加并输出其和
importjava.util.Scanner;publicclassEx5_2{publicstaticintsumDIgits(longn){inttmp;intre...
import java.util.Scanner;
public class Ex5_2 {
public static int sumDIgits(long n){ int tmp;
int result = 0;
Long m = new Long(n);
while(n>0){
tmp = m.intValue();
result = tmp%10 +result;
tmp = tmp/10;
}
return result;
}
public static void main(String[] args) {
System.out.println("Please Input a long number:");
Scanner sc = new Scanner(System.in);
Ex5_2 ex = new Ex5_2();
System.out.println(ex.sumDIgits(sc.nextLong()));
}
}
帮忙看怎么输入数了以后就什么都不输出求解答啊 展开
public class Ex5_2 {
public static int sumDIgits(long n){ int tmp;
int result = 0;
Long m = new Long(n);
while(n>0){
tmp = m.intValue();
result = tmp%10 +result;
tmp = tmp/10;
}
return result;
}
public static void main(String[] args) {
System.out.println("Please Input a long number:");
Scanner sc = new Scanner(System.in);
Ex5_2 ex = new Ex5_2();
System.out.println(ex.sumDIgits(sc.nextLong()));
}
}
帮忙看怎么输入数了以后就什么都不输出求解答啊 展开
4个回答
展开全部
代码有散此问题是死循环 while(n>0){循环中没有重颂胡新给n赋过值总是大于0会一直循环的。
这样就行了
import java.util.Scanner;
public class Ex5_2 {
public static int sumDIgits(long n){
int result = 0;
while(n>0){
result = (int) (n%10 +result);
n = n/10;
}
return result;
}
public static void main(String[] args) {
System.out.println("Please Input a long number:"冲樱迅);
Scanner sc = new Scanner(System.in);
Ex5_2 ex = new Ex5_2();
System.out.println(ex.sumDIgits(sc.nextLong()));
}
}
这样就行了
import java.util.Scanner;
public class Ex5_2 {
public static int sumDIgits(long n){
int result = 0;
while(n>0){
result = (int) (n%10 +result);
n = n/10;
}
return result;
}
public static void main(String[] args) {
System.out.println("Please Input a long number:"冲樱迅);
Scanner sc = new Scanner(System.in);
Ex5_2 ex = new Ex5_2();
System.out.println(ex.sumDIgits(sc.nextLong()));
}
}
展开全部
while(n>0){
tmp = m.intValue();
result = tmp%10 +result;
tmp = tmp/10;
}
你这念历是个死循扒高稿春孝环啊。。。n永远大于0。。。
希望可以帮助你,谢谢采纳。。。
tmp = m.intValue();
result = tmp%10 +result;
tmp = tmp/10;
}
你这念历是个死循扒高稿春孝环啊。。。n永远大于0。。。
希望可以帮助你,谢谢采纳。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class Ex5_2 {
public int sumDIgits(long n){
int tmp;
int result = 0;
Long m = new Long(n);
tmp = m.intValue();
while(tmp>埋拆携弯伏0){
result = tmp%10 +result;
tmp = tmp/10;
}
return result;
}
public static void main(String[] args) {
System.out.println("御衫Please Input a long number:");
Scanner sc = new Scanner(System.in);
System.out.println(Ex5_2.sumDIgits(sc.nextLong()));
}
}
public class Ex5_2 {
public int sumDIgits(long n){
int tmp;
int result = 0;
Long m = new Long(n);
tmp = m.intValue();
while(tmp>埋拆携弯伏0){
result = tmp%10 +result;
tmp = tmp/10;
}
return result;
}
public static void main(String[] args) {
System.out.println("御衫Please Input a long number:");
Scanner sc = new Scanner(System.in);
System.out.println(Ex5_2.sumDIgits(sc.nextLong()));
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.*;
class Ex5_2
{
Ex5_2()
{
System.out.println("Please Input a long number:");
Scanner sc = new Scanner(System.in);
String s = sc.next();
int x = s.length();
int sum = 0;
for(int i = 0;i<扮羡罩x;i++)
{
int a = Integer.parseInt(s.substring(i,i+1));
sum = sum+a;
}
System.out.println(sum);
}
public static void main(String[] args)
{
new Ex5_2();
}
}
这样也派悄可以,就是有点厅闹绕
class Ex5_2
{
Ex5_2()
{
System.out.println("Please Input a long number:");
Scanner sc = new Scanner(System.in);
String s = sc.next();
int x = s.length();
int sum = 0;
for(int i = 0;i<扮羡罩x;i++)
{
int a = Integer.parseInt(s.substring(i,i+1));
sum = sum+a;
}
System.out.println(sum);
}
public static void main(String[] args)
{
new Ex5_2();
}
}
这样也派悄可以,就是有点厅闹绕
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询