Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
  
Lab 3A: For Loops 
 
Introduction: 
 
For loops were added to Java (and many other languages) as a compact alternative to the while 
loop. This was accomplished by combining several parts of the while loop into the first line of 
the for loop. As you know, the Java syntax of the while loop is: 
 
initialization 
while ( logical expression ) 
{ 
   block of code to be repeated 
   update variables 
} 
 
A for loop shortens this code by placing the initialization, the logical expression, and code to 
update variables used in the logical expression on one line. This is followed by the block of code 
to be repeated. The Java syntax of the for loop is: 
 
for ( initialization ; logical expression ; update variables ) 
{ 
   block of code to be repeated 
} 
 
The order in which parts of the for loop are executed is exactly the same as a while loop: 1) 
initialization, 2) logical expression, 3) block of code to be repeated, and 4) update variables. 
Steps 2,3,4 are repeated as long as the logical expression remains true. Thus any while loop can 
be converted into a for loop and vice versa. For loops typically reduce the number of lines in a 
program, making the code easier to read and debug. 
 
Instructions: 
 
Consider the following Java program. It starts by prompting the user to enter a number. Then it 
uses a for loop to print out some numbers based on the users input. 
 
import java.util.Scanner; 
public class Main 
{ 
    public static void main(String[] args) 
    { 
       // Get number from user 
       Scanner scnr = new Scanner(System.in); 
       int input = 0; 
       System.out.println("Enter a number:"); 
       input = scnr.nextInt();; 
     
       // Print numbers from [1..input] 
       for (int num = 1; num <= input; num++) 
          System.out.print(num + " "); 
       System.out.println(); 
 
       // Print EVEN numbers from [0..input-1] (TBA) 
     
       // Print ODD numbers from [input..1] (TBA) 
    } 
} 
    
Step 1: Copy this program into your Java program editor, and compile it. Hopefully you will not 
get any error messages. 
 
Step 2: Run your program type in "5" to see what it prints. Notice that all of the output is on one 
line because we used spaces between the numbers, and the new line is printed after the for loop. 
The variable "num" declared in the for loop is only valid inside the loop itself. If we tried to print 
it out after the loop we would get an error saying the variable is "out of scope". 
 
Step 3: Edit your program and add a second for loop to print out the EVEN numbers [0..input-1] 
on a single line. To do this, you need to modify the initialization, the logical expression, and the 
update variables code. You can use the same print statements as the first for loop. 
 
Step 4: Compile and test your program with a variety of input values. For example, if the user 
types in an 11, you should see: 
 
Enter a number: 
11 
1 2 3 4 5 6 7 8 9 10 11  
0 2 4 6 8 10  
 
Step 5: Edit your program again and add a third for loop to print the ODD numbers [input..1] on 
a single line. This problem is tricky because we are decreasing the numbers as we are printing, 
and we have to figure out the first value to print. If input is odd, we start by printing input. If 
input is even, we must start by printing input-1. 
 
Step 6: Compile and test your program with a variety of input values. For example, if the user 
types in an 8, you should see: 
 
Enter a number: 
8 
1 2 3 4 5 6 7 8  
0 2 4 6  
7 5 3 1 
 
Step 7: Once you think your program is working correctly, upload your final program and a copy 
of your program output into Blackboard for grading.