Home > Back-end >  O n the sum of nonnegative integers
O n the sum of nonnegative integers

Time:10-07

Subject content:
Input nonnegative integer n, and output them, 1 & lt;=n<=100, while each number & lt; 800, does not meet the requirements of the input and output message: "error",

Input format:
Input consists of two lines, the first line, including an integer n, said a total of n number, the second line: contains n integers,

The output format:
Output n number and,

Input the sample:
4
1 2 3 4

The output sample:
10
Time limit: 500 ms memory limit: 32000 KB

CodePudding user response:

import java.util.Scanner;
The abstract class Parent {
Private int sum=0;
Private Scanner item=new Scanner (System. In);
Private Scanner items=new Scanner (System. In);
Public Parent () {
System. The out. Println (" enter a positive integer ");
String STR=item. The next ();
For (int x=0; xChar s=STR. CharAt (x);
if(! Character. IsDigit (s)) {
System. The out. Println (" Error ");
return;
}
}
Int n=Integer. The valueOf (STR);
If (n<1 | n> 100) {
System. The out. Println (" Error ");
return;
}
System. Out. Println (" input "+ n +" integer less than 800, separated by a space ");
String [] STRS=items. NextLine (). The split (" \ \ s + ");
For (String x: STRS) {
Int tem=Integer. The valueOf (x);
If (tem> 800) {
System. The out. Println (" Error ");
return;
} else {
The sum +=tem;
}
}
System.out.println(sum);
}
}
The class Sub extends the Parent {
Public static void main (String [] args) {
The new Sub ();
}
}
  • Related