forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise067.java
More file actions
43 lines (37 loc) · 1.11 KB
/
Exercise067.java
File metadata and controls
43 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Scanner;
/**
* Created by media on 2016-07-05.
*/
public class Exercise067 {
public static boolean isPositiveAndOdd(int checkedNumber){
if(checkedNumber < 0){
System.out.print("Input number is less than 0");
return false;
} else if(checkedNumber%2 == 0){
System.out.print("Input number is even");
return false;
} else {
return true;
}
}
public static int oddSum(int maxComponent){
if(maxComponent == 1){
return 1;
} else{
return maxComponent + oddSum(maxComponent-2);
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int userNumber = 0;
int sum = 1;
int component =1;
while (!isPositiveAndOdd(userNumber)){
System.out.print("\nWrite maximal number: ");
userNumber = in.nextInt();
}
System.out.printf("\nYou selected: %d", userNumber);
int answer = oddSum(userNumber);
System.out.printf("\nYour answer is: %d", answer);
}
}