JAVA--基础

23 篇文章 0 订阅
**
 * 分支结构:根据某些具体条件来执行指定的代码
 * if :如果    else :否则
 * 语法:
 * if  (逻辑表达式)
 * {
 *     条件满足时执行的代码
 * }
 * else{
 *
 * }
 * 如果逻辑表达式的结果为真,就执行里面的代码,如果为假就 不执行
 */

public class Demo01分支结构 {

    public static void main(String[] args) {
        Scanner input  = new Scanner (System.in);
        //从键盘输入三条边,判断是否构成三角形,如果可以构成,如果是等边三角形,就输出等边三角形,
        // 如果是直角三角形,就输出直角三角形、
        System.out.println("请输入三条边:");
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        if((a+b>c)&&(a+c>b)&&(b+c>a)){
            System.out.println("可以构成三角形");
            if(a / b == 2|| a / c==2||b/a==2||b /c==2 ||c/a==2 ||c/b==2) {
                System.out.println("这个三角形是直角三角形");

            }
            else if (a ==b||a== c) {
                System.out.println("你输入的是等腰三角形");
            }
            else if (a ==b && b== c) {
                System.out.println("你输入的是等边三角形");
            }
        }
        else{
            System.out.println("你的输入不构成三角形,请重新输入");
        }
    }
}
 
 
 
import java.util.Random; import java.util.Scanner; public class Demo02switchcase分支结构 { public static void main(String[] args) { /**  * switch(常量值)  * {  * case 值1:  * 语句  * break;  * case 值2:  * 语句  * break;  * case 值3:  * 语句  * break;  * deaulft:  * 语句;  * }  * 如果switch里面的常量值跟case里面的值完全匹配时就开始执行  * 对应case下面的语句直接遇到break ,如果switch得常量在case中  * 值就直接进入到deaulft  */  int a =(int) (Math.random()*100); Random b = new Random(); int c =b.nextInt(100); Scanner input = new Scanner(System.in); int entry = input.nextInt(); switch (entry){ case 1: System.out.println(a + c); } } }
 
 
/**  * else if(){}:多用于区间的分支  * switch...case :常量分支  * switch(常量值){  * case 值1:  * 语句  * break;  * }  * 在jdk 1.7以前switc的常量值只能是整数( char byte short)  * 在jdk1.7及1.7之后的版本 Switc的常量值可以使字符串  *  * 练习:  * 从键盘中输入注册,注册的时候保存用户得用户名和密码,再输入登录,登录的时候  * 用户从键盘中输入登陆时需要用得到用户名和密码进行匹配,完全相同就提示登录成功  * 错误的时候就提示登录失败,如果输入的信息不是登录或者注册就提示用户输入有误  */   import java.util.Scanner; public class test3 { public static void main(String[] args) { /**  * 练习:  * 从键盘输入年份,计算该年 每个月最大天数多少天 switch .  * */  Scanner input= new Scanner(System.in); System.out.println("请输入年份"); int entry = input.nextInt(); System.out.println("请输入你要查询的月份 "); int month = input.nextInt(); switch (month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println(month+"月31天"); break; case 4: case 6: case 9: case 11: System.out.println(month+"月有30天"); break; case 2: { if ((entry % 4 == 0 && entry % 100 != 0) || entry % 400 == 0) { System.out.println("2月份29天"); } else { System.out.println("2月份28天"); } break; } default:{ System.out.println("输入错误"); } } //得出这个年份是不是闰年  String a = input.nextLine(); switch (a){ case"登录": System.out.println("登录成功"); break; } } }

 

 

 

 
 

练习题:

 
public class test2 { /**  * 练习: 随机从0-100之间生成两个随机数,从键盘中输入1表示  * 做加法运算 2+3 = 5 ;  * 输入2做减法运算,输入3乘法,输入4除法运算,其他得就提示输入错误  * 并且题出来之后用户从键盘输入答案并判断是否正确。  * @param args  */  public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("1.加法运算 2.减法运算 3.乘法运算 4.除法运算 "); int entry = input.nextInt(); int a = (int) (Math.random()*100); System.out.println("a="+a); int b = (int) (Math.random()*100); System.out.println("b="+b); if (entry==1){ int num=a+b; System.out.println("a+b="+num); System.out.println("请输入你的答案:"); int c = input.nextInt(); if(c==num){ System.out.println("恭喜,你猜对了"); } else { System.out.println("很抱歉,你猜错了"); } } else if(entry==2){ int num=a-b; System.out.println("a-b="+num); System.out.println("请输入你的答案:"); int c = input.nextInt(); if(c==num){ System.out.println("恭喜,你猜对了"); } else { System.out.println("很抱歉,你猜错了"); } } else if(entry==3){ int num=a*b; System.out.println("a*b="+num); System.out.println("请输入你的答案:"); int c = input.nextInt(); if(c==num){ System.out.println("恭喜,你猜对了"); } else { System.out.println("很抱歉,你猜错了"); } } else if(entry==2){ int num=a/b; System.out.println("a/b="+num); System.out.println("请输入你的答案:"); int c = input.nextInt(); if(c==num){ System.out.println("恭喜,你猜对了"); } else { System.out.println("很抱歉,你猜错了"); } } else { System.out.println("输入错误"); } } }

 

 

 

 
import java.util.Scanner; public class test4 { /**  * 练习:  * 从键盘中输入注册,注册的时候保存用户得用户名和密码,再输入登录,登录的时候  * 用户从键盘中输入登陆时需要用得到用户名和密码进行匹配,完全相同就提示登录成功  * 错误的时候就提示登录失败,如果输入的信息不是登录或者注册就提示用户输入有误  */  public static void main(String[] args) { Scanner input = new Scanner(System.in); String entry = input.nextLine(); String name ="0"; int password=0; switch (entry){ case "注册": System.out.println("请输入用户名:"); name = input.nextLine(); System.out.println("请输入密码:"); password = input.nextInt(); case "登录": System.out.println("请输入用户名:"); String Name = input.nextLine(); System.out.println("请输入用户名:"); int Password = input.nextInt(); if(name.equals(Name)&&password==Password){ System.out.println("登录成功"); } else { System.out.println("登录失败"); } default:{ System.out.println("你的输入有误请重新输入"); } } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值