Minimal a "Sum" java programing

homework
java

#1

Hi, I wanna to minimal a program (to sum the input number which less than ten and loop 10 times) by using 4 lines of code inside main(). However, I can't find the way to do this.
Please help.

Here my code:

`import java.util.Scanner;

public class sum {

/**
 * @param args
 */
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner((new Scanner(System.in)).nextLine());
	int index=0;
	
	
	for(int i=0;i<10;i++){
	
		if ((int)input.nextInt() < 10){
			index += (int)input.nextInt();
		} 
		
		
		
	
	
}

	System.out.print("The sum of all numbers smaller than 10 is: " + index);

}
}`

Thank you!


#2

Waah sounds like you are supposed to figure that out!
Count the things you need to do and put each thing on a line.
If you need to do more than 4 things, you'll have to look for ways to cram them together I suppose. In particular you'll need to find out what counts as a "line" in the problem description.

This is rather redundant isn't it?

(int)input.nextInt()

And are you sure you've read the input description correctly? Sounds like there's only one number. Or maybe you didn't paraphrase it correctly.


#4

Oops, It's doesn't work. Because it was no error in the eclipse, so I haven't test it yet.
and the following is the original version. :pensive:

`import java.util.Scanner;

public class sum {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int index=0;

for(int i=0;i<10;i++){
	int value=input.nextInt();
	if (value < 10){
		index += value;
	}

}

System.out.print("The sum of all numbers smaller than 10 is: " + index);

}
}`


#5

Then why take user input?

Or specifically out of the inputted numbers? That's not how I read that.
And what's the format of the input? If it's a single line then perhaps a one-liner can be used to get the line and split it and convert it etc
Or perhaps all lines can be read (until end of file), and likewise do a one-liner


#6

Becoz the hw requested :stuck_out_tongue:
btw, I have figure out that by using a nested for loop.
Thank You :slightly_smiling: