Java 2 实用教程学习

本文介绍了Java中的三元运算符用于简洁的条件判断,以及If语句的三种格式,包括简单的If、If-Else和嵌套If。同时,讲解了Switch语句的使用,包括case穿透现象。接着,展示了for和while循环的示例,如计算偶数之和、寻找水仙花数等。最后,提到了跳转控制语句break和continue在循环中的作用,并演示了如何生成和使用随机数。
摘要由CSDN通过智能技术生成

2.7三元运算符

😋三元运算符语法格式:
关系表达式?表达式1:表达式2;
TRUE FALSE
public class sanyuan{
    public static void main(String[] args){
        int a=10;
        int b=20;
        int c=a>b?a:b;
        System.out.println("c:"+c);
    }
}

2.8 三元运算符应用

public class sanyuan{
    public static void main(String[] args){
        int height1=150;
        int height2=210;
        int height3=165;
        int tempHeight =height1>height2?height1:height2;
        int maxHeight =tempHeight>height3?tempHeight:height3;
        System.out.println("maxHeight:"+maxHeight);
    }
    
}

3.1分支结构

3.1.1 If语句格式1

public class If{
    public static void main (String args[]){
     System.out.println("start");
     //if age>18,you can go
     int age=17;
     if(age>=18){
         System.out.println(" go ");
     }
     System.out.println(" end ");
    }
}
流程图如下

3.1.2 If语句格式2

public class If{
    public static void main (String args[]){
     int num=10;
     if(num%2==0)
     {
         System.out.println("is even number");
     }
     else
     {
         System.out.println("is odd number");
     }
     
    }
}

3.1.3 If语句格式3

import java.util.Scanner;
public class If{
    public static void main (String args[]){
        Scanner sc= new Scanner(System.in);
        System.out.println("Please enter Your grades");
        int score =sc.nextInt();
        if(score>=0&&score<=100)
        {
            if(score>=95&&score<=100)
            {
                System.out.println("Get a bike");
            }
            else if(score>=90&&score<=94)
            {
                System.out.println("Go to the amusement park");
            }
            else if(score>=80&&score<=89)
            {
                System.out.println("Get a transformer");
            }
            else
            {
                System.out.println("make an efffort");
            }
            
        }
        else 
        {
            System.out. println("error");
        }
     
    }
}

3.1.4 Switch语句

break:结束Switch语句
import java.util.Scanner;
public class Switch{
    public static void main (String args[]){
        Scanner sc= new Scanner(System.in);
        System.out.println("Please enter :");
        int week =sc.nextInt();
        switch(week){
            case 1:
            System.out.println("run");
            break;
            case 2:
            System.out.println("swim");
            break;
           case 3:
            System.out.println("walk");
            break;
           case 4:
            System.out.println("bicycle");
            break;
           case 5:
            System.out.println("boxing");
            break;
           case 6:
            System.out.println("climb");
            break;
           case 7:
           System.out.println("eat");
           break;
           default:
            System.out.println("error");
            break;           
        }
    }

}
switch语句case穿透
import java.util.Scanner;
//public class Switch{
public class Switch{
    public static void main (String args[]){
        Scanner sc= new Scanner(System.in);
        System.out.println("Please enter :");
        int week =sc.nextInt();
        switch(week){
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("working day");
                break;
            case 6:
            case 7:
                System.out.println("day off");
                break;
        }   
    }

}

3.2循环结构

3.2.1for循环

 public class For{
     public static void main(String[]args){
         for(int i=0;i<=5;i++){
             System.out.println(i);
         }
         for(int i=5;i>=1;i--){
             System.out.println(i);
         }
     }
 }

求1~100之间的偶数之和

 public class For{
     public static void main(String[]args){
         /*for(int i=0;i<=5;i++){
             System.out.println(i);
         }
         for(int i=5;i>=1;i--){
             System.out.println(i);
         }*/
         int sum=0;
         for(int i=1;i<=100;i++){
             if(i%2==0){
                 sum+=i;
             }
         }
        System.out.println("1~100Even sum"+sum);
     }
 }
求水仙花数
public class For{
     public static void main(String[]args){
         /*for(int i=0;i<=5;i++){
             System.out.println(i);
         }
         for(int i=5;i>=1;i--){
             System.out.println(i);
         }
         int sum=0;
         for(int i=1;i<=100;i++){
             if(i%2==0){
                 sum+=i;
             }
         }
        System.out.println("1~100Even sum"+sum);
     }*/
     int count=0;
     for(int i=100;i<=999;i++){
         int ge=i%10;
         int shi=i/10%10;
         int bai=i/10/10%10;
         if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i){
             System.out.print(i+"  ");
             count++;
             if(count%2==0){
                 System.out.println();
             }
         }
     }
 }
 }

