05-循环

05-循环

  • 5.1 引言

    • 循环可以让一个程序反复的执行语句。
    • Java提供了三中类型的循环语句:while语句、do-while循环和for循环。
  • 5.2 while循环

    • while循环在条件为真的情况下,重复的执行语句。

    • while循环的语法如下:

      while(循环继续语句){
      //循环体
      语句(组);
      }
      
    • 循环中包含的重复执行的语句部分称为循环体。循环体的每一次执行都被认为是一次循环的迭代。每个循环都含有循环继续条件,循环继续条件是一个布尔表达式,控制循环体的执行。在循环体执行前总是先计算循环条件以决定是否执行它。若条件是true,则执行循环体;若条件是flase,则终止整个循环,并且程序控制转移到while循环后的下一条语句。
      在这里插入图片描述

    • 循环继续条件应该总是放在圆括号内。只有当循环体只包含一条语句或不包含语句时,循环体的花括号才可以省略

    • 要保证循环继续条件最终可以变为false,以便程序能够结束。一个常见的错误是无限循环(也就是说,循环会永远执行下去)。如果你的程序运行了过长的时间而不结束,可能其中就有无限循环。如果你是从命令窗口运行程序的,按CTRL+C来结束运行。

    • 程序员经常会犯的错误就是使循环多执行一次或者少执行一次。这种情况通常称为差一错误。

    • 示例:提示用户为两个个位数相加的问题给出答案。现在你可以使用循环编写程序,让用户重复输入新的答案,直到答案正确为止。

      package chapter05;
      
      import java.util.Scanner;
      
      public class RepeatAddtionQuiz {
          public static void main(String[] args){
              int number1 = (int)(Math.random() * 10);
              int number2 = (int)(Math.random() *10 );
              Scanner input = new Scanner(System.in);
              System.out.print("What is " + number1 + " + " + number2 + "? ");
              int answer = input.nextInt();
              while (number1 + number2 != answer){
                  System.out.print("Wrong answer.Try again.What is " + number1 + " + " + number2 + "? ");
                  answer = input.nextInt();
              }
              System.out.println("You got it! ");
          }
      }
      
      
  • 5.3 示例学习:猜数字
    在这里插入图片描述

    package chapter05;
    
    import java.util.Scanner;
    
    public class GuessNumber {
        public static void main(String[] args){
            int number = (int)(Math.random() * 101);
            Scanner input = new Scanner(System.in);
            System.out.println("Guess a magic number between 0 and 100");
            int guess = -1;
            while (guess != number){
                System.out.print("\nEnter your guess: ");
                guess = input.nextInt();
    
                if (guess == number)
                    System.out.println("Yes, the number is " + number);
                else if (guess > number)
                    System.out.println("Your guess is too high");
                else
                    System.out.println("Your guess is too low");
            }
        }
    }
    
    
  • 5.4 循环设计策略

    • 编写循环时应该考虑三个步骤:

      • 1、确定需要重复的语句
      • 2、将这些语句放在一个循环中
      • 3、为循环继续条件编码,并为控制循环添加合适的语句
    • 给出以下一个程序:可以产生5个问题,在学生回答完所有5个问题后,报告回答正确的题数。这个程序还显示该测试所花费的时间,并列出所有的题目。

      package chapter05;
      
      import java.util.Scanner;
      
      public class SubtractionQuizLoop {
          public static void main(String[] args){
              final int NUMBER_OF_QUESTION = 5;
              int correctCount = 0;
              int count = 0;
              long stratTime = System.currentTimeMillis();
              String output = " ";
              Scanner input = new Scanner(System.in);
      
              while (count < NUMBER_OF_QUESTION){
                  int number1 = (int)(Math.random() * 10);
                  int number2 = (int)(Math.random() * 10);
                  if (number1 < number2){
                      int temp = number1;
                      number1 = number2;
                      number2 = temp;
                  }
                  System.out.print("What is " + number1 + " - " + number2 + "? ");
                  int answer = input.nextInt();
      
                  if (number1 - number2 == answer){
                      System.out.println("You are correct!");
                      correctCount++;
                  }
                  else
                      System.out.println("Your answer is wrong.\n" + number1 + " - " + number2 + "should be " + (number1 + number2));
                  count++;
                  output += "\n" + number1 + "-" + number2 + "=" + answer + ((number1 - number2 == answer) ? "correct":"wrong");
              }
              long endTime = System.currentTimeMillis();
              long testTime = endTime - stratTime;
              System.out.println("Correct count is " + correctCount + "\nTest time is " + testTime / 100 + " seconds\n" + output);
          }
      }
      
      
  • 5.5 使用用户确认或者标记值控制循环

    • 一种控制循环的常用技术是在读取和处理一组值时指定一个特殊值。这个特殊的输入值也称为标记值,用以表明循环的结束。如果一个循环使用标记值来控制它的执行,它就称为标记位控制的循环。

    • 给出一个程序:用来读取和计算个数不确定的整数之和,并以输入0表示输入结束。

      package chapter05;
      
      import java.util.Scanner;
      
      public class SentinelValue {
          public static void main(String[] args){
              Scanner input = new Scanner(System.in);
              System.out.print("Enter an integer (the input ends if it is 0): ");
              int data = input.nextInt();
              int sum = 0;
              while (data != 0){
                  sum += data;
                  System.out.print("Enter an integer (the input ends if it is 0): ");
                  data = input.nextInt();
              }
              System.out.println("The sum is " + sum);
          }
      }
      
      
    • 不要比较浮点数值是否相等来进行循环控制。因为浮点值都是近似值,使用他们可能导致不精确的循环次数和不精确的结果。

    • 如果要输入大量的数据值,那么从键盘上输入是非常烦琐的。可以将这些数据用空格隔开,保存在一个名为input.txt的文本文件中,然后使用下面的命令运行这个程序:

      java SentineValue < input.txt
      
    • 这个命令称为输入重定向。程序从文件input.txt中读取输入,而不是让用户在运行时从键盘输入数据。

    • 类似的,还有输出重定向,输出重定向将输出发送给文件,而不是将他们显示在控制台上。输出重定向的命令为:

      java ClassName > output.txt
      
    • 可以在同一个命令中同时使用输入重定向和输出重定向。

      java SentineValue < input.txt > output.txt
      
  • 5.6 do-while循环

    • do-whil循环和while循环基本一样,不同的是他先执行循环体一次,然后判断循环继续条件。

    • do-while循环是while循环的变体。他的语法是:

      do{
      	//循环体;
      	语句(组);
      }while(循环继续条件);
      

    在这里插入图片描述

    • 首先执行循环体,然后计算循环继续条件。如果计算结果为true,则重复执行循环体;如果是false,则终止do-while循环。
    • while循环与do-while循环的不同之处在于:计算循环继续条件和执行循环体的先后顺序不同。在使用do-while循环的情况下,循环体至少执行一次。可以使用while循环或者do-while循环来编写循环。某些情况下选择其中一个会比另一种更加方便。
      package chapter05;
      
      import java.util.Scanner;
      
      public class TestDoWhile {
          public static void main(String[] args){
              int data;
              int sum = 0;
              Scanner input = new Scanner(System.in);
              do {
                  System.out.print("Enter an integer (the input ends if it is 0): ");
                  data = input.nextInt();
                  sum += data;
              }while (data != 0);
              System.out.println("The sum is : " + sum);
          } 
      }
      
      
    • 如果循环中的语句至少要执行一次,建议使用do-while循环。如果使用while循环,那么这些语句必须在循环前和循环内都出现。
  • 5.7 for循环

    • for语句循环的语法如下:

      for(初始操作;循环继续条件;每次迭代后的操作){
      //循环体;
      语句(组);
      }
      

      在这里插入图片描述

    • for循环语句从关键字for开始,然后是用一对圆括号住的循环控制结构体。这个结构体包括初始操作、循环继续条件和每次迭代后的操作。控制结构体后紧跟着花括号括起来的循环体。初始操作、循环继续条件和每次迭代后的操作都要用分号隔开。

    • 一般情况下,for循环使用一个变量来控制循环体的执行次数,以及什么时候循环终止。这个变量称为控制变量。初始操作是指初始控制变量,每次迭代后的操作通常会对控制变量做自增或自减,而循环继续条件检验控制变量是否达到终止值。

    • 控制变量必须在循环控制结构体内或循环前说明。如果循环控制变量只在循环内使用而不再其他地方使用,那么在for循环的初始操作中声明它是一个良好的编程习惯。如果在循环控制结构体内声明变量,那么在循环外不能引用他。

    • for循环中的初始操作可以是0个或者多个以逗号隔开的变量声明语句或赋值表达式。

    • 如果省略for循环中的循环继续条件,则隐含的认为循环继续条件为true。

  • 5.8 采用哪种循环

    • for循环更加简洁,并且相比另外两种循环而言更容易避免错误。
    • while循环和for循环都称为前测循环,因为继续条件是在循环体执行之前检测的,do-while循环称之为后测循环,因为循环条件是在循环体执行之后检测的。
    • 如果已经提前知道重复次数,那就采用for循环。无法确定重复次数,就采用while循环,在检测继续条件前需要执行循环体,就用do-while循环替代while循环。
  • 5.9 嵌套循环

    • for循环打印一个乘法表的程序:

      package chapter05;
      
      public class MultiplicationTable {
          public static void main(String[] args){
              System.out.println("            Multiplication Table");
              System.out.print("    ");
              for (int j = 1;j <= 9;j++)
                  System.out.print("   " + j);
              System.out.println("\n---------------------------------------------");
              for (int i = 1;i <= 9;i++){
                  System.out.print(i + " | ");
                  for (int j = 1;j <= 9;j++){
                      System.out.printf("%4d",i * j);
                  }
                  System.out.println();
              }
          }
      }
      
      
  • 5.10 最小化数值错误

    package chapter05;
    
    public class TestSum {
        public static void main(String[] args){
            float sum = 0;
            for (float i = 0.01f;i <= 1.0f;i = i + 0.01f)
                sum += i;
            System.out.println("The sum is " + sum);
        }
    }
    
    
  • 5.11 示例学习

    • 5.11.1 求最大公约数
      • 提示用户输入两个正整数,然后找到他们的最大公约数。

        package chapter05;
        
        import java.util.Scanner;
        
        public class GreatestCommonDivisor {
            public static void main(String[] args){
                Scanner input = new Scanner(System.in);
                System.out.print("Enter first integer: ");
                int n1 = input.nextInt();
                System.out.print("Enter second integer: ");
                int n2 = input.nextInt();
                int gcd = 1;
                int k = 2;
                while (k <= n1 && n2 % k == 0){
                    if (n1 % k == 0 && n2 % k == 0)
                        gcd = k;
                    k++;
                }
                System.out.println("The greatest common divisor for " + n1 + " and " + n2 + " is " + gcd);
            }
        }
        
        
    • 5.11.2 预测未来学费
      • 假设某个大学今年的学费是10000美元,而且以每年7%的速度增加。多少年之后学费会翻倍?

        package chapter05;
        
        public class FutureTuition {
            public static void main(String[] args){
                double tuition = 10000;
                int year = 0;
                while (tuition < 20000){
                    tuition = tuition * 1.07;
                    year++;
                }
                System.out.println("Tuition will be doubled in " + year + " years");
                System.out.printf("Tuition will be $%.2f in %1d years",tuition,year);
            }
        }
        
        
    • 5.11.3 将十进制数转换为十六进制数
      • 意识用户输入一个十进制数,然后将它转换为一个字符串形式的十六进制数。
        package chapter05;
        
        import java.util.Scanner;
        
        public class Dec2Hex {
            public static void main(String[] args){
                Scanner input = new Scanner(System.in);
                System.out.print("Enter a decimal number: ");
                int decimal = input.nextInt();
                String hex = "";
                while (decimal != 0){
                    int hexValue =  decimal % 16;
                    char hexDigit = (0 <= hexValue && hexValue <= 9) ? (char)(hexValue + '0') : (char)(hexValue - 10 + 'A');
                    hex = hexDigit + hex;
                    decimal = decimal / 16;
                }
                System.out.println("The hex number is " + hex);
            }
        }
        
        
  • 5.12 关键字break和continue

    • 可以在一个循环中使用break立即终止循环。
      package chapter05;
      
      public class TestBreak {
          public static void main(String[] args){
              int sum = 0;
              int number = 0;
              while (number < 20){
                  number++;
                  sum += number;
                  if (sum >= 100)
                      break;
              }
              System.out.println("The number is " + number);
              System.out.println("The sum is " + sum);
          }
      }
      
      
    • 可以在循环中使用关键字continue。当程序遇到continue时,他会结束当前的迭代。程序控制转向该循环体的末尾。换句话说,continue只是跳出了一次迭代,而关键字break是跳出了整个循环。
      package chapter05;
      
      public class TestContinue {
          public static void main(String[] args){
              int sum = 0;
              int number = 0;
              while (number < 20){
                  number++;
                  if (number == 10 || number == 11)
                      continue;
                  sum += number;
              }
              System.out.println("The sum is " + sum);
          }
      }
      
      
    • continue语句总是在一个循环内。在while和do-while循环中,continue语句之后会马上计算循环继续条件;而在for循环中,continue语句之后会立即先执行每次迭代之后的动作,在计算循环循环条件。
    • 很多程序设计语言都有goto语句。goto语句可以随意的将控制转移到程序中的任意一条语句中,然后执行它。这使程序很容易出错。Java中的break语句和continue语句是不同于goto语句的。他们只能运行在循环中或者switch语句中。break语句跳出整个循环,而continue语句跳出循环的当前迭代。
  • 5.13 示例学习:判断回文

package chapter05;

import java.util.Scanner;

public class Palindrome {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = input.nextLine();
        int low = 0;
        int high = s.length() - 1;
        boolean isPalindrome = true;
        while (low < high){
            if (s.charAt(low) != s.charAt(high)){
                isPalindrome = false;
                break;
            }
            low++;
            high--;
        }
        if (isPalindrome)
            System.out.println(s + " is a palindrome");
        else 
            System.out.println(s + " is not a palindrome");
    }
}

  • 5.14 示例学习:显示素数
package chapter05;

public class PrimeNumber {
    public static void main(String[] args){
        final int NUMBER_OF_PRIME = 50;
        final int NUMBER_OF_PRIME_PER_LINE = 10;
        int count = 0;
        int number = 2;
        System.out.println("The first 50 prime numbers are \n");
        while (count < NUMBER_OF_PRIME){
            boolean isPrime = true;
            for (int divisor =2; divisor <= number; divisor++){
                if (number % divisor == 0){
                    isPrime = false;
                    break;
                }
            }
            if (isPrime){
                count++;
                if (count % NUMBER_OF_PRIME_PER_LINE == 0){
                    System.out.println(number);
                }
                else 
                    System.out.println(number + " ");
            }
            number++;
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值