Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
CS 1301 – Fall 2015 
Lab 6 
 
Working with Loops – Chapter 5 
 
Use JGrasp to complete the following programs selected for this lab. These programs use Scanner class for 
inputs. They illustrate the use of Java loops. Understand what each loop does, and keep these programs for 
future reference. 
 
//*****************************************************************  
// Average.java                           Author: Lewis/Loftus  
// Demonstrates the use of a while loop with a sentinel value.  
//*****************************************************************  
   import java.text.DecimalFormat;  
   import java.util.Scanner;  
   public class Average  
   {  
   //-----------------------------------------------------------------  
   // Computes the average of a set of values entered by the user.  
   // The running sum is printed as the numbers are entered.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
      {  
         int sum = 0, value, count = 0;  
         double average;  
         Scanner scan = new Scanner (System.in);  
         System.out.print ("Enter an integer (0 to quit): ");  
         value = scan.nextInt(); 
  
         while (value != 0) // enter sentinel value of 0 to terminate loop  
         {  
            count = count + 1;  
            sum = sum + value;  
            System.out.println ("The sum so far is " + sum);  
            System.out.print ("Enter an integer (0 to quit): ");  
            value = scan.nextInt();  
         }  
 
         System.out.println ();  
         if (count == 0)  
            System.out.println ("No values were entered.");  
         else  
         {  
            average = (double)sum / count;  
            DecimalFormat fmt = new DecimalFormat ("0.###");  
            System.out.println ("The average is " + fmt.format(average));  
         }  
      }  
   } 
 
 
//********************************************************************  
// WinPercentage.java                        Author: Lewis/Loftus  
// Demonstrates the use of a while loop for input validation.  
//********************************************************************  
   import java.text.NumberFormat;  
   import java.util.Scanner;  
   public class WinPercentage  
   {  
   //-----------------------------------------------------------------  
   // Computes the percentage of games won by a team.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
      {  
 
Page 1 of 4 
         final int NUM_GAMES = 12;  
         int won;  
         double ratio;  
         Scanner scan = new Scanner (System.in);  
         System.out.print ("Enter the number of games won (0 to "  
            + NUM_GAMES + "): ");  
         won = scan.nextInt(); 
  
         while (won < 0 || won > NUM_GAMES)  
         {  
            System.out.print ("Invalid input. Please reenter: ");  
            won = scan.nextInt();  
         } 
  
         ratio = (double)won / NUM_GAMES;  
         NumberFormat fmt = NumberFormat.getPercentInstance();  
         System.out.println ();  
         System.out.println ("Winning percentage: " + fmt.format(ratio));  
      }  
   }  
 
 
//********************************************************************  
// ReverseNumber.java                          Author: Lewis/Loftus  
// Demonstrates the use of a do loop.  
//********************************************************************  
   import java.util.Scanner;  
   public class ReverseNumber  
   {  
   //-----------------------------------------------------------------  
   // Reverses the digits of an integer mathematically.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
      {  
         int number, lastDigit, reverse = 0;  
         Scanner scan = new Scanner (System.in);  
         System.out.print ("Enter a positive integer: ");  
         number = scan.nextInt();  
          
         do  
         {  
            lastDigit = number % 10;  
            reverse = (reverse * 10) + lastDigit;  
            number = number / 10;  
         } while (number > 0);  
          
         System.out.println ("That number reversed is " + reverse);  
      }  
   }  
 
 
//********************************************************************  
// Multiples.java                              Author: Lewis/Loftus  
// Demonstrates the use of a for loop.  
//********************************************************************  
   import java.util.Scanner;  
   public class Multiples  
   {  
   //-----------------------------------------------------------------  
   // Prints multiples of a user-specified number up to a user-  
   // specified limit.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
      {  
         final int PER_LINE = 5;  
         int value, limit, mult, count = 0;  
         Scanner scan = new Scanner (System.in);  
         System.out.print ("Enter a positive value: ");  
         value = scan.nextInt();  
 
Page 2 of 4 
         System.out.print ("Enter an upper limit: ");  
         limit = scan.nextInt();  
         System.out.println ();  
         System.out.println ("The multiples of " + value + " between " +  
            value + " and " + limit + " (inclusive) are:"); 
  
         for (mult = value; mult <= limit; mult = mult + value)  
         {  
            System.out.print (mult + "\t");  
            // Print a specific number of values per line of output  
            count = count +1;  
            if (count % PER_LINE == 0)  
               System.out.println();  
         }  
      }  
   }  
 
 
//********************************************************************  
// Stars.java                                Author: Lewis/Loftus  
// Demonstrates the use of nested for loops.  
//********************************************************************  
   public class Stars  
   {  
   //-----------------------------------------------------------------  
   // Prints a triangle shape using asterisk (star) characters.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
      {  
         final int MAX_ROWS = 10;  
 
         for (int row = 1; row <= MAX_ROWS; row = row + 1)  
         {  
            for (int star = 1; star <= row; star = star + 1)  
               System.out.print ("*");  
          
            System.out.println();  
         }  
      }  
   } 
 
 
Now, use the above programs for guidance to complete the following practice programs 
 
 
Exercise 1:  Write a Java program (called Practice_6_1) that uses a while loop to determine and print out the 
sum of all values between 1 and 100. Document the code, properly label the outputs, and organize the outputs 
using escape characters and formatting objects when applicable. 
 
 
Exercise 2: Write a Java program (called Practice_6_2) that uses nested loops to print out the following 
shape. See chapter 5 slides for related example.  
 
**********  
*********  
********  
*******  
******  
*****  
****  
***  
**  
*  
 
 
 
Page 3 of 4 
 
Page 4 of 4 
Exercise 3:  Write a Java program (called Practice_6_3) that uses a while loop to determine and print out all 
even numbers between 50 and 100 on a single line, separated by commas. Then another while loop in the 
same program to print out all odd numbers between 50 and 100 on a new line, separated by commas. Use 
proper label for the outputs as shown in the example below. 
 
 Even numbers between 50 and 100: 50, 52, 54, 56, 58, 60, 62, 64, …  
 
 Odd numbers between 50 and 100:  51, 53, 55, 57, 59, 61, 63, 65, …  
 
 
Exercise 4: Write a Java program (called Practice_6_4) to read a string value from the user, the program 
prints out the entered string, and then prints out the string one character per line. Use proper labels for the 
outputs as shown in the example below. 
  
Entered String: Test input.  
 
Character #1: T  
Character #2: e  
Character #3: s  
Character #4: t  
Character #5:   
Character #6: i  
Character #7: n  
Character #8: p  
Character #9: u  
Character #10: t 
Character #11: .  
  
 
Submission: 
 
1.  Save all programs. 
2.  Check with your instructor for submission instructions.