JAVA语言学习笔记

数据类型自动转换

public class test{
	public static void main(String[] args) {
	int a=155;
	float b=21.0f;
	System.out.println("a="+a+",b="+b); //输出a,b的值
	System.out.println("a/b="+(a/b)); //输出a/b的值
	}
}

得到显示结果

a=155,b=21.0
a/b=7.3809524
当2个数中有一个为浮点数时,其运算的结果会直接转换为浮点数,当表达式中变量的类型不同时,java会自动将较小的表示范围转换成较大的表示范围后再做运算。

强制类型的转换

public class test{
	public static void main(String[] args) {
	int a=155;
	int b=9;
	float g,h;
	System.out.println("a="+a+",b="+b);//输出a,b的值
	g=a/b;                             //将a/b的结果放在g中
	System.out.println("a/b="+g+"\n"); //输出g的值
	System.out.println("a="+a+",b="+b); //输出a,b的值
	h=(float)a/b;                       //先将a强制转换成float类型后再参加运算
	System.out.println("a/b="+h);       //输出b的值
	System.out.println("(int"h="+(int)h);  //将变量h强制转换成int型

结果如下

a=155,b=9
a/b=17.0
a=155,b=9
a/b=17.222221
(int)h=17

运算练习

public class tes {
	public static void main(String args[]) {
		int x = 3;
		int y = 17;
		boolean yn=true;
		System.out.println(x+y*x--); //X--为下次运算时X值-1再运算
		System.out.println(-x*y+y);
		System.out.println(x<y&&yn);
		System.out.println(x>y||!yn);
		System.out.println(y!=++x?x:y);
		System.out.println(y++/--x);
	}
}

🏥运算符中
x++/x–运算符表示x值+1或x值-1,需要注意的是这个运算符表示循环引用时下一次赋值X自动+1或-1,本次运算中x++或x-- 赋值不变,仍就等于X

if语句应用

public class If{
	public static void main(String[]args) {
		int a=1;
		int b=2;
		if(a>0) {
			if(b>a) {
				System.out.println("a大于0");
				System.out.println("b大于a");
			}
		}
	}
}

if-else语句应用

public class PriceAndAmount{
		public static void main(String[] args) {
			int price=-5;
			int amount=-10;
			if(price>0&&amount>0) {
				int totalCost=price*amount;
				System.out.println(totalCost);
			}else {
				System.out.println("price和amount的值必须都大于0,否则无法计算totalcost");
			}
		}
	}

if-else嵌套

public class CountScrip{
	public static void main(String[] args) {
		int totalCost=350;
		if(totalCost<100) {
			System.out.println("购物金额不足100元,不赠送抵价券。");
		}else if(totalCost<=500) {
				System.out.println("购物金额满100元,赠送55元抵价券。");
			}else {
				System.out.println("购物金额满500元,赠送155元抵价券。");
		}
	}
}

根据总价值自动判断满足何种条件

购物金额满100元,赠送55元抵价券。

while语句应用
斐波那契序列前50项

//while语句应用
public class test{
	public static void main(String[] args) {
		final int MAX=50;
		long i=0,j=1,k=1;
		while(k<=MAX) {
			System.out.print(" "+i+" "+j);
			i=i+j;  //计算斐波那契序列中的下一个数
			j=i+j;  //计算斐波那契序列中的下一个数
			k=k+2;  //用于改变循环的条件表达式的值
		}
		System.out.println();		
	}
}

程序运行结果

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049

进阶,从键盘上输入一个数,判断该数字是否为斐波那契数列中的数

import java.io.*;
public class Fibonacci{
	public static void main(String[] args) {
	int a=0,b=1,n,num;
	String str;
	BufferedReader buf;
	buf=new BufferedReader(new InputStreamReader(System.in));
	System.out.print("请输入一个正整数:");
	str=buf.readLine();     //从键盘上读入字符串赋给变量str
	num=Integer.parseInt(str);  //将str转换成 int类型后赋给num
	while(b<num){
		n=a+b;
		a=b;
		b=n;
		}
		if(num==b)System.out.println(num+"是斐波那契数");
		else System.ut.println(num+"不是斐波那契数");
	}
}

do-while语句

public class SettleAccoutsUsingWhile{
	public static void main(String[] args) {
		int times=5;
		do{                 //dowhile语句程序块
			int price=5;
			int amount=10;
			if(price>0 && amount>0) {
				int totalCost=price*amount;
				System.out.println(totalCost);
			}else {
				System.out.println("price和amount的值必须大于0,否则无法计算totalcost");
			}
			times=times-1;
		}while(times>0);       //do-while语句的条件表达式;
		System.out.println("while语句执行结束。可以休息一下了。");
	}
}

switch语句应用

public class UsingSwitchSmart{
	public static void main(String[] args) {
		int goodsNumber=3;
		switch(goodsNumber) {
		case 1:   //如果goods值为1,则代码从这里开始执行
		case 2:
			System.out.println("此商品属于食品分部");
			break;
		case 3:	
		case 4:
		case 5:
			System.out.println("此商品属于百货分部");
			break;
		default:
			//如果goods值在前面的case语句中没有匹配值,则代码从则例开始执行
			System.out.println("无此商品分类别号");
		}
		System.out.println("switch语句执行结束");
	}
}

根据输入判断

此商品属于百货分部
switch语句执行结束

复合语句练习

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadConsoleInput{
	public static void main(String[] args) throws IOException{
		BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
		int serveTimes=0;
		int totalCost=0;
		int hasMoreGoods=0;
		System.out.println("清输入结账元需要接待的顾客次数:");
		serveTimes=Integer.parseInt(reader.readLine());
		for(int i=0;i<serveTimes;i++) {
			totalCost=0;
			do {
				System.out.println("清输入商品单价:");
				int price=Integer.parseInt(reader.readLine());
				if(price<0) {
					System.out.println("商品单价不能小于0");
					hasMoreGoods=1;
					continue;
				}
				System.out.println("清输入商品数量:");
				int amount=Integer.parseInt(reader.readLine());
				if(amount<0) {
					System.out.println("商品数量不能小于0");
					hasMoreGoods=1;
					continue;
				}
				totalCost=totalCost+price*amount;
				System.out.println("当前总价为:"+totalCost);
				System.out.println("还有商品需要结算么?(输入1为有,其他数字为没有)");
				hasMoreGoods=Integer.parseInt(reader.readLine());
			} while(hasMoreGoods==1);
			System.out.println("本次消费金额为:"+totalCost);
			System.out.println("欢迎光临");
		}
		System.out.println(serveTimes+"次结算已经结束,可以休息");
	}
}
	

输入结账元需要接待的顾客次数:
2
输入商品单价:
100
输入商品数量:
22
当前总价为:2200
还有商品需要结算么?(输入1为有,其他数字为没有)
1
输入商品单价:
12
输入商品数量:
100
当前总价为:3400
还有商品需要结算么?(输入1为有,其他数字为没有)
0
本次消费金额为:3400
欢迎光临
输入商品单价:
1
输入商品数量:
1000
当前总价为:1000
还有商品需要结算么?(输入1为有,其他数字为没有)
0
本次消费金额为:1000
欢迎光临
2次结算已经结束,可以休息

break,default语句应用

import java.util.*;
public class test1{
	public static void main(String[] args) {
		int month,days;
		Scanner reader=new Scanner(System.in);
		System.out.print("请输入月份:");
		month=reader.nextInt();
		switch(month) {
		case 2:days=28;
		break;
		case 4:
		case 6:
		case 9:
		case 11:days=30;
		break;
		default:days=31;
		}
		System.out.println(month+"月份有"+days+"天");
	}	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值