-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopTest2.java
More file actions
114 lines (86 loc) · 2.38 KB
/
Copy pathLoopTest2.java
File metadata and controls
114 lines (86 loc) · 2.38 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.*;
public class LoopTest2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/*
System.out.println("Enter the size of the square:");
int squareSize = scan.nextInt();
for(int i=0; i<squareSize; i++) {
for(int j=0; j<squareSize; j++) {
System.out.print("X");
}
System.out.println();
}
*/
/* print the reverse of the String*/
System.out.println("Enter something!");
String userInput = scan.nextLine();
int length = userInput.length();
for(int i=length-1; i>=0; i--) {
System.out.print(userInput.substring(i, i+1));
//System.out.println(userInput.charAt(i));
}
/* count zeros, evens, odds with arithmetic */
/*
int numZeros = 0;
int numEvens = 0;
int numOdds = 0;
System.out.println("Enter a number:");
int userNum = scan.nextInt();
while(userNum>0) {
int onesDigit = userNum % 10;
if(onesDigit==0) {
numZeros++;
}
if(onesDigit%2==0) {
numEvens++;
} else {
numOdds++;
}
userNum = userNum / 10;
}
*/
/* count zeros, evens, odds with chars- INCORRECTLY */
/*
String numberString = scan.nextLine();
int numberStringLength = numberString.length();
for(int i=0; i<numberStringLength; i++) {
char c = numberString.charAt(i);
System.out.println("the char is " + c);
int cNum = (int) c;
System.out.println("the number is " + cNum);
if(c=='0') {
numZeros++;
}
if(c%2 == 0) {
numEvens++;
} else if(c%2 == 1) {
numOdds++;
}
*/
/* count zeros, evens, odds with chars- CORRECTLY */
/*
if(c == '0' || c == '2' || c=='4' || c=='6' || c=='8') {
numEvens++;
} else if(c == '1' || c == '3' || c == '5' || c == '7' || c == '9') {
numOdds++;
}
*/
/* count zeros, evens, odds with Strings */
/*
String singleCharSubstring = numberString.substring(i, i+1);
// will get the substring at position i up to but not including position i+1
// substring has length 1
int num = Integer.parseInt(singleCharSubstring);
if(num==0) {
numZeros++;
}
if(num%2 == 0) {
numEvens++;
} else {
numOdds++;
}
} */
// System.out.println(numZeros + " zeros, " + numEvens + " evens, and " + numOdds + " odds.");
}
}