编写程序:编写一个方法, 计算一个整数各位数字之和。使用下面的方法头: public static int sumDigits(long n) 例如:调用 sumDigits(234)返回 9。
编写程序:编写一个方法, 计算一个整数各位数字之和。使用下面的方法头: public static int sumDigits(long n) 例如:调用 sumDigits(234)返回 9。
参考答案和解析
正确
相关考题:
●试题八阅读以下说明和Java代码,将解答写入答题纸的对应栏内。【说明】下面的程序中定义了两个方法求自然数1~100的和。具体如下:int sum1(int n);利用循环求1~n的和,int sum2(int n);利用递归方法求和1~n的和;在main()方法中调用这两个方法求1~100的和并显示。在程序的每条横线处填写一个适当的语句,使程序的功能完整。public class Sum{public static void main (1){//1.调用sum1(int n),求1~100的和//标准输出(2) ("1~100的和:"+sum1(100));//2.调用sum2(int n),求1~100的和//标准输出(2) ("1~100的和:"+sum2(100));}static int sum1(int n){int result=0;for(int i=1;i=n;i++)(3)retrun result;}static int sum2(int n){if (4)return 1;else(5)}}
阅读以下说明和Java代码,将解答写入对应栏内。【说明】下面的程序中定义了两个方法求自然数1~100的和。具体如下:int suml(int n);利用循环求1~n的和,int sum2(int n);利用递归方法求和1~n的和;在main()方法中调用这两个方法求1~100的和并显示。在程序的每条横线处填写一个适当的语句,使程序的功能完整。public class Sum {public static void main (1){//1. 调用sum1(int n),求1~100的和//标准输出(2) ("1~100的和:" +sum1(100));//2. 调用sum2(int n),求1~100的和//标准输出(2) ("1~100的和:"+sum2(100));}static iht sum1( int n){int result=0;for(int i=1;i<=n;i++)(3)retrun result;}static int sum2(int n){if (4)return 1else(5)}}
下列程序打包到example包,main方法调用线程类输出0~9这10个数,请填写横线处的内容。注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。______interface MyInterface{public abstract void print(int n);}class Mythread extends Thread ______ MyInterface{public void run(){for(int i = 0; i < 10; i++)this.print(i);}public void print(int n){System.out.print(n +" ");}}public class Example1_6{public static void main(String argv[]){Mythread th = new Mythread();______}}
下面的程序是10000以内的“相亲数”。所谓相亲数是指这样的一对数:甲数的约数之和等于乙数,而乙数的约数等于甲数,(例如220和284是一对相亲数)请在程序的每条横线处填写一条语句,使程序的功能完整。注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。public class QinShu{public static void main(String args[ ]){for(int n=1;n<9999;n++){int s=divsum(n);if( )System.out.println(n+","+s);}}public static int divsum(int n){//该方法的功能是求一个数的所有约数int s=0;for(int i=1;____________________i++)if(____________________)s+=i;return s;}}
阅读以下函数说明和C语言函数,将应填入(n)的字句写在答题纸的对应栏内。[说明1]函数int fun1(int m, int n)的功能是:计算并返回正整数m和n的最大公约数。[函数1]int fun1(int m, int n){while ((1)) {if (m>n) m=m-n;else n=n-m;}(2);}[说明2]函数long fun2(char*str)的功能是:自左至右顺序取出非空字符串str中的数字字符形成一个十进制整数(最多8位)。例如,若字符串str的值为“f3g8d5.ji2e3p12fkp”,则函数返回值为3852312。[函数2]long fun2(char *str){int i=0;long k=0;char *p=str;while (*p!='\0' (3)) {if (*p>='0' *p<='9') {k=(4)+ *p - '0';++i;}(5);}return k;}
下面的程序是用do_while语句计算10的阶乘。请在程序的每条横线处填写一个语句,使程序的功能完整。注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。源程序文件代码清单如下:public class DoWhileLoop{public static void main(________){int n=10;long result=1;do{_______________}______________System.out.println("10的阶乘为: "+result);}}
阅读下面利用递归来求n!的程序 class FactorialTest { static long Factorial(int n){ //定义Factorial()方法 if(n==1)return 1; else return n * Factorial(______); } public static void main(String a[]) { //main()方法 int n=8; System.out.println(n+"!="+Factorial(n)); } } 为保证程序正确运行,在下画线处应该填入的参数是A.n-1B.n-2C.nD.n+1
下面的程序中定义了两个方法求自然数1~100的和。具体如下:int suml(int n);利用循环求1~n的和,int sum2(int n);利用递归方法求和1~n的和;在main()方法中调用这两个方法求1~100的和并显示。在程序的每条横线处填写一个适当的语句,使程序的功能完整。public class Sum{public static void main(String args[]){//1.调用suml(int n),求1~100的和System.out.println("1~100的和:"+sum1(100));//2,调用sum2(int n),求1~100的和System.out.println("1~100的和:"+sum2(100));}static int suml(int n){int result=0;for(int i=1;i<=n;i++)________________retrun result;}static int sum2(int n){if(______________)return 1;else_____________}}
请编写一个函数fun(),它的功能是将一个数字字符串转换为一个整数(不得调用C语言提供的将字符串转为整数的函数)。例如,若输入字符串“-1234”,则函数把它转换为整数值 -1234。注意:部分源程序给出如下。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。试题程序:include <stdio.h>include <string.h>long fun(char *p){}main ( ){char s[6];long n;printf("Enter a string:\n");gets(s);n=fun(s);printf("%ld\n",n);}
下面的程序是求9999以内的“完全数”。所谓完全数是指这样的自然数:它的各个约数(不包括该数自身)之和等于该数自身。如28=1+2+4+7+14就是一个完全数。请在程序的每条横线处填写一个语句,使程序的功能完整。注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。public class QuanShu{public static void main(String args[]){for(int n=l;n<9999;n++)if(______________)System.out.println(n);}public static iht divsum(int n){//该方法功能是求一个数的所有约数int s=0;for(int i=l;i<n;i++)if(_________________)__________________return s;}}
本题利用递归方法求前n个自然数的和(n=lO)。 public class javal{ public static void main(String[]args){ int sum=add(10): System.out.println("1+2+…+9+10="+ sum); } public static int add( ){ if(n= =l){ ; } else ; } }
本题定义了一个求两个数的最大值的方法max,并调用该方法计算67和23的最大值。 public class javal{ public static void main(String[]args){ javal temp=new javal; int res=max(67,23); System.out.println("res="+res); } static int maX( ){ int maxNum; if(ab) ; else maxNum=b; ; } }
指出下列哪个方法与方法public void add(int a){}为错误的重载方法()A、public int add(int a)B、public void add(long a)C、public int add(long a)D、public void add(float a)
本题定义了一个方法add(),用于求两个整形数的和。方法中有两个整形参数a和b,方法体中计算a和b的和sum,并将结果返回。程序中调用add()方法求整数24和34的和,并将结果打印输出。public class javal{public static void main(String[]args){int a=24,b=34;System.out.println(add(a,b));}public static int add( ){;sum—a+b;;}}
下列哪些是方法public int add (int a)的重载方法?() A、 public int add (long a);B、 public void add (int a);C、 public void add (long a);D、 public int add (float a);
为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为()A、 static void method( )B、 public void method( )C、 final void method( )D、 abstract void method( )
哪一行定义了一个静态变量?()A、public static int i;B、static public int i;C、public int static i;D、int public static i;
M类中有一个没有形式参数,且没有返回值的方法method,若要使得用M.method()就可以调用该方法,则method方法的方法头的正确形式应该是()。A、static void method()B、public method()C、final void method()D、static method()
下列方法定义中,方法头不正确的是()。A、public static x(double a)B、public static int x(double y)C、void x(double d)
main方法是Java程序执行的入口点,关于main方法的方法头以下哪项是合法的()?A、public static void main( )B、public static void main( String args[] )C、public static int main(String [] arg )D、public void main(String arg[] )
public class Yikes { public static void go(Long n) {System.out.println(”Long “);} public static void go(Short n) {System.out.println(”Short “);} public static void go(int n) {System.out.println(”int “);} public static void main(String [] args) { short y= 6; long z= 7; go(y); go(z); } } What is the result?() A、 int LongB、 Short LongC、 Compilation fails.D、 An exception is thrown at runtime.
public class Wow { public static void go(short n) {System.out.println(”short”); } public static void go(Short n) {System.out.println(”SHORT”);} public static void go(Long n) {System.out.println(” LONG”); } public static void main(String [] args) { Short y= 6; int z=7; go(y); go(z); } } What is the result?() A、 short LONGB、 SHORT LONGC、 Compilation fails.D、 An exception is thrown at runtime.
单选题public class Wow { public static void go(short n) {System.out.println(”short”); } public static void go(Short n) {System.out.println(”SHORT”);} public static void go(Long n) {System.out.println(” LONG”); } public static void main(String [] args) { Short y= 6; int z=7; go(y); go(z); } } What is the result?()A short LONGB SHORT LONGC Compilation fails.D An exception is thrown at runtime.
单选题public class Yikes { public static void go(Long n) {System.out.println(”Long “);} public static void go(Short n) {System.out.println(”Short “);} public static void go(int n) {System.out.println(”int “);} public static void main(String [] args) { short y= 6; long z= 7; go(y); go(z); } } What is the result?()A int LongB Short LongC Compilation fails.D An exception is thrown at runtime.
单选题为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为()A static void method( )B public void method( )C final void method( )D abstract void method( )
问答题请编写一个函数fun,它的功能是:将一个表示正整数的数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。例如,若输入字符串“1234”,则函数把它转换为整数值1234。函数fun中给出的语句仅供参考。 注意:部分源程序存在文件PROG1.C文件中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。/**********code.c**********/#include #include long fun(char *p){ int i,len;/* len为串长*/ long x=0; len=strlen(p); /*以下完成数字字符串转换为数字,注意字符’0’不是数字0*/ return x;}void main() { char s[6]; long n; printf(Enter a tring:); gets(s); n = fun(s); printf(%ld,n);}