• 分支语句 (if) 练习 Java代码



     1 package xuexi;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Lianxi {
     6     
     7     public static void main(String[] args){
     8 
     9     System.out.println("请输入1数字并回车");
    10         Scanner in = new Scanner(System.in);
    11     int i1=in.nextInt();
    12     System.out.println("请输入1组数字并回车");
    13     int i2=in.nextInt();
    14     int i3=i1+i2;
    15     System.out.println("2组数字之和="+i3);
    16     
    17             
    18     
    19     }
    20     
    21 }
    输入2个数字并相加求和
     1 package xuexi;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Fenzhiyuju {
     6 
     7     public static void main(String[] args) {
     8 
     9         Scanner in = new Scanner(System.in);
    10         int a = 1; // 1.短袖 2,长袖
    11         System.out.println("请选择类型(1,短袖 2,长袖)");
    12         a = in.nextInt();
    13 
    14         if (a == 1) {
    15             System.out.println("我要买短袖");
    16             System.out.println("请选择颜色:(1,表示白色,2表示黑色)");
    17             int b = in.nextInt();
    18             if (b == 1) {
    19                 System.out.println("我选择了白色");
    20             } else {
    21                 System.out.println("我选择了黑色");
    22             }
    23         }
    24 
    25         else {
    26             System.out.println("我选择了长袖");
    27             System.out.println("请选择颜色(1,表示黄色,2,表示绿色)");
    28             int c = in.nextInt();
    29             if (c == 1) {
    30                 System.out.println("我选择了黄色");
    31             } else {
    32                 System.out.println("我选择了绿色");
    33             }
    34 
    35         }
    36         System.out.println("这是你要的衣服请收好");
    37 
    38     }
    39 }
    买衣服进行选择

    import java.util.Scanner;   // 引用

    Scanner in = new Scanner(System.in);

    a = in.nextInt();

  • 相关阅读:
    DOS命令行
    很好的GDAL初学者资料
    VC++工程文件
    C#操作word文档
    vue 如何不复用组件的 DOM All In One
    2021 最新 XBOX 打丧尸游戏 All In One
    gti gc All In One
    custom height drawer All In One
    Redux & React Hooks All In One
    StackOverflow sites All In One
  • 原文地址:https://www.cnblogs.com/zjy954/p/5024414.html
Copyright © 2020-2023  润新知