jdk是Java开发工具包全称Java develoment kit
JDK = JRE + java的开发工具
JDK是提供给Java开发人员使用的其中包含了Java的开发工具,也包括了JRE.
所以安装了JDK,就不用单独安装JRE了。
JRE (java runtime environment)也就是Java运行环境,只是运行Java而不是开发的话,只需要运行开发好的.class文件,只需要安装JRE就可以了
JRE = JVM + java核心类库
包括Java虚拟机(JVM java virtual machine)和Java程序所需核心类库等,
如果想要运行一个开发好的Java程序,计算机中只需要安装JRE即可。
需求说明
开发一个Hello.java程序,可以输出“hello,world!“
开发步骤
Java源文件以.java为扩展名。源文件的基本组成部分是类(class),如本类中的Hello类。
Java应用程序的执行入口是main()方法。他有固定的书写格式:
public static void main(string[] args){...}
java语言严格区分大小写。
Java方法由一条条语句构成,每个语句以分号;结束。
大括号成对出现,缺一不可,程序中每一个类在编译的时候都会对应一个.class文件
一个源文件最多只能有一个public类,其他类的个数不限
如果源文件包含一个public类,则文件名必须按该类名命名!
一个源文件中最多只能有一个public类,其他类个数不限,也可以将main方法写在非public类中然后指定运行非public类,这样入口方法就是非public的main方法
这个实例演示了Java转义字符用法,特别注意回车和换行的区别
//演示转义字符
public class ChangeChar{
public static void main(String[] args) {
//\t :一个制表位,实现对齐功能
System.out.println("北京\t天津\t上海");
//\n :换行符号
System.out.println("jack\nsmith\nmarry");
//\\ :一个\
System.out.println("C:\\Windows\\System32\\cmd.exe");
//\" :一个"
System.out.println("老韩说:\"要好好学习Java,有前途\"");
//\' :一个'
System.out.println("老韩说:\'要好好学习Java,有前途\'");
// \r :一个回车,注意,回车和换行是不一样的
// 这个地方回车,光标定位到开头,然后执行北京,替换开头两个字
System.out.println("世界你好,我是傅硕\r北京");
}
}
课堂练习
//转义字符课堂练习
public class ClassTestZy{
public static void main(String[] args) {
System.out.println("书名\t作者\t价格\t销量\n三国\t罗贯中\t120\t1000");
}
}
效果如图:
找不到文件可能原因
主类名和文件名不一致
解决办法:声明为public的主类名应该与文件名一致,否则编译失败
缺少分号
解决办法:编译失败,注意错误出现的行数,再到源代码中指定位置改错
常见语法错误举例
Java注释类型
单行注释//
多行注释/**/,多行注释不允许嵌套
文档注释/***/
注释内容可以被JDK提供的工具javadoc所解析,生成一套以网页文件形式体现的该程序说明文档,一般写在类
//文档注释
/**
* @author fushuo
* @version 1.0
*/
public class common{
//编写一个main方法
public static void main(String[] args){
}
}
命令行输入:
javadoc -d D:\Users\傅硕\OneDrive\桌面\javacode\temp -author -version common.java
javadoc 工具软件识别以下标签:
标签 | 描述 | 示例 |
---|---|---|
@author | 标识一个类的作者 | @author description |
@deprecated | 指名一个过期的类或成员 | @deprecated description |
{@docRoot} | 指明当前文档根目录的路径 | Directory Path |
@exception | 标志一个类抛出的异常 | @exception exception-name explanation |
{@inheritDoc} | 从直接父类继承的注释 | Inherits a comment from the immediate surperclass. |
{@link} | 插入一个到另一个主题的链接 | {@link name text} |
{@linkplain} | 插入一个到另一个主题的链接,但是该链接显示纯文本字体 | Inserts an in-line link to another topic. |
@param | 说明一个方法的参数 | @param parameter-name explanation |
@return | 说明返回值类型 | @return explanation |
@see | 指定一个到另一个主题的链接 | @see anchor |
@serial | 说明一个序列化属性 | @serial description |
@serialData | 说明通过writeObject( ) 和 writeExternal( )方法写的数据 | @serialData description |
@serialField | 说明一个ObjectStreamField组件 | @serialField name type description |
@since | 标记当引入一个特定的变化时 | @since release |
@throws | 和 @exception标签一样. | The @throws tag has the same meaning as the @exception tag. |
{@value} | 显示常量的值,该常量必须是static属性。 | Displays the value of a constant, which must be a static field. |
@version | 指定类的版本 | @version info |
dir d:\abc\test200
cd /D c:
cd d:\abc\test
cd ..\..\abc\test
cd ..
cd \
变量有三个基本要素:类型,名称和值
如:int a = 1;定义一个变量,类型int整形,名称a,值1
System.out.println(100 + 98);//198
System.out.println("100" + 98);//10098
System.out.println(100 + 3 + "hello");//103hello
System.out.println("hello" + 100 + 3);//hello1003
public strtic void main(String[] args){
int n1 = 1;
long n2 = lL;
}
float num1 = 1.1
这样写是错误的,因为默认是double类型.
float num1 = 1.1F
这样写是正确的
double num1 = 1.1
或者是这样写
double num1 = 1.1F
这样写也是对的,因为空间是足够的
十进制数形式如:5.12 431.0f .512(必须要有小数点)
科学计数法形式:如:5.12e2[5.12 *10的二次方] 5.2E-2 [5.12 *10的负二次方]
double num11 = 2.7;
double num12 = 8.1 / 3;//2.7
System.out.println(num11);//2.7
System.out.println(num12);//接近2.7的一个数,但不是2.7
//得到的启示是,在对运算结果是小数的进行相等判断时,小心
//应该是以两个数差值的绝对值,在某个精度范围内判断
if(num11 == num12){
System.out.println("相等");//这样写是错误的,根本不会输出这个.
}
//正确的写法
if(Matn.abs(num11 - num12) < 0.000001){
System.out.println("equal")
}
如果知道这个类在那个包下就先定位到包,再找到类,然后是方法.
不知道类在哪个包的情况.
//演示char的基本使用
public class VarDetail{
public static void main(String[] args){
char c1 = 'a';
char c2 = '\t';
char c3 = 'han';
char c4 = 97; //说明:字符类型可以直接存放一个数字这里输出A
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
}
}
char c1 = 'a'
;
char c2 = '中'
;
char c3 = "9"
;//这样写是错的,双引号是字符串.
char c3 = '\n';
在Java中,char本质是一个整数,在输出的时候是Unicode对应的字符.
可以直接给char附一个整数,然后输出,按照对应的Unicode字符输出[97]
char类型可以进行运算,相当于一个整数,因为他都对应的有Unicode码.
字符串类型本质探讨
一共规定了128个字符的编码,只占用了一个字节的后面7位,最前面的1位统一规定位0,缺点是不能表示所有的值。
一个英文字母和一个汉字都占用2个字节,这对存储空间造成了浪费,Unicode兼容ASCII码,表示最大字符是65536
public class Boolean{
public static void main(String[] args){
//演示判断成绩是否通过
//定义一个布尔变量
boolean inPass = true;
if(isPass == true){
System.out.println("考试通过,恭喜");
}else{
System.out.println("考试没有通过,下次努力");
}
}
}
4. 使用细节
不可以用非0的整数替代false和true这点和c语言不同。
Java程序进行赋值或者运算的时候,精度小的类型自动转换为精度大的数据类型,这个就是自动转换类型
数据类型按精度大小排序位这个要记住
char->int->long->float->double
byte->short->int->long-float-double
int a = 'c';//这个是对的
double d = 80;//这个是对的
public class AutoConvertDetail{
public static void main(String[] args){
int n1 = 10;
double d1 = n1 + 1.1;//注意1.1默认位double类型,
float d1 = n1 + 1.1;//这样写时错误的因为1.1时double类型,1.1f才是float类型。
int n2 = 1.1;//错误,double不能转为int
//(byte,short)和char不发生类型转换
//当把数赋给byte时,先判断数是否在byte范围内,如果是就可以
byte b1 = 10;//对,-128,-127先判断类型
int n2 = 1;
byte b2 = n2;//这样是不行的,因为n2已经是int类型了
char c1 = b1;//错误,原因是byte不能自动转成char
//出现了short,char,byte中任何一个,精度就会提升。
byte b2 = 1;
byte b3 = 1;
short s2 = 1;
short s2 = b2 + s1;//错,b2+s1->int
byte b4 = b2 + b3;//错,b2 + b2 ->int
}
}
public class ForceConvert{
public static void main(String[] args){
//演示强制转换类型
int n1 = (int)1.9;
System.out.println("n1=" + n1);
int n2 = 2000;
byte b1 = (byte)n2;//数据超过了byte范围-127,128
System.out.println("b1"+b1);
}
}
int x = (int)10*3.5 + 1.5*6//这样写会报错,因为强转符号只针对最近的操作数有效。
int x = (int)(10*3.5 + 6*1.5)//这样就是正确的。
char c1 = 100;//ok
int m = 100;//ok
char c2 = m;//错误,int精度大
char c3 = (char)m;//可以,这是强转
判断下列语句能否通过编译:
short s = 12;//不可以,9是int类型,s是short无法赋值,必须强制类型转换
s = s-9;
//----------
byte b = 10;//不可以,理由同上
b = b + 11;
b = (byte)(b+11);
//----------
char c = 'a';//可以,自动转为精度最大的float,doule可以接受float
int i = 16;
float d = .314F;
double result = c + i + d;
//----------
byte b = 16;//不可以,只要有byte,short和char参与运算一律提升了精度为int
short s = 14;
short t = s + b;
int n1 = 100;
float f1 = 1.1F;
double d1 = 1.4;
boolean b1 = true;
//--------------
String s1 = n1 + "";
String s2 = f1 + "";
String s3 = d1 + "";
String s4 = n1 + "";
String s5 = "123"
System.out.println(s1+" "+s2+" "+s3+" "+s4);
String s5 = "123";
int num1 = Integer.parseInt(s5);
double num2 = Double.parseDouble(s5);
float num3 = Float.parseFloat(s5);
long num4 = Long.parseLong(s5);
byte num5 = Byte.parseByte(s5);
byte num5 = Boolean.parseBoolean("true");
short num6 = short.parseShort(s5);
//字符串转为字符:就是把字符串第一个字符取出
s5.charAt(0);//解读:就是将s5字符串的第一个字符返回。得到的是字符不是数字。
在把String类型转为基本数据类型的时候,确保,String类型能够转为有效的数据,比如不能把“hello"转为一个整数,如果格式不正确,就会抛出异常。
public class Homework02.java{
public static void main(String[] args){
char c1 = '\n';
char c2 = '\t';
char c3 = '\\';
char c4 = '1';
char c5 = '2';
char c6 = '3';
System.out.println(c1);
}
}
public class HomeWork03{
public static void main(String[] args){
String s1 = "老人与海";
String s2 = "骆驼祥子";
char sex1 = "男";
char sex2 = "女";
double price1 = 10;
double price2 = 11;
System.out.println(s1+s2);
System.out.println(sex1 + sex2);//得到男字符值+女字符值
Syste.out.println(price1+price2);
}
}
public class Homework04{
public static void main(String[] args){
String name = "fushuo";
char gender = "男";
String hobby = "read";
double score = 100.0;
int age = 22;
System.out.println("姓名\t年龄\t成绩\t性别\t爱好\n"+name+"\t"+age+"\t"+score+"\t"+gender+"\t"+hobby)
}
}
算数运算符
对数值型运算
public class ArithmeticOperator{
public static void main(Strint[] args){
//演示算术运算符的使用
// /的使用
System.out.println(10 / 4);//等于2.不是2.5
double d = 10 / 4;
System.out.println(d);//等于2.0而不是2,也不是2.5
System.out.println(10.0/4);//等于2.5
//-----------
//取模的使用
//%的本质是a%b = a-a/b*b
System.out.println(10%3);//1
System.out.println(-10%3);//-1
System.out.println(10%-3);//1
System.out.println(-10%-3);//-1
//---------------
//++的使用
int i = 10;
//如果作为独立语句使用前置加加和后置加加一样
i++;
++i;
//作为表达式使用,++i先自增后赋值i++先赋值后自增
int j = 8;
int k = ++j;
int m = j++;
System.out.println("k=" + k + "j=" +j);
}
}
//面试题1
int i = 1;
i = i++;//使用临时变量,temp = i;->i = i+1;->i = temp
System.out.println(i);//结果是1
//面试题2
int i = 1;
i = ++i;//使用临时变量,i = i+1->temp = i;->i = temp
System.out.println(i);//结果是2
//面试题3
int i1 = 10;
int i2 = 20;
int i = i1++;
System.out.println("i="+i);//10
System.out.println("i2="+i2);//20
i = --i2;
System.out.println("i="+i);//19
System.out.println("i2="+i2);//19
假如还有59天放假,问:合xx个星期零xx天
public class ArithmeticOperatorExercise02{
public static void main(String[] args){
/*
思路分析
1.使用int变量days保存天数
2.一个星期是7天,星期数:days/7零xx天days%7
3.输出
*/
int days = 59;
int weeks = days/7;
int leftDays = days%7;
System.out.println(days+"天 合" + weeks + "星期零"+leftDays+"天")
}
}
定义一个变量保存华氏温度,华氏温度转换摄氏温度公式为5/9*(华氏温度-100),请求出华氏温度对应地摄氏温度
public class ArithmeticOperatorExercise03{
public static void main(String[] args){
double f = 44.2;
double c = 5.0/9*(f-100);//注意不可以是5,不然返回0!!!!!!因为5/9=0
System.out.println("华氏温度:"+f+"对应的摄氏温度:"+c);
}
}
关系运算符的相关案例
public class RelationalOperator{
public static void main(String[] args){
int a = 9;
int b = 8;
System.out.println(a>b);//T
System.out.println(a>=b);//T
System.out.println(a<=b);//F
System.out.println(a<b);//F
System.out.println(a == b);//F
System.out.println(a!=b);//T
boolean flag = a>b;//T
System.out.println(flag)//T
}
}
public class LogicOperator{
public static void main(String[] args){
//&&和&的使用
int age = 50;
if(age > 20 && age < 90){
System.out.println("ok100");
}
if(age > 20 & age < 90){
System.out.println("ok100");
}
}
}
&&:如果第一个条件false,第二个条件不会判断,最终条件false,效率高
&:逻辑与,不管第一个条件是否为false,第二个条件都会判断,效率低
开发中基本使用&&,效率高
短路或:如果第一个条件是true那么第二个条件不会判断,效率较高,逻辑或,不管第一个条件是否为true第二个条件都会判断。
int x = 5;
int y = 5;
if(x++==6 & ++y==6){
x = 11;
}
System.out.println("x="+x+",y="+y);x:6,y:6
//--------
int x = 5,y = 5;
if(x++==5 | ++y==5){
x = 11;
}
System.out.println("x="+x+",y="+y);x:11,y:6
//--------
int x = 5,y = 5;
if(x++==6 && ++y==6){
x = 11;
}
System.out.println("x="+x+",y="+y);x:6,y:5
//----------
int x = 5,y = 5;
if(x++==5 || ++y==5){
x = 11;
}
System.out.println("x="+x+",y="+y);x:11,y:5
//-----------
boolean x = true;
boolean y = false;
short z = 46;
if((z++==46)&&(y=true)) z++;//注意y=true赋值语句
if((x=false)||(++z==49)) z++;
System.out.println("z="+z);//输出z:50
赋值运算符就是将某个运算后的值,赋给指定的变量
分类
赋值运算符的特点
int num = 20;int num2 = 78*34-10;int num3 = a;
复合赋值运算等价于下面的效果
比如:a+=3
等价于a=a+3
复合赋值运算符会进行类型转换。
byte b = 2;b+=3;b++;
int n1 = 10;
n1 += 4;//14
n1 /= 4;// 3
//复合赋值运算符会进行类型转换
byte b = 3;
b += 2;//真正等价于b = (byte)(b+2)因为b = b + 2会报错(精度提升)
//同样
b++;//底层也有类型转换b = (byte)(b+1)
条件表达式?表达式1:表达式2;
运算规则
int a = 10;
int b = 99;
int result = a>b?a++:b--;//先返回b然后自减,返回的结果是99而不是98
三元运算符的使用细节
int a = 3;
int b = 8;
int c = a > b ? (int)1.1 : (int)3.4;//强转类型才能匹配
double d = a > b ? a : b + 3;//可以的,double可以接受int
1. 三元运算符可以转成if--else语句
int res = a>b ? a++ : --b;
案例:实现三个数的最大值
public class TernaryOperatorExerise{
public static void main(String[] args){
int n1 = 55;
int n2 = 33;
int n3 = 123;
//思路
//先得到n1和n2中最大数,保存到max1,然后和n2比较
int max1 = n1 > n2 ? n1:n2;
int max = max1 > n3 ? max1:n3;
System.out.println(max);
//尝试使用一条语句实现,可以这样写,但是不清晰,后面还可以使用更好的方法解决
}
}
int totalNum = 10;int n = 90
比如:ThankShotGame
变量名、方法名:多单词组成的时候,第一个单词首字母小写,第二个单词开始买个单词首字母大写:xxxYyyZzz(小驼峰,简称驼峰法)
常量名:所有字母都大写,多单词时每个单词用下划线连接:XXX_YYY_ZZZ
比如:定义一个税率TAX_RATE
后面我们学习到类,包,接口等,我们命名规范要这样遵守,更加详细看文档
键盘输入语句需要一个扫描器(对象),就是Scanner
//案例演示
import java.util.Scanner//表示把java.util下的Scanner类导入
public class Input{
public static void main(String[] args){
//演示接受用户的输入
//步骤
//Scanner 类 表示简单文本扫描器,在java.util包
//1.引入/导入 Scanner类所在的包
//2.创建Scanner对象
Scanner myScanner = new Scanner(System.in);
//3.接受用户输入
System.out.println("请输入名字");
String name = myScanner.next();//接受用户输入字符串
System.out.println("请输入年龄");
int age = myScanner.nextInt();//接受用户输入int
System.out.println("请输入薪水");
double sal = myScanner.nextDouble();//接受用户double
System.out.println("人的信息如下:");
System.out.println("name="+name+"age="+age+"sal");
}
}
int n1 = 0b1010;
int n2 = 1010;
int n3 = 01010;
int n4 = 0x10101;
public static void main(String[] args){
int a = 1>>2;//1向右位移两位
int b = -1>>2;
int c = 1<<2;//
int d = -1<<2;//
int e = 3 >>> 2;//无符号右移
//a b c d e结果各是多少
}
~2 = ?;//按位取反
2&3 = ?;//按位与
2|3 = ?;//按位或
~-5=?;
13&7 = ?;
5|4 = ?;
-3^3 = ?//^按位异或
按位与&:两位全为1,结果为1,否则0
按位或|:两位有一个是1,结果是1,否则0
按位异或^:两位一个为0,一个为1,结果为1,否则0
按位取反~:0->1,1->0
比如:2&3=? ~-2 = ? ~2=?
int a = 1>>2
相当于1/2/2 = 0
int c = 1 << 2
相当于1*2 *2 = 4
10/3 3
10/5 3
10%2 0
//注意小数的取模运算
-10.5%3 -1.5//a%b 当a是小数时,公式= a -(int)a/b*b
int i = 66;
System.out.println(++i+i);//打印134
int num1 = (int)"18";//错误,要用包装类Interger.parseInt("18")
int num2 = 18.0;//错误
double num3 = 3d;//正确
double num4 = 8;//正确
int i = 49;char ch = i+1;t;//错误,尝试把int变成char
byte b = 19;short s = b+2;//错误,int-short
double d = Double.parseDouble("13.2");
char c1 = 'd';
String x = c1 + ""
程序从上到下逐行执行,中间没有任何判断和跳转
基本语法:
if(条件表达式){
执行代码块:(可以有多条语句)
}
说明:当条件表达式为true时,就会执行{}的代码。如果是false,就不执行。
特别说明,如果{}中只有一条语句,则可以不用{},但是建议写上{}
//案例说明,编写一个程序,可以输入人的年龄,如果年龄大于18岁,则输出,你的年龄大于18,要对自己的行为负责
import java.utils.Scanner;
public class If01{
public static void main(String[] args){
//思路分析
//接受输入的年龄,应该定义一个Scanner对象
//把年龄保存到一个变量int age
//使用if判断,输出信息
Scanner myScanner = new Scanner(System.in);
System.out.println("请输入年龄");
int age = myScanner.nextInt();
if (age >= 18){
System.out.println("你的年龄大于18,你要对自己的行为负责")
}
}
}
基本语法
if(条件表达式){
执行代码块1;
}
else{
执行代码块2;
}
说明:当条件表达式成立,即执行代码块1,否则执行代码块2,如果执行代码块只有一条语句则{}可以省略,否则不可以省略。
案例演示
import java.util.Scanner;
public class If02{
publci static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
System.out.println("请输入年龄");
int age = myScanner.nextInt();
if(age >= 18){
System.out.println("你的年龄大于18")
}else{
System.out.println("你的年龄不大")
}
}
}
//单分支和双分支练习题
//对下列代码,若有输出,指出输出结果
int x = 7;
int y = 4;
if(x > 5){
if(y>5){
System.out.println(x+y);
}
System.out.println("韩顺平教育");
}else
System.out.println("x is" + x);
//输出内容是:
//韩顺平教育
//编写程序,声明两个double变量并赋值,判断第一个数大于10.0,第二个数小于20.0,打印两数之和
public class IfExercise2{
public static void main(String[] args){
double x = 18.0;
double y = 3.0;
if(x>10.0 && y<20){
System.out.println("x+y="+(x+y))
}
}
}
//定义两个变量int判断二者之和,是否能被3又能被5整除,打印提示信息
public class IfExercise3{
public static void main(String[] args){
int x = 8;
int y = 4;
int z = x + y;
if(z%3==0&&z%5==0){
System.out.println(x+"和"+y"的和既可以被3又可以被5整除");
}else{
System.out.println(x+"和"+y"的和不能同时被3又可以被5整除");
}
}
}
//判断一个年份是否是闰年,闰年的条件时符合下面二者之一:
//年份能被4整除,但不能被100整除
//能被400整除
import java.util.Scanner;
public class IfExercise3{
public static void main(String[] args){
Scanner myYear = new Scanner(System.in);
System.out.println("请输入一个年份");
int year = myYear.nextInt();
if (year%400 ==0||(year%4==0&&year%100!=0){
System.out.println(year+"年是闰年");
}else{
System.out.println(year+"年不是润年");
}
}
}
案例演示
输入保国同志的芝麻信用分:
如果:
1信用分为100分时,输出信用极好
2信用分为(80,99)时输出信用优秀
3信用分为(60,80)时输出信用一般
4其他情况,输出信用不及格
5从键盘输入报国的芝麻信用分,并加以判断
import java.util.Scanner;
public class If3{
public static void main(String[] args){
Scanner my = new Scanner(System.in);
System.out.println("请输入你的芝麻信用分!");
int score = my.nextInt();
//先对输入信用分,进行一个有效判断,进行有效判断,否则提示输入错误
if(score >=1 && score <= 100){
if(score == 100){
System.out.println("信用良好");
}else if(score >80 && score <= 99 ){
System.out.println("信用优秀");
}else if(score > 60 && score <= 80 ){
System.out.println("信用一般")
}else{
System.out.println("信用不及格")
}
}else{
System.out.println("信用分需要在1-100,请重新输入")
}
}
}
案例演示2
看看代码输出什么?
boolean b = true;
if(b == false)
System.out.println("a");//如果改成b=false编译可以通过
else if(b)
System.out.println("b");//b
else if(!b)
System.out.println("c");
else
System.out.println("d");
在一个分支结构中完整的嵌套了另一个分支结构,里面的分支结构成为内层分支,外面的分支结构成为外层分支,但是嵌套不要超过三层(可读性不好)
if(){
if(){
//if else
}else{
//if-else
}
}
应用案例 参加歌手比赛,如果初赛成绩大于8进入决赛,否则提示淘汰,并且根据性别提示进入男子组或者女子组 可以让学员先练习一下,输入成绩和性别,进行判断和输出信息
import java.util.Scanner;
public class NestedIf.java{
public static void main(String[] args){
Scanner myInput = new Scanner(System.in);
System.output.println("请输入成绩");
double score = myInput.nextDouble();
System.output.println("请输入性别");
char gender = myInput.next().charAt(0);
if (score < 8.0){
System.out.println("你已经被淘汰");
}else{
if (gender == '男'){
System.out.println("你进入男子组比赛");
}else{
System.out.println("你进入女子组比赛");
}
}
}
}
嵌套分支的练习题 要求: 出票系统,根据淡旺季的月份和年龄,打印票价 旺季(4-10):城人(18-60):60 儿童(<18):半价 老人(>60):1/3 淡季: 成人:40 其他:20
import java.util.Scanner;
public class If{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
System.out.println("请输入月份");
int month = myScanner.nextInt();
System.out.println("请输入年龄");
int age = myScanner.nextInt();
if (month >=4 && month <= 10){
if (age>60){
System.out.println("你的票价为20元");
}else if (age < 18){
System.out.println("你的票价为"+60/2+"元");
}else{
System.out.println("你的票价为60元");
}
}else{
if (age < 18){
System.out.println("你的票价为20元");
}else{
System.out.println("你的票价为40元");
}
}
}
}
案例: 编写一个程序,该程序可以接收一个字符,a,b,c,d,e,f,g a表示星期一,b表示星期二 根据用户输入显示相应的信息。要求使用switch语句完成
import java.util.Scanner;
public class Switch01{
public static void main(String[] args){
Scanner.out.println("请输入一个字符(a-g)");
Scanner myScanner = new Scanner(System.in);
char ch = myScanner.next().charAt(0);//在Java中只要有值返回就是一个表达式
stitch(ch){
case 'a':
System.out.println("今天是星期一");
break;
case 'b':
System.out.println("今天是星期二");
.......
default:
System.out.println("你输入的信息不匹配");
}
}
}
public class SwitchDetail{
public static void main(String[] args){
char c = 'a';
switch(c){
case 'a':
System.out.println("do");
break;
case "dddd"://和表达式类型不匹配,而且无法转换
System.out.println("dd");
break;
case '20'://可以的,应为char可以转换为int
System.out.println("sd");
default:
System.out.println("dd");
}
}
}
double c = 1.0;
switch(c){
case 1.0://错误
}
课堂练习 SwitchExercise.java
import java.util.Scanner;
public class SwitchExercise{
public static void main(String[] args){
Scanner myScanner = new Scanner(system.in);
System.out.println("请输入字符");
char ch = Scanner.next().charAt(0);
switch (ch){
case 'a':
ch = 'A';
break;
case 'b':
ch = 'B';
break;
case 'c':
ch = 'C';
break;
case 'd':
ch = 'D';
break;
case 'e':
ch = 'E';
break;
default:
System.out.println("other");
}
}
}
题目2:
import java.util.Scanner;
public class SwitchExercise02{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
System.out.println("请输入学生成绩");
int score = myScanner.nextInt();
if (score <=100 && score >=0){
switch (score/60){
case 0:
System.out.println("成绩不合格");
break;
case 1:
System.out.println("成绩合格");
}
}else{
System.out.println("你的输入有误请重新输入");
}
}
}
题目3:
import java.util.Scanner;
public class SwitchExercise03{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
int month = myScanner.nextInt();
switch (month/3){
case 1:
System.out.println("春季");
break;
case 2:
System.out.println("夏季");
break;
case 3:
System.out.println("春季");
break;
default:
System.out.println("冬季");
}
}
}
题目3另一种解答(利用穿透)
import java.util.Scanner;
public class SwitchExercise03{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
int month = myScanner.nextInt();
switch (month/3){
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
case 12:
case 1:
case 2:
System.out.println("冬季");
default:
System.out.println("输入有误");
}
}
}
如果判断的具体数值不多而且更符合byte,short,int,char,enum,String这两种类型,虽然两种语句都可以说hi用但是更建议使用switch语句
其他情况,对区间判断,对结果为Boolean类型判断使用if
int i = 1;
for(; i <= 10 ;){
System.out.println("你好");
i++;
}
int count = 3;
for(int i = 0,j = 0;i < count;i++,j += 2){
System.out.println("i=" + i + "j=" +j);
}//输出00 12 24
for循环控制练习 1.打印1~100之间所有9的倍数的整数,统计个数,以及总和(化繁为简,先死后或)
public class ForExercise{
public static void main(String[] args){
int sum = 0;
int count = 0;
for(int i = 0 ;i <=100 ;i++){
if(i % 9 == 0){
System.out.println("i="i);
count++;
sum += i;//累积
}
}
}
}
循环变量初始化; while(循环条件){ 循环体(语句); 循环变量迭代; }
//while循环的案例
public class While01{
public static void main(String[] args){
int i = 1;//循环变量初始化
while(i <= 10 ){
System.out.println("你好"+i);
i++;
}
System.out.println("退出循环");
}
}
while循环的练习题
public class WhileExercise{
public static void main(String[] args){
int i = 1
while(i <= 100){
if(i%3==0){
System.out.println("i="+i);
}
i++;
}
System.out.println("退出循环");
}
}
public class WhileExercise2{
public static void main(String[] args){
int i = 40;
while(i<=200){
if (i%2==0){
System.out.println(i);
}
i++;
}
}
}
循环变量初始化 do{ 循环体(语句); 循环变量迭代; }while(循环条件);
int i = 1;//循环变量初始化
do{
//循环执行语句
System.out.println("你好,世界"+i);
//循环变量迭代
i++;
}while(i <= 10);
System.out.println("退出循环")
课堂练习题
public class DoWhileExercise{
public static void main(String[] args){
int i = 1;
do{
System.out.println("i="+i);
i++;
}while(i <= 100);
}
}
public class DoWhileExercise02{
public static void main(String[] args){
int i = 1;
int sum = 0;
do{
sum += i;
i++;
}while(i <= 100);
System.out.println(sum);
}
}
public class DoWhileExercise{
public static void main(String[] args){
int i = 1;
int sum = 0;
do{
if(i % 5 == 0 && i % 3 != 0){
System.out.println(i);
}
}while( i <= 200);
}
}
import java.util.Scanner;
public class DoWhileExercise{
public static void main(String[] args){
//不停的问还钱吗?
//使用char answer接受回答
//在do-while的while判断如果是y就不在循环。
char answer = ' ';
Scanner myScanner = new Scanner(System.in);
do{
System.out.println("老韩使出五连鞭");
System.out.println("老韩问:还钱吗?y/n");
answer = myScanner.next().charAt(0);
System.out.println("他的回答是:"+answer);
}while(answer != 'y');
}
}
多重循环执行步骤分析:
//双重for
for(int i = 0;i < 2 ;i++){
for(int j = 0;j<3;j++){
System.out.println("i=" + i + "j=" + j);
}
}
多重循环控制应用实例
import java.util.Scanner;
public class MulForExercise{
public static void main(String[] args){
double avgScore = 0;
double sumScore = 0;
int count = 0;//累计及格人数
Scanner myScanner = new Scanner(System.in);
for(int i = 0;i < 3;i++){
for(int j = 0;j < 5;j++){
double score = 0;
System.out.println("请输入第"+(i+1)+"个班级,第"+(j+1)+"个学生的成绩");
score = myScanner.nextDouble();
System.out.println("输入完成");
sumScore += score;
if(score > =60){
count += 1;
}
}
System.out.println("该班级所有学生的平均成绩为"+sumScore/5+"分");
}
avgScore = sumScore/15;
System.out.println("三个班级的平均成绩为"+avgScore);
}
}
public class MulFor{
public static void main(String[] args){
for(i=1;i<=9;i++){
for(j=1;j<=9;j++){
}
}
}
}
多从循环控制练习题
public class Stars{
public static void main(String[] args){
for(int i = 1;i<=5;i++){
for(int k=1;k<=5-i;k++){
System.out.print(' ');
}
for(int j=1;j<=2*i-1;j++){
System.out.print('*');
}
System.out.print('\n');
}
}
}
public class Stars{
public static void main(String[] args){
for(int i = 1;i<=5;i++){
for(int k=1;k<=5-i;k++){
System.out.print(' ');
}
for(int j=1;j<=2*i-1;j++){
if(j==1||j==2*i-1||i==5){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.print('\n');
}
}
}
for(int i=0;i<=10;i++){
if(i == 3){break;}
System.out.println("ok"+i);
}
需求:随机生成1-100的一个数,直到生成97,看看你一共用了多少次? 提示:使用(int)(Math.random()*100)+1 思路分析:
public class Break{
public static void main(String[] args){
int count = 0;
while(true){
count = count+1;
int num = (int)(Math.random()*100)+1;
if(num == 97){break;}
}
System.out.println("一共用了"+count+"次猜中");
}
}
注意事项和细节说明:
课堂练习题
public class BreakExercise{
public static void main(String[] args){
int count = 0;
for(int i = 1;i<=100;i++){
count = count + 1;
if(count > 20){break;x = i;}
aaa }
System.out.println("当和第一次大于20时,当前数为"+x);
}
}
import java.util.Scanner;
public class BreakExercise02{
public static void main(String[] args){
int count = 3;
Scanner myScanner = new Scanner(System.in);
for(int i = 1;i<=3;i++){
System.out.println("请输入用户名和密码。");
String username = myScanner.next();
String password = myScanner.next();
if("丁真".equals(username) && "666".equals(password)){
System.out.println("登录成功");
break;
}else{
System.out.println("登录失败,你还有"+(3-i)+"次机会");
}
}
}
}
基本结束:
基本语法:
{ ... continue; ... } ddd 例子:
public class Continue01{
//编写一个main方法
public static void main(String[] args){
//代码
int i = 1;
while( i <= 4){
i++;
if( i == 2){
continue;
}
System.out.println("i="+i);
}
}
}
for(int i=1;i<=5;i++){
if(i==3){
System.out.println("韩顺平教育"+i);
return;//return用在主方法中,直接退出程序,所以输出两个hello world和韩顺平教育
}
System.out.println("Hello World");
}
System.out.println("go on");
1.编程实现如下功能 某人有100000元,每经过一个路口,需要缴费,规则如下: 1)当现金>50000时,每次交5% 2)当现金<=50000时,每次交1000 编程计算该人可以经过多少次路口,要求:使用while break方法完成。
public class JiaoFei{
public static void main(String[] args){
double money = 100000;
int count = 0;
while(true){
if(money>50000){
money = 0.95*money;
}else if(money>1000 && money<=50000){
money -= 1000;
}else{
break;
}
count = count + 1;
}
System.out.println("他可以经过"+count+"次路口");
}
}
我的答案是63 真实结果应该是62,错误原因是我吧第二个判断条件money>1000写成了money>0; 2.判断一个整数,属于哪个范围:大于0;小于0;等于0
import java.util.Scanner;
public class ZhengShuPanDuan{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
int number = myScanner.nextInt();
if (number > 0){
System.out.println("大于0");
}else if(number == 0){
System.out.println("等于0");
}else{
System.out.println("小于0");
}
}
}
3.判断一个年份是否为闰年
import java.util.Scanner;
public class RunNian{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
int year = myScanner.nextInt();
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)){
System.out.println(year+"年是闰年");
}else{
System.out.println(year+"年是平年");
}
}
}
4.判断一个整数是否为水仙花数,所谓水仙花数是指一个三位数,其各个位上数字立方和等于其本身,例如:153 = 111+333+555
import java.util.Scanner;
public class ShuiXianHua{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
int num = myScanner.nextInt();
int ge = num % 10;
int shi = (int)((num % 100) / 10);
int bai = (int)(num / 100);
if (num == ge*ge*ge + shi*shi*shi + bai*bai*bai){
System.out.println(num+"是水仙花数");
} else{
System.out.println(num+"不是水仙花数");
}
}
}
5.看看一下代码输出什么?
public class Demo{
public static void main(String[] args){
int m = 0,n = 3;
if(m>0){
if(n>2)
System.out.println("ok1");
else
System.out.println("ok2");
}
}
}
什么都不输出;
6.输出1-100之间的不能被5整除的数,每5个一行
public class Exercise06{
public static void main(String[] args){
int count = 0;
for(int i = 1;i<=100;i++){
if(i % 5 != 0){
System.out.print(i+"\t");//注意,这里不可以用单引号,会报错的
count += 1;
}
if(count % 5 == 0){
System.out.println();
}
}
}
}
7.输出小写的a-z以及大写的Z-A
public class Exercise07{
public static void main(String[] args){
for(char c1 = 'a';c1<='z';c1++){
System.out.println(c1+" ");
}
for(char c1 = 'Z';c1 >= 'A';c1--){
System.out.println(c1+" ");
}
}
}
8.求出1-1/2+1/3-1/4...1/100的和
public class Exercise08{
public static void main(String[] args){
double num = 1.;
for(int i = 2;i<=100;i++){
if(i%2 == 0){
num = num-1./i;//注意这个地方不能写成1/i必须写成1./i
}else{
num = num + 1./i;
}
}
System.out.println("结果为"+num)
}
}
我的结果为1.0,正确的结果是0.688错误的原因是是因1/2=0,1/3=0....... 9.求1+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+3+...+100)的结果
public class Exercise09{
public static void main(String[] args){
int count = 0;
int count1 = 0;
for(int i = 1;i<=100;i++){
count = count + i + count1;
count1 = count;
}
System.out.println("结果为"+count);
}
}
我的结果为-102,整个逻辑就错了。修改过后如下,结果为171700:
public class Exercise09{
public static void main(String[] args){
int sumAll = 0;
for(int i = 1;i<=100;i++){
int sumSingle = 0;
for(int j = 1;j<=i;j++){
sumSingle = sumSingle + j;
}
sumAll += sumSingle;
}
System.out.println("结果为"+sumAll);
}
}
//定义一个数组
double[] hens = {3,5,1,3.4,2,50};
//数组遍历
for(int i = 0;i<6;i++){
System.out.println(hens[i]);
}
//提示:可以通过数组名.length得到数据的大小/长度
public class Array02{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//声明并开辟空间
double[] scores = new double[5];
//赋值
for(int i = 0;i<5;i++){
scores[i] = input.nextDouble();
}
//打印
for(int i = 0;i<5;i++){
System.out.println(scores[i]);
}
}
}
public class ArrayExercise01{
public static void main(String[] args){
char array[] = new char[26];
int start = 0;
for(char i = 'A';i<='Z';i++){
array[start] = i;
start += 1;
}
for(int i = 0;i<26;i++){
System.out.println(array[i]);
}
}
}
2.请求出一个数组int[]的最大值{4,-1,9,10,23},并得到对应的下标。
public class ArrayExercise02{
public static void main(String[] args){
int[] array = {4,-1,9,10,23};
int t = 0;
int n = 0;
int i = 0;
for(;i<5;i++){
if(array[i]>t){
t = array[i];
n = i;
}
}
System.out.println("最大值为"+t+"对应的下标为:"+n);
}
}
3.求出一个数组的和和平均值
public class ArrayExercise03{
public static void main(String[] args){
int[] array = {3,2,5,1,8,2,34};
double sum = 0.;
double average = 0.;
for(int i = 0;i<array.length;i++){
sum += array[i];
}
average = sum/(array.length);
System.out.println("数组的和为"+sum+"数组的平均值为"+average);
}
}
int n1 = 2;int n2 = n1;//n2的变化不会影响n1 2. 数组在默认情况下是引用传递,赋的值是地址。 3. 看一个案例,分析数组赋值的内存图(重点)。 int n1 = 10; int n2 = n1; n2 = 90; 此时n1不会变成90,仍然是10. 但是数组就不一样了,是地址拷贝(引用拷贝)看下面例子 int [] arr1 = {1,2,3}; int [] arr2 = arr1;//吧arr1赋给arr2,此时arr2的变化会影响arr1 arr2[0] = 10; //此时arr1[0]也变成了10;
int[] arr1 = {1,2,3};
int[] arr2 = arr1;
编写代码 实现数组拷贝(内容复制)将int[] arr1 = {10,20,30};拷贝到arr2数组,要求数据空间是独立的
int[] arr1 = {10,20,30};
int[] arr2 = arr1;//不能这么干,这样说白了就是让arr2指向arr1,他们的数据空间肯定不是独立的.
//正确的做法
//创建一个新的数组arr2,开辟新的数据空间
//大小arr1.length
int[] arr2 = new int[arr1.length];
//遍历arr1,把每个元素拷贝到arr2对应的元素位置
for(int i = 0;i<arr1.length;i++){
arr2[i] = arr1[i];
}
要求:把数组的元素内容反转 arr{11,22,33,44,55,66}->{66,55,44,33,22,11} //思考
//方法1
int[] arr = {11,22,33,44,55,66};
//思路:把arr[0] 和arr[5]进行交换,以此类推。
//一共要交换3次 = arr.length/2
//每次交换时,对应的下标是arr[i]和arr[arr.length-1-i]
for(int = 0;i<arr.length/2;i++){
int temp = arr[arr.length-1-i];//暂存
arr[arr.length-1-i] = arr[i];
arr[i] = temp;
}
System.out.println("====反转后数组====");
for(int i = 0;i<arr.length;i++){
System.out.println(arr[i] + "t");
}
//方法2
int[] arr1 = {11,22,33,44,55,66};
int len = arr1.length;
int[] arr2 = new int[len];
for(int i = 0;i<len;i++){
arr2[len-i-1] = arr1[i];
}
System.out.println("=====反转后的数组====");
for(int i = 0;i<len;i++){
System.out.println(arr2[i] + "\t");
}
//或者方法2也可以这么写
for(int i = len-1,j=0;i>=0;i--,j++){
arr2[j] = arr[i];
}
要求:实现动态的给数组添加元素效果,实现对数组扩容。
public class ArrayAdd{
public static void main(String[] args){
/*
思路分析
1.定义初始数组int[] arr = {1,2,3}//下标0-2
2.定义一个新的数组 int[] arrNew = new int[arr.length+1];
3.遍历arr数组,依次将arr的元素拷贝到arrNew数组
4.将4赋值给arrNew最后一个元素。[arrNew.length-1] = 4;
5.让arr 指向arrNew;arr = arrNew;那么原来的arr数组就被销毁。
*/
int[] arr = {1,2,3};
int[] arrNew = new int[arr.length + 1];
//遍历arr数组,一次将arr的元素拷贝到arrNew数组
for(int i = 0;i< arr.length;i++){
arrNew[i] = arr[i];
}
//把4赋给arrNew的最后一个元素
arrNew[arrNew.length - 1] = 4;
//让arr 指向arrNew;
arr = arrNew;
//输出arr看看效果
System.out.println("======arr扩容后元素情况======");
for(int i = 0;i< arr.length;i++){
System.out.println(arr[i] + "\t");
}
}
}
//下面的程序是为了实现3功能
import java.util.Scanner;
public class ArrayAdd02{
public static void main(String[] args){
int[] arr = {1,2,3};
Scanner myScanner = new Scanner(System.in);
while(true){
System.out.println("是否继续?y/n");
char ans = myScanner.next().charAt(0);
if(ans == 'n'){break;}
System.out.println("请输入要添加的元素");
int[] arrNew = new int[arr.length + 1];
//遍历arr数组,一次将arr的元素拷贝进arrNew数组
for(int i = 0;i<arr.length;i++){
arrNew[i] = arr[i];
}
int num = myScanner.nextInt();
//把num赋给arrNew最后一个
arrNew[arrNew.length - 1] = num;
//让arr指向arrNew;
arr = arrNew;//原来的arr会被销毁
//输出arr看看效果
System.out.println("=====arr扩容后元素情况=======");
for(int i = 0;i< arr.length;i++){
System.out.println(arr[i] + "\t");
}
}
}
}
课后练习题: 要求:有一个数组{1,2,3,4,5}可以将该数组进行缩减,提示用户是否继续缩减,每次缩减最后一个元素,当只剩下最后一个元素时提示:不能再缩减。
import java.util.Scanner;
public class ArrayReduce{
public static void main(String[] args){
int[] myArray = {1,2,3,4,5};
Scanner myScanner = new Scanner(System.in);
while(true){
int[] myNewArray = new int[myArray.length-1];
//提示用户是否进行缩减
System.out.println("是否进行缩减操作?y/n");
char ans = myScanner.next().charAt(0);
if(ans == 'n'){System.out.println("程序退出");break;}
if(myArray.length == 1){
System.out.println("元素只剩一个,不能缩减");
break;
}
for(int i = 0; i<myNewArray.length;i++){
myNewArray[i] = myArray[i];
}
myArray = myNewArray;
//输出myArray看看效果
for(int i = 0;i<myArray.length;i++){
System.out.print(myArray[i] + "\t");
}
}
}
}
排序是将一群数据,依照指定的顺序进行排列的过程 排序的分类
冒泡排序的基本思想时:通过对待排序序列从后向前(从下标较大的元素开始),依次比较相邻元素的值,若发现逆序则交换,使得值较大的元素逐渐从前移向后部,就像水底气泡一样向上冒。
将五个无序:24,69,80,57,13使用冒泡排序法从小到大排列。
public class BubbleSort{
//编写一个main方法
public static void main(String[] args){
//化繁为简,先死后活
/*
数组[24,69,80,57,13]
第一轮排序:目标把最大数放在最后
第一次比较[24,69,80,57,13]
第二次比较[24,69,80,57,13]
第三次比较[24,69,57,80,13]
第四次比较[24,69,57,13,80]
*/
int[] arr = {24,69,80,57,13};
int temp = 0;
for(int i = 0;i<arr.length-1;i++){
for(int j = 0;j<arr.length-i;j++){//4次比较
//如果前面的数>后面的数,就交换
}
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
System.out.println("===第"+(i+1)+"轮===");
for(int j = 0;j< arr.length;j++){
System.out.println(arr[j]+"\t");
}
}
}
在Java中,我们常用的查找有两种:
案例演示:
public class SeqSearch{
public static void main(String[] args){
String[] names = {"白眉鹰王","金毛狮王","紫衫龙王","青翼蝠王"};
//遍历数组,注意比较,如果有,则提示信息,并退出
//这里老师给大家一个编程思想/技巧,一个经典的方法
int index = -1;
for(int i = 0;i< names.length;i++){
//比较字符串equals,如果找到名字就是当前元素
if(findName.equals(names[i])){
System.out.println("恭喜你找到"+findName);
System.out.println("下标为"+i);
//把i保存在index中
index = i;
break;
}
}
if(index == -1){
//没有找到
System.out.println("没有找到");
}
}
}
请用二维数组输出如下图形 000000 001000 020300 000000
//从形式上看int[][]
//可以这样理解,原来的一维数组的每个元素是一维数组
int[][] arr = {{0,0,0,0,0,0},
{0,0,1,0,0,0},
{0,2,0,3,0,0},
{0,0,0,0,0,0}};
//输出二维图形
for(int i = 0;i< arr.length;i++){
//遍历二维数组的每个元素
//遍历二维数组的每个元素
for(int j = 0;j<arr[i].length;j++){
System.out.println(arr[i][j] + " ");
}
System.out.println();//换行
}
使用方法2:动态初始化
使用方法3:动态初始化-列数不确定
public class TwoDimensionalArray03{
//编写一个main方法
public static void main(String[] args){
/*
看一个需求:动态创建下面的二维数组,并输出
i = 0:1
i = 1:2 2
i = 2:3 3 3
一个有三个一维数组,每个一维数组的元素是不一样的
*/
//创建一个二维数组,一个有3个一维数组,但是每一个一维数组还没有开数据空间。
int[][] arr = new int[3][];//列数不确定就不写。
for(int i = 0;i<arr.length;i++){
//遍历arr每一个一维数组
//如果没有给一维数组new 那么arr[i]就是null
arr[i] = new int[i+1];
//遍历一维数组,并给一维数组的每个元素赋值
for(int j = 0;j< arr[i].length;j++){
arr[i][j] = i + 1;
}
}
//遍历arr输出
for(int i = 0;i< arr.length;i++){
for(int j = 0;j< arr[i].length;j++){
System.out.print(arr[i][j] + " ");
}
}
}
}
案例:int arr[][] = {{4,6},{1,4,5,7},{-2}};遍历该二维数组,并求和
public class TwoDimensionalArray05{
//编写main方法
public static void main(String[] args){
int count = 0;
int arr[][] = {{4,6},{1,4,5,7},{-2}};
for(int i = 0;i<arr.length;i++){
for(int j = 0;j< arr[i].length;j++){
count = count + arr[i][j];
}
}
System.out.println("该二维数组的和为"+count);
}
}
案例:使用二维数组打印一个10行的杨辉三角 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 规律:
public class YangHui{
public static void main(String[] args){
int[][] arr = new int[10][];//开辟空间
for(int i = 0;i<arr.length;i++){
arr[i] = new int[i+1];//开辟内层空间
for(int j = 0;j< arr[i].length;j++){
if(j == 0 || j == arr[i].length - 1){//每一行第一个元素和最后一个元素都是1
arr[i][j] = 1;
}else{
arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
}
}
}
//输出打印看一下
for(int i = 0;i<arr.length;i++){
for(int j = 0;j<arr[i].length;j++){
System.out.print(arr[i][j] + "\t");
}
}
System.out.println();
}
}
第一题我选B,D
第二题我觉得答案是blue
第三题输出1 3 5 7
第四题
import java.util.Scanner;
public class Homework05{
public static void main(String[] args){
/*
已知有个升序的数组,要求插入一个元素,该数组顺序依然是升序,比如:[10,12,45,90],添加23后数组为[10,12,23,45,90]
*/
//这里我首先用的是冒泡排序做法。
int[] arr = {10,12,45,90,100,110,112};
int[] arrNew = new int[arr.length+1];
int temp = 0;
System.out.println("请输入一个元素");
Scanner myScanner = new Scanner(System.in);
int num = myScanner.nextInt();
arrNew[0] = num;
for(int i = 0;i<arr.length;i++){
arrNew[i+1] = arr[i];
}
arr = arrNew;
for(int j = 0;j< arr.length-1;j++){
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
System.out.println("添加后数组值如下:");
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i]+"\t");
}
}
}
根据韩老师的提示,换一个做法
import java.util.Scanner;
public class Homework05{
public static void main(String[] args){
/*主要思路
1.先确定添加数应该插入到那个索引?
2.然后扩容
*/
//定义原始数组
int[] arr = {10,12,45,98,100,110,112};
int[] arrNew = new int[arr.length+1];//定义新数组
System.out.println("请输入一个元素");
Scanner myScanner = new Scanner(System.in);
int insertNum = myScanner.nextInt();
int index = -1;//index就是要插入的位置
//遍历arr数组,如果发现insertNum<=arr[i]说明i就是要插入的位置
//使用index保留index=i
for(int i = 0;i<arr.length;i++){
if(insertNum<=arr[i]){
index = i;
break;//找到位置后退出
}
}
//判断index的值
if(index == -1){//说明还没有找到位置
index = arr.length;
}
//下面将arr的元素拷贝到arrNew并且跳过index位置
for(int i = 0,j = 0;i<arrNew.length;i++){
if(index != i){
arrNew[i] = arr[j];
j++;
}
else{
arrNew[i] = insertNum;
}
// if(index == -1){
// arrNew[arrNew.length-1] = insertNum;
// }
}
//让arr指向arrNew
arr = arrNew;
//打印扩容后的数组
for(int i = 0; i< arrNew.length;i++){
System.out.print(arrNew[i] + "\t");
}
}
}
5.随机生成10个整数(1-100)保存导数组,并倒序打印以及求平均值,求最大值和最大值的下标,并查找里面是否有8
public class Homework06{
public static void main(String[] args){
int[] arr = new int[10];
int max = 0;//定义最大值和最小值和和,这里初始化为arr[0]最好
int min = 100;
int maxIndex = 0;//定义最大值最小值的下标
int minIndex = 0;
double sum = 0.;//这里初始化为arr[0]最好
int temp = -1;
double average = 0.;
for(int i = 0;i<10;i++){//生成数组
int num = (int)(Math.random()*100)+1;
arr[i] = num;
}//顺序打印
for(int i = 0;i<arr.length;i++){
System.out.print(arr[i]+"\t");
}
System.out.println();
for(int i = 0;i< arr.length;i++){//打印信息
System.out.print(arr[arr.length-i-1]+"\t");
sum += arr[i];
if(arr[i]>=max){
max = arr[i];
maxIndex = i;
}
if(arr[i]<=min){
min = arr[i];
minIndex = i;
}
//检查生成的数组里面是否含有8;
if(arr[i] == 8){
temp = 0;
}
}
System.out.println();
average = sum/arr.length;
if(temp == -1){
System.out.println("这里面不含有8");
}else{
System.out.println("里面含有8");
}
System.out.println("平均值为:"+average+"最大值为"+max+"最大值下标为"+maxIndex+"最小值为"+min+"最小值的下标为"+minIndex);
}
}
第六题我认为答案为:aa zz 韩韩 cc
7.写出冒泡排序的代码
import java.util.Scanner;
public class Homework07{
public static void main(String[] args){
int temp = 0;
int count = 0;
int[] arr = {1,4,3,2};
Scanner myScanner = new Scanner(System.in);
while(true){//一直问是否继续添加元素
System.out.println("是否添加?y/n");
char choice = myScanner.next().charAt(0);
if(choice == 'n'){
System.out.println("退出添加");
break;
}
System.out.println("请输入你要添加的元素");
int num = myScanner.nextInt();
int[] arrNew = new int[arr.length+1];
for(int i = 0;i < arr.length;i++){
arrNew[i] = arr[i];
}
arrNew[arrNew.length-1] = num;
arr = arrNew;
}
//冒泡排序
for(int i = 0;i< arr.length-1;i++){
for(int j = 0 ;j< arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
//输出排序后的结果
for(int i = 0;i< arr.length;i++){
System.out.print(arr[i]+"\t");
}
}
}
public class Cat{
public static void main(String[] args){
//使用oop面向对象来解决
//实例化一只猫(创建一只猫对象)
//1.new Cat()创建一只猫
//2.Cat cat1 = new Cat();把创建的猫赋给cat1
Cat cat1 = new Cat();
cat1.name = "小白";
cat1.age = 3;
cat1.color = "white"
//创建第二只猫
Cat cat2 = new Cat();d
cat2.name = "小花";
cat2.age = 100;
cat2.color = "black";
//访问对象属性
System.out.println("第一只猫的信息"+cat1.name+" "+cat1.age + " "+cat1.color);
}
}
//定义一个猫类,(自定义的数据类型)
class Cat{
//属性
String name;//名字
int age;//年龄
String color;//颜色
//行为
}
张老太养了两只猫,一只名字叫做小白,今年3岁,白色,还有一只叫做小花,今年100岁,花色。请编写一个程序,
当用户输入小猫名字时,就显示该猫的名字,年龄,颜色。如果用户输入的小猫名字错误,则显示张老太没有这只猫。
类喝对象的区别和联系
对象在内存中存在形式
Cat cat = new Cat();
//cat是对象名,(对象引用)相当于人的名字
//new Cat()创建的对象空间(数据)才是真正的对象。相当于真正的人。
cat.name = "小白";
cat.age = 12;
cat.color = "red";
看一个思考题 我们定义一个人类(Person)包括名字和年龄 看看下面一段代码
Person p1 = new Person();
p1.age = 10;
p1.name = "小明";
Person p2 = p1;//p1赋给了p2,让p2指向p1
System.out.println(p2.age);
请问p2.age究竟是多少?并画出内存图:
看一个习题,并分析画出内存布局图
Person a = new Person();
a.age = 10;
a.name = "小马";
Person b;
b = a;
System.out.println(b.name);
b.age = 200;
b = null;
System.out.println(a.age);
System.out.println(b.age);
public class Method01{
public static void main(String[] args){
//方法使用
//1.方法写好后,如果不去调用(使用)就不会输出
//2.先创建对象,然后调用方法即可
Person p1 = new Person();
p1.speak();//调用方法
p1.cal01();//调用cal01方法
p1.cal02(5);//调用cal02方法,同时给n = 5
p1.cal02(10);//调用cal02方法,同时给n = 10
int returnRes = p1.getSum(10,20);//调用getSum方法,同时赋值,用returnRes接收
}
}
class Person{
String name;
int age;
//方法(成员方法)
//添加speak 成员方法,输出“我是一个好人”
//1.public 表示方法是公开的
//2.void :代表方法是没有返回值
//3.speak():speak是方法名,()形参列表
//4.{}方法体,可以写我们执行的代码
public void speak(){
System.out.println("我是一个好人");
}
//添加cal01 成员方法,可以计算从1+..+1000的结果
public void cal01(){
//循环完成
int res = 0;
for(int i = 1;i<= 1000;i++){
res += i;
}
System.out.println("计算结果="+res);
}
//添加cal02成员方法,该方法可以接受一个数n,计算1+..+n的结果
//1.(int n)形参列表 表示当前有一个形参,可以接受用户的输入
public void cal02(int n){
//循环完成
int res = 0;
for(int i = 1;i<= n;i++){
res += i;
}
System.out.println("cal02方法 计算结果="+res);
}
//添加getSum成员方法,可以计算两个数的和
//public表示方法是公开的
//int:表示方法在执行后,返回一个值
//getSum 方法名
//(int num1,int num2)形参列表,2个形参,可以接受用户传入的两个数
//return res;表示把res值返回给
public int getSum(int num1,int num2){
int res = num1 + num2;
return res;
}
}
方法调用小节
为什么需要成员方法?看一个需求 遍历一个数组,输出数组的各个元素值。
public class Method02{
//编写一个main方法
public static void main(string[] args){
//遍历一个数组,输出数组的各个元素值
int[][] map = {{0,0,1},{1,1,1},{1,1,3}};
//遍历map数组
//传统的解决方法就是直接遍历,但是代码复用性差,用起来不方便。
for(int i = 0;i< map.length;i++){
for(int j = 0;j<map[i].length;i++){
System.out.print(map[i][j] + " ");
}
System.out.println();
}
//另一个思路:定义一个类MyTools,然后写一个成员方法,调用方法实现,看看效果如何。
//成员方法的好处:代码的复用性更高,可以将实现的细节封装起来,然后供其他用户来调用即可
MyTools tool = new MyTools();
tool.printArray(map);
}
}
//把输出的功能,写到一个类的方法种,然后调用该方法即可
class MyTools{
//方法,接收一个二维数组
public void printArray(int[][] map){
//打印传入的map数组
for(int i = 0;i< map.length;i++){
for(int j = 0;j<map[i].length;i++){
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
}
public 返回数据类型 方法名(参数列表){//方法体 语句; return 返回值; }
//返回两个整数的和与差
public class Test{
public static void main(String[] args){
AA a = new AA();
int[] res = a.getSumAndSub(1,4);
}
}
class AA{
//将和,差返回值保存到数组中
public int[] getSumAndSub(int n1,int n2){
int[] resArr = new int[2];//创建一个数组
resArr[0] = n1 + n2;
resArr[1] = n1 - n2;
return resArr;
}
}
public class MethodDetail02{
public static void main(String[] args){
}
}
class A{
//同一个类的方法调用:直接调用即可
public void print(int n){
System.out.println("print()方法被调用 n="+n);
}
public void sayOk(){//sayOk调用print(直接调用)
print(10);
}
}
public class MethodDetail03{
public static void main(String[] args){
}
}
class A{
//跨类中的方法A类调用B类方法:需要通过对象名调用
public void m1(){
//创建B对象
B b = new B();
b.hi();
}
}
class B{
public void hi(){
System.out.println("B类中的hi()被执行");
}
}
public class MethodExercise01{
public static void main(String[] args){
AA a = new NN();
boolean b = a.odd(1);
}
}
//编写类AA,有一个方法:判断一个数奇偶性
class AA{
public boolean odd(int n){
//可以用三元运算符
return num % 2 != 0 ? true:false;
//也可以直接这样写
return num % 2 != 0;
}
}
2.根据行、列、字符打印对应行数和列数的字符,比如:行:4,列:4,字符#,则打印相应的效果
public class MethodExercise{
public static void main(String[] args){
AA a = new AA();
a.printChar(4,4);
}
}
class AA{
public void printChar(int row ,int column){
for(int i = 0; i< row;i++){
for(int j = 0; j < column;j++){
System.out.print("#");
}
System.out.println();
}
}
}
public class Test{
public static void main(String[] args){
int a = 10;
int b = 20;
//创建AA对象
AA swap = new AA();
swap.swap(a,b);//调用swap
System.out.println("a=" + a + "b=" + b);//a=10 b=20,这里面输出的a,b都是主栈的a,b
}
}
class AA {
public void swap(int a,int b){
System.out.println("\na和b交换的值\na="+a + "\tb=" + b);//a=10 b=20,这里面的a,b都是swap栈里面的a,b
//完成a和b的交换
int tmp = a;
a = b;
b = tmp;
System.out.println("\na和b交换后的值\na=" + a+"\tb="+b);//a=20 b=10 这里面的a,b都是swap栈里面的a,b
}
}
结论:
对于基本数据类型,传递的是值(值拷贝),形参的任何改变不影响实参!
public class Method{
public static void main(String[] args){
//测试
B b = new B();
int[] arr = {1,2,3};
b.test100(arr);//调用方法,arr传递的是地址!!!!!!
System.out.println("tes的arr数组");
//遍历数组
System.out.println("main的数组");
for(int i = 0;i< arr.length;i++){
System.out.print(arr[i] + " ");
}
System.out.println();
//测试
Person p = new Person();
p.name = "jack"
p.age = 20;
b.test200(p);
System.out.println("main的p.age="+p.age);//10000
}
}
class B{
//B类中编写一个方法test100,
//可以接受一个数组,在方法中修改数组,看看原数组是否变化
public void test100(int[] arr){
arr[0] = 200;
//遍历数组
System.out.println("test100的arr数组");
for(int i = 0;i< arr.length;i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
class Person{
String name;
int age;
}
class B{
public void test200(Person p){
p.age = 10000;//修改对象属性
}
}
数组是引用类型,引用类型传递时传递的是地址(传递也是值,但是值是地址),可以通过形参影响实参!
输出结果,发现main栈的数组已经被改变了(形参影响实参),因为main栈和test100栈的arr都指向同一个地址,(arr传递是地址)
test100的arr数组
200 2 3
main的arr数组
200 2 3
如图,在test200中将p置空,主栈中p.age输出什么?
答案仍然是输出10000,而不是异常,原因是,test200中p置空只是让test200里面的p指向空指针
对主栈中的p没有影响
如图,在test200里面重新new一个对象p,那么主栈中输出如何?
答案是,仍然对主栈没有影响,因为test200栈中p指向了新开的一个对象
public class Method{
public static void main(String[] args){
Person p1 = new Person();
p1.name = "milan";
p1.age = 100;
//创建一个对象
MyTools tools = new MyTools();
Person p2 = tools.copyPerson(p);
//p1和p2是Person对象,但是是两个独立的对象,属性相同
System.out.println(p1 == p2);
}
}
class Person{
String name;
int age;
}
class MyTools{
//编写一个方法copyPerson可以复制一个Person对象,返回复制的对象,克隆对象,注意要求得到的
//新对象和原来的对象是两个独立的对象,只是他们的属性相同。
//思路:
//方法的返回类型Person
//方法的名字copyPerson
//方法的形参Person p
//方法体,创建一个新对象,复制属性,返回即可
public Person copyPerson(Person p){
//创建一个新对象
Peron p2 = new Person();
p2.name = p.name;
p2.age = p.age;
return p2;
}
}
//输出什么
public void test(int n){
if(n > 2){
test(n -1);
}
System.out.println("n=" + n);
}
//阶乘
public int factorial(int n){
if(n == 1){
return 1;
}else{
return factorial(n-1)*n;
}
}
本文章使用limfx的vscode插件快速发布