我再次陷入了一个程序。
public int sumNumber(int a , int b, int c)
{
int line = a+b+c;
return line;
}
public int sumNumber (int b , int c )
{
int total = b +c;
return total;
}
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
Assignment12 a12 = new Assignment12();
int y = a12.sumNumber(1, 2, 3);
System.out.println("Enter two three-digit numbers");
int num1 = in.nextInt(y), num2 = in.nextInt(y);
int x = a12.sumNumber(0, 1);
System.out.println("The sum of the digits is : "+ y );
我想添加我输入的数字,然后添加第二组。 输出应该是这样的:
Enter two three-digit numbers
521
412
sum = 15
澄清正在发生的事情:
Enter 2 three digit numbers
5+2+1 = 8
4+1+2 = 7
then it adds 8 and 7
sum = 15
我故意重载了......另外你们能不能让它尽可能简单(我还在学习)
最佳答案
使用两种方法而不是一种方法
一个表示数字位数和
第二个数字和
数字和--
public int digitSum(int a)
{
int sum=0;
while(a!=0) //while(a)
{
sum += a%10;
a /= 10;
}
return sum;
}
数字和
public int sum(int a,int b)
{
// You can Get Digits Sum Here or Before any Where by just calling above method
return digitSum(a)+digitSum(b);
}
关于java - 在同一输入行添加 3 个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36122890/