3.8控制流程

2019-03-11  本文已影响0人  十六只猴子王

javahi用条件语句和循环结构来控制流程

块作用域

块(即复合语句)指由一堆大括号括起来的若干条简单java语句,不能嵌套两个块中声明同名的变量

条件语句

if (condition) statement
在java中,更一般的田间语句格式是
if (condition) statement1 else statement2

if(yourSales>=target)
{
  performance = "Satisfactory";
  bonus = 100 + 0.01 * (yourSales - target);
}
else
{
  performance ="Unstaisfactory";
  bonus  = 0;
}

循环

当条件为true时,while循环执行一条语句格式为
while(condition) statement

import java. util.*;
public class Retirement
{
   public static void main(String[] args)
   {
      // 读取输入
      Scanner in = new Scanner(System.in);

      System.out.print("How much money do you need to retire? ");
      double goal = in.nextDouble();

      System.out.print("How much money will you contribute every year? ");
      double payment = in.nextDouble();

      System.out.print("Interest rate in %: ");
      double interestRate = in.nextDouble();

      double balance = 0;
      int years = 0;

      // 未达到目标时更新帐户余额
      while (balance < goal)
      {
         // add this year's payment and interest
         balance += payment;
         double interest = balance * interestRate / 100;
         balance += interest;
         years++;
        // 增加了一个计数器,并在循环中更新当前的累积数量
      }

      System.out.println("You can retire in " + years + " years.");
   }
}

循环体的代码有可能不被执行。如果希望至少执行一次则使用do/while循环语句,格式为
do statement while (condition);

import java.util.*;
public class Retirement2
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);

      System.out.print("How much money will you contribute every year? ");
      double payment = in.nextDouble();

      System.out.print("Interest rate in %: ");
      double interestRate = in.nextDouble();

      double balance = 0;
      int year = 0;

      String input;

      // 用户尚未准备注销时更新帐户余额
      do
      {
         // add this year's payment and interest
         balance += payment;
         double interest = balance * interestRate / 100;
         balance += interest;

         year++;

         // 打印当前余额
         System.out.printf("After year %d, your balance is %,.2f%n", year, balance);
         //格式化输出
         // 询问是否准备好注销并获得输入
         System.out.print("Ready to retire? (Y/N) ");
         input = in.next();
      }
      while (input.equals("N"));
   }
}

确定循环

for循环语句时支持迭代的一种通用结构,利用每次迭代之后跟新的计数器或类似的变量来控制迭代的次数。
for语句的第一部分通常用于对计数器的初始化;第二部分给出每次新一轮循环执前检测的循环条件;第三部份指示如何更新计数器

计算抽奖中奖的改路

import java.util.*;
public class LotteryOdds
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);

      System.out.print("How many numbers do you need to draw? ");
      int k = in.nextInt();

      System.out.print("What is the highest number you can draw? ");
      int n = in.nextInt();

    /*
   *计算二项式系数n*(n-1)*(n-2)*…(n-k+1)/(1*2*3*…*k)
  */

      int lotteryOdds = 1;
      for (int i = 1; i <= k; i++)
         lotteryOdds = lotteryOdds * (n - i + 1) / i;

      System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");
   }
}

foreach

for(元素类型t 元素变量x : 遍历对象obj){
引用了x的java语句;
}

多重选择 :switch语句

switch 语句将从与选项匹配的case便签处开始执行直到遇到break语句,或者执行到switch语句的结束处为止,如果没有匹配的case便签,遇到default子句,就执行这个子句
case便签可以是:

中断控制流程语句

*break语句,标签必须放在希望跳出的最外层循环之前,并且必须紧跟一个冒号

上一篇下一篇

猜你喜欢

热点阅读