3.2.2while循环

public class While {
    public static void main (String [] args ){
        for(int i=1;i<=5;i++)
        {
            System.out.println("HelloWorld");
        }
        System.out.println("-------------");
        int j=1;
        while(j<=5)
        {
            System.out.println("HellloWorld");
            j++;
        }
    }
public class While{
    public static void main(String []args ){
        int count =0;
        double paper=0.1;
        int zf=8844430;
        while(paper<=zf){
            paper*=2;
            count++;
            System.out.println(paper);
        }
    System.out.println(count);
    }
}

3.2.3跳转控制语句

break:跳出循环,结束循环
continue:跳过本次循环,继续下次循环
import java.util.Scanner;
public class Continue{
    public static void main (String args[]){
       lo:while(true){
       Scanner sc= new Scanner(System.in);
        System.out.println("Please enter :");
        int week =sc.nextInt();
        switch(week){
            case 0:
            System.out.println("thank");
            break lo;
            case 1:
            System.out.println("run");
            break;
            case 2:
            System.out.println("swim");
            break;
           case 3:
            System.out.println("walk");
            break;
           case 4:
            System.out.println("bicycle");
            break;
           case 5:
            System.out.println("boxing");
            break;
           case 6:
            System.out.println("climb");
            break;
           case 7:
           System.out.println("eat");
           break;
           default:
            System.out.println("error");
            break;           
        }
    }
 
}
}

3.3Random

  1. 导包

import java.util.Random;
  1. 创建对象

Random  r=new Random ();
  1. 获取随机数

int number=r.nextInt(10);
import java.util.Random;
public class Rando{
    public static void main(String []args ){
        Random r = new Random(0);
        for (int i=1; i<=10; i++){
            int num= r.nextInt(10)+1;
            System .out.println(num);
        }
    }
}
import java.util.Random;
import java.util.Scanner;
public class Rando{
    
    public static void main(String []args ){
    Random r=new Random();
    Scanner sc =new Scanner(System.in);
    int randomnum=r.nextInt(100)+1;
    System.out.println(randomnum);
    while(true){
        System.out.println("please enter");
        int num=sc.nextInt();
        if(num>randomnum){
            System.out.println("big");
        }
        
        else if(num<randomnum){
            System.out .println("small");
        }
        else
        {
            System.out .println("right");
            break;
        }
    }
}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java2实用教程 rar 第1章Java入门 1 1Java的诞生 1 2Java的特点 1 3安装Sun公司的SDK 1 4一个Java程序的开发过程 1 5一个简单的Java应用程序的开发过程 1 6一个简单的Java小应用程序 1 7什么是JSP 习题 第2章标识符 关键字和数据类型 2 1标识符和关键字 2 2Java的基本数据类型 2 3Java的数组 习题 第3章运算符 表达式和语句 3 1运算符与表达式 3 2语句概述 3 3控制语句 3 4 循环语句 3 5break和continue语句 习题 第4章类 对象和接口 4 1编程语言的几个发展阶段 4 1 1机器语言 4 1 2过程语言 4 1 3面向对象编程 4 2类 4 2 1类声明 4 2 2类体 4 2 3成员变量和局部变量 4 2 4方法 4 2 5方法重载 4 2 6构造方法 4 2 7类方法和实例方法 4 2 8值得注意的问题 4 3对象 4 3 1创建对象 4 3 2使用对象 4 3 3于象的引用和实体 4 3 4参数传值 4 4static关键字 4 4 1实例变量和类变量的区别 4 4 2实例方法和类方法的区别 4 5this关键字 4 6包 4 6 1包语句 4 6 2import语句 4 6 3将类打包 4 7访问权限 4 7 1私有变量和私有方法 4 7 2共有变量和共有方法 4 7 3友好变量和友好方法 4 7 4受保护的成员变量和方法 4 7 5public类与友好类 4 8类的继承 4 8 1子类 4 8 2子类的继承性 4 8 3成员变量的隐藏和方法的重写 4 8 4final关键字 4 9对象的上转型对象 4 10多态性 4 11abstract类和abstract方法 4 12super关键字 4 13接口 4 13 1接口的声明与使用 4 13 2理解接口 4 13 3接口回调 4 13 4接口做参数 4 14内部类 4 15匿名类 4 15 1和类有关的匿名类 4 15 2和接口有关的匿名类 4 16异常类 4 16 1try catch语句 4 16 2自定义异常类 4 17Class类 4 17 1获取类的有关信息 4 17 2使用Class实例化一个对象 4 18基本类型的类包装 4 18 1Double类和Float类 4 18 2Byte Integer Short 工 ong类 4 18 3Character类 4 19反编译和文档生成器 4 20JAR文件 4 20 1将应用程序压缩为JAR文件 4 20 2将类压缩成JAR文件 4 20 3更新 查看JAR文件 习题 第5章字符串 5 1字符串 5 2字符串的常用方法 5 3字符串与基本数据的相互转化 5 4对象的字符串表示 5 5StringTokenizer类 5 6字符串与字符 字节数组 5 7StringBuffer类 5 8正则表达式 习题 第6章时间 日期和数字 6 1Date类 6 2Calendar类 6 3Math类 6 4BigInteger类 习题 第7章AWT组件及事件处理 7 1Java窗口 7 1 1 Frame常用方法 7 1 2菜单条 菜单 菜单项 7 1 3窗口与屏幕 7 2文本框 7 2 1TextField类的主要方法 7 2 2文本框上的ActionEvent事件 7 3内部类实例做监视器 7 4按钮与标签 7 4 1标签组件 7 4 2按钮组件 7 5菜单项 7 6文本区 7 6 1TextArea类主要方法 7 6 2文本区上的TextEvent事件 7 7面板 7 7 1Panel类 7 7 2ScrollPane类 7 8布局 7 8 1FlowLayout布局 7 8 2BorderLayout布局 7 8 3CardLayout布局 7 8 4GridLayout布局 7 8 5BoxLayout布局 7 8 6null布局 7 9画布 7 10选择型组件 7 10 1选择框 7 10 2下拉列表 7 10 3滚动列表 7 11Component类的常用方法 7 12窗口事件 7 13鼠标事件 7 14焦点事件 7 15键盘事件 7 16使用剪贴板 7 17打印 7 18综合实例 习题 第8章建立对话框 8 1Dialog类 8 2文件对话框 8 3消息对话框 8 4确认对话框 8 5颜色对话框 习题 第9章Java多线程机制 9 1Java中的线程 9 2Thread类的子类创建线程 9 3使用Runnable接口 9 4线程的常用方法 9 5GUI线程 9 6线程同步 9 7在同步方法中使用wait notif 和nodf3 All 方法 9 8挂起 恢复和终止线程 9 9计时器线程Timer 9 10线程联合 9 11守护线程 习题 第10章输入输出流 10 1File类 10 2FileInputStream类 10 3FileOutputStream类 10 4FileReader类和FileWriter类 10 5使用文件对话框打开和保存文件 10 6RandornAccessFile类 10 7数据流 10 8数组流 10 9对象流 10 10序列化与对象克隆 10 11文件锁FileLock 10 12Process类中的流 10 13带进度条的输入流 习题 第11章Java网络的基本知识 11 1使用URL 11 2读取URL中的资源 11 3显示URL资源中的HTML文件 11 4处理超链接 11 5InetAdress类 11 6套接字 11 7网络中的数据压缩与传输 11 8UDP数据报 11 9广播数据报 习题 第12章JavaApplet基础 12 1JavaApplet的运行原理 12 2网页向JavaApplet传值 12 3JavaApplet扣使用URL 12 4JavaApplet中建立新线程 12 5JavaApplet中使用套接字 习题 第13章常见数据结构的Java实现 13 1链表 13 2栈 13 3树集 13 4树映射 13 5散列集 13 6散列表 13 7向量 习题 第14章图形与图像 14 1绘制文本 14 2绘制基本图形 14 3建立字体 14 4清除 14 5Java2D 14 6图形的布尔运算 14 7绘制钟表 14 8绘制图像 14 9制作JPG图像 14 10XOR绘图模式 14 11打印图形 图像 习题 第15章Java数据库连接 JDBC 15 1创建数据源 15 2JDBC ODBC桥接器 l5 3顺序查询 15 4可滚动结果集 15 5排序查询 15 6模糊查询 15 7随机查询 15 8更新 添加 删除记录 l5 9预处理语句 15 10数据库访问中的套接字技术 习题 第16章Java与多媒体 16 1在小程序中播放音频 16 2在另一个线程中创建音频对象 16 3在应用程序中播放音频 16 4Java媒体框架 JMF 习题 第17章JavaSwing基础 17 1几个重要的类 17 2中间容器 17 3各种组件 习题">Java2实用教程 rar 第1章Java入门 1 1Java的诞生 1 2Java的特点 1 3安装Sun公司的SDK 1 4一个Java程序的开发过程 1 5一个简单的Java应用程序的开发过程 1 6一个简单的Java小应用程序 1 7什么是JSP 习题 第2章标识符 关键字和数据类型 2 1标识 [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天乐!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值