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