现在的位置: 首页 > 综合 > 正文

The first step of Java[3]

2013年10月03日 ⁄ 综合 ⁄ 共 1749字 ⁄ 字号 评论关闭

/*
   Write a Java program called AverageNumbers2.java that calculates the average of numbers 1 to 50 using the for loop. 
   Do it again using the while loop.
*/

public class AverageNumbers2
{
 public static void main(String[] args)
 {
  double total = 0;
  for (int i = 1; i < 51; i++)
  {
   total += i;
  }
  System.out.println("The average of numbers 1 to 50 = " + total / 50);
 }
 
}

/*
   Write a Java program called BreakLoop.
   java that uses a for loop with the variable "count" and count 1 to 10.
   Display "count=<count>" each time you loop through. 
   Break out of the loop at 5. 
   At the end of the program display "Broke out of the loop at count = 5".
*/

public class BreakLoop
{
 public static void main(String[] args)
 {
  for (int i = 1; i < 11; i++)
  {
   if (5 == i)
   {
    break;
   }
   System.out.println("Count = " + i);
  }
  System.out.println("Broke out of the loop at count = 5.");
 }

}

/*
   Write a Java program called ContinueLoop.java that uses a for loop with the variable "count" and count 1 to 10.
   Display "count=<count>" each time you loop through. 
   Skip the display statement using the continue statement if count = 5. 
   At the end of the program display "Used continue to skip printing 5".
*/

public class ContinueLoop
{
 public static void main(String[] args)
 {
  for (int i = 1; i < 11; i++)
  {
   if (5 == i)
   {
    continue;
   }
   System.out.println("Count = " + i);
  }
  System.out.println("Used continue to skip printing 5.");
 }

}

/*
   Write a Java program called InputParms.
   java that accepts 3 arguments in the main method using an array. 
   Iterate through the array looking for your name using a for loop. 
   Display the message "The name <your name> was found" if your name is found.
*/
public class InputParms
{
 private static String _myName = "DL88250";
 
 public static void main(String[] args)
 {
  for (int i = 0; i < args.length; i++)
  {
   if (0 == _myName.compareTo(args[i]))
   {
    System.out.println("The name <"+ _myName + "> was found!");
    return;
   } 
  }
  System.out.println("The name <"+ _myName + "> was not found!");
 }
}
 

【上篇】
【下篇】

抱歉!评论已关闭.