Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
  
  
Lab 2A: If Statements 
 
Introduction: 
 
Very often when you write code, you want to perform different actions for different decisions. 
You can use conditional statements in your code to do this. In Java we have three common 
conditional statements: 
 
• if statement - use this statement if you want to execute a block of code when a condition 
is true. The syntax of this command is shown below: 
 
if (condition) 
{ 
code to execute if condition is true  
} 
 
• if-else statement - use this statement if you want to execute one block of code when a 
condition is true and a different block of code if the condition is false. The syntax of this 
command is shown below: 
 
if (condition) 
{ 
   code to execute if condition is true  
} 
else 
{ 
   code to execute if condition is false  
} 
 
• nested if-else statement - use this statement if there are three or more alternatives and 
you only want to execute one block of code. The syntax of this command is shown 
below: 
 
if (condition1) 
{ 
   code to execute if condition1 is true  
} 
else if (condition2) 
{ 
   code to execute if condition2 is true  
} 
. 
. 
. 
else if (conditionN) 
{ 
   code to execute if conditionN is true  
}  
else 
{ 
   code to execute if none of the conditions are true  
} 
 
When the nested if-else statement is executed, the program evaluates the conditions one at a time 
from top to bottom, and when it finds a condition that is true, it executes the following block of 
code. After executing this code, the program jumps to the end of the nested if-else statement. If 
none of the conditions are true, the code following the final else will be executed. 
 
Instructions: 
 
Consider the following Java program. It reads a float from the user and then uses nested if-else 
statements to execute different blocks of code depending on which conditions are true. Notice 
that when the block of code following a condition is only one line long, you do not need to use 
the { } brackets. 
 
import java.util.Scanner; 
public class Main 
{ 
 public static void main(String[] args)  
 { 
     Scanner scnr = new Scanner (System.in); 
   
    // Read test score 
    System.out.println("Enter test score: "); 
    float Score = scnr.nextFloat(); 
 
    // Check test score 
    if (Score >= 100) 
        System.out.println("You grade is A+\n"); 
    else if (Score > 90) 
        System.out.println("Your grade is A\n"); 
    else  
        System.out.println("You are something else\n"); 
} 
} 
 
Step 1: Create a program called "Grade.java" and cut and paste the code above into your file. 
Compile and run the program to see what it does when you type in 95. See what happens if you 
run the program and type in 105 or 85. You should see all three output messages. 
 
Step 2: When you are testing a program it is a good idea to see what happens when you enter 
any of the constants in the program (100 and 90 in this case) and values that are +1 and -1 of 
these values. These are called the "boundary conditions". Does your program output the 
messages you expect? If not, you may need to change the ">" into ">=" or vice versa. 
 
Step 3: Now that the A case is working correctly, extend your program to add code to check if 
the score is in the B range, and print out the message "Your grade is B". Compile and test your 
program to make sure that the new boundary case works correctly. 
 
Step 4: Edit your program again and add code to check for input scores that are in the C range, 
the D range, and the F range using the grading scale for this class. Your program should print out 
corresponding "Your grade is" messages. Compile and test your program to make sure that the 
new boundary cases work correctly. 
 
Step 5: Once you think your program is working correctly, upload your final program and a 
copy of your program output into Blackboard for grading.