Java Classroom Test 2 (two ways)

Source: Internet
Author: User

Experiment Source Code

This is a source code that does not use an array form

/*
2017/10/10 Wang Classroom Test 2
*/
Import Java.util.Scanner;

public class Number {



public static void Main (String args[]) {
Num n=new num ();
System.out.println ("Please enter an integer:");
Scanner input=new Scanner (system.in);
int Num=input.nextint ();
SYSTEM.OUT.PRINTLN ("digit" +n.numweishu (num));



}

}
Class num{

public int Numweishu (int a) {
int sum=0;
int num1;//to find the remainder 2
int num2;//Take integer
int Weishu;
num1=a%10; Take the last one.
NUM2=A/10; Take away all the last one
SUM=SUM+NUM1; Sum
Weishu=1; Statistics Number of digits
System.out.println (NUM1); Enter each bit
while (num2!=0)//Remove the last one without a number and then jump out of the loop
{
num1=num2%10;
NUM2=NUM2/10;
SUM=SUM+NUM1;
weishu=weishu+1;
System.out.println (NUM1);

}

System.out.println ("sum" +sum);
return Weishu;

}

}

Using the array source code

/*
2017/10/10 Wang Classroom Test 2
*/
Import Java.util.Scanner;

public class Number {



public static void Main (String args[]) {
Num n=new num ();
System.out.println ("Please enter an integer:");
Scanner input=new Scanner (system.in);
int Num=input.nextint ();
System.out.println ("\ n number of digits" +n.numweishu (num));


}

}
Class num{
public int num[];
public int Numweishu (int a) {
int num[]=new int[10];
int num1;//to find the remainder 2
int num2;//Take integer
int Weishu;
num1=a%10; Take the last one.
NUM2=A/10; Take away all the last one
NUM[0]=NUM1;
Weishu=1; Statistics Number of digits
while (num2!=0)//Remove the last one without a number and then jump out of the loop
{
num1=num2%10;
NUM2=NUM2/10;
NUM[WEISHU]=NUM1;
weishu=weishu+1;

}

int sum;
for (int i=weishu-1;i>=0;i--) {

Sum+=num[i];
System.out.print (num[i]+ "\ t");
}

System.out.print ("\ n sum result is" +sum);
return Weishu;

}


}

Java Classroom Test 2 (two ways)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.