1115Digital Roots ACM Answer Java edition

Source: Internet
Author: User

The problem starts running on its own, submit runtime error. Very puzzled, the online search is generally an array out of bounds, and then test themselves, found that when the number of input is too large, more than int

Range of Time Integer.parseint (ST) will be error. So the program has been modified, then accepted.

Import Java.util.Scanner;

public class DigitalRoots1115 {

public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
String st = Sc.nextline ();
while (true) {
if (St.length () <2)
if (Integer.parseint (st) = = 0)
Break
Calculate (ST);
st = Sc.nextline ();
}

}

public static void Calculate (String st) {
int sum = 0;
int length = St.length ();
Char [] a =st.tochararray ();
for (int i = 0; i < length; i++) {
Sum+=integer.parseint (a[i]+ "");
}
if (Sum < 10)
SYSTEM.OUT.PRINTLN (sum);
Else
Calculate (string.valueof (sum));
}

}

1115Digital Roots ACM Answer Java edition

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.