This commit is contained in:
ldy
2026-03-01 23:18:55 -05:00
commit 67f753a5d1
3087 changed files with 218259 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Question1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@@ -0,0 +1,57 @@
public class SumAndAverage {
public static void main(String[] args) {
int sum = 0; // Store the accumulated sum init to 0
double average; // average in double
int lowerbound = 1; // The lowerbound to sum
int upperbound = 100; // The upperbound to sum
int cntOdd = 0;
// Use a for-loop to sum from lowerbound to upperbound
for(int number = lowerbound; number <= upperbound; number+=2) { //only odd num: increment = 2
sum += number; // same as "sum = sum + number"
++cntOdd;
}
// Compute average in double. Beware that int/int produces int.
average = (double) sum / cntOdd;
// Print sum and average
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
// 6.1 while loop
System.out.println("---- While-loop:");
sum = 0;
int number = lowerbound;
int cnt_7 = 0;
while(number <= upperbound) {
if(number % 7 == 0) { //when can divided by 7, add sum and count
sum += number;
++cnt_7;
}
number++;
}
// Compute average in double. Beware that int/int produces int.
average = (double) sum / cnt_7;
// Print sum and average
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
// 6.2 do-while
System.out.println("---- Do-while:");
sum = 0; // Reset sum
number = lowerbound;
do {
sum += number * number;
number++;
} while(number <= upperbound);
// Compute average in double. Beware that int/int produces int.
average = (double) sum / (upperbound - lowerbound + 1);
// Print sum and average
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
}
}