103讲 顺序控制、单分支使双分支
内容纲要

103讲 顺序控制、单分支使双分支

file

file

file

单分支

  • if - else 语句

file

编写一个程序,可以输入人的年龄,如果该同志年龄大于18岁,则输出“你年龄大于18,要对自己的行为负责,送入监狱”

import java.util.Scanner;
public class IfCause{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("please enter ure age:");
        int age = scan.nextInt();
        if( age >= 18){
            System.out.println("you should sended to prison");
        }else
            System.out.println("nothing for that, but u do some mistake");
    }
}

file

  • 分支控制if-else

    单分支if

file

双分支 if -else

  • file

  • if-else双分支流程图

file


  • 练习题

  • file

  • 单分支和双分支练习题

file

  //题目1
  import java.util.Scanner;
  public class IfExercise01{
      public static void main(String[] args){
          Scanner myScanner = new Scanner(System.in);
          System.out.println("请分别输入2个值中间用回车隔开:");
          double num1 = myScanner.nextDouble();
          double num2 = myScanner.nextDouble();
          if(num1 > 10.0 && num2 < 20.0){
              double result = num1 + num2;
              System.out.println("结果总和=" + result);
          }else{
          System.out.println("什么也没有");
          }
          System.out.println("==========");
      }
  }
  //题目2:定义两个变量int,判断二者的和,是否能被3又能被5整除,打印提示信息。
  public class IfExercise02{
      public static void main(String[] args){
          int a = 60;
          int b = 30;
          int sum = a + b;
          if( sum%3 + sum%5 == 0 ){
              System.out.println("it could be dilivered by 3&5");
          }else
              System.out.println("it cloudn't do that");

      }
  }
  • 题目2分析如下

file

file

  • 题目3

    //判断一个年份是否是闰年,闰年的条件是符合下面二者之一:(1)年份能被4 整除,但不能被100 整除;(2)能被400 整除
    public class IfExercise03{
      public static void main(String[] args){
          int year = 3306;
          if( year%400 == 1){
              System.out.println(true);
          }else-if(year%100 == 0){
              System.out.println(false);
          }else
              System.out.print
          ///爷思路乱了。
      }
    }

file

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