java基础笔记三

关于scanner类

scanner 类util包下的工具类,用于用户输入数据,这里需要导入util包。

public class Test01 {
	public static void main(String[] args) {
	
		//Scanner  接受用户的输入
		 Scanner  sc= new Scanner(System.in);//数据类型为scanner类,将数据存储在sc的变量中
		//给出输入提示
		 System.out.println("请输入你的账号:");
		 String account =sc.next();
	     System.out.println("你的账号是"+account);
		
		System.out.println("请输入商品的价格:");
		double price = sc.nextDouble();
		System.out.println(price);
		

流程控制语句

顺序结构:逐行执行代码,每行必走

判断一个数是否为偶数
int num = 4;
		if(num%2==0) {
		System.out.println("此数是偶数");	
		}else {
		//代码块
		System.out.println("不是偶数");
			
学生成绩的评定
Scanner sc = new Scanner(System.in);
		
		
		System.out.println("请输入您的成绩:");
		int score =   sc.nextInt();
		if(score>=80&&score<=100) {
			System.out.println("优");
		}else if(score>=60&&score<=80){
			System.out.println("优");
		}else {
			System.out.println("...");
		}
超市收银结账
	//收银程序
	//输出商品的单价 price
	//输出商品的数量 count
	//输出商品金额  totalprice
	//顾客付的金额为money 
	//如果商品金额不小于500,则打八折
	//如果付多了,则需找零钱		
	//否则则补钱或者刚好	
	System.out.println("单价");
	double price = sc.nextDouble();
	System.out.println("数量");
	double  count =sc.nextDouble();
	System.out.println("商品金额");
	double money = sc.nextDouble();
	
	double totalprice = price * count;	
	
	if (totalprice>=500){
		totalprice *=0.8;
	} 
	if(money>=totalprice) {
		double change = money-totalprice;
		System.out.println("应付"+money +",需找"+change);
	}else if(money<totalprice) {
		System.out.println("您还需支付"+(totalprice-money));
	}
		
随机数练习
	//Math类:lang包下的类,提供数学计算的相关方法
	//获取1--6之间的随机数
	// 0.0-0.54 0--9  
int i = (int)(6*Math.random()+1);
// int强制转换为整数,6*Math.random()可以使得取值范围在0.0--0.54,从0.0开始所以要加1
	if(i<3) {
		System.out.println("小了");
	}
	Math.random();//random随机数,0.0--1.0之间不包括1.0的所有浮点型随机数

分支结构:有条件的执行,并非每句都执行

public class Test04 {
	public static void main(String[] args) {
	int num = 2;
	switch (num) {//key值跟case的value值匹配
	//byte short long int char string 
	case 1 ://相当于if(条件)
		System.out.println("11111111");
	 	break;//跳出当前case块
	case 2 ://else if(条件)
		System.out.println("2222222222");
	 	break;
	default://所有case未匹配到时执行的内容
		System.out.println("11122222222");
	 	break; 		
	}

####用户输入数据

```java
//让用户输入指令
//请选择如下功能:1.显示全部记录2.查询登录记录
//0.退出
	
	Scanner sc = new Scanner(System.in);
	System.out.println("请选择如下功能:1.显示全部记录2.查询登录记录0.退出");
	int command = sc.nextInt();
	switch (command) {
	case 1:
		System.out.println("显示全部记录");
		break;
	case 2:
		System.out.println("查询登录记录");
		break;	
	case 0:
		System.out.println("退出");
		break;
	default:
		System.out.println("错误");
		break;
	}

循环结构:有条件的反复执行,良性的,反复执行(如果…则继续)

while循环
public class Test05 {
	public static void main(String[] args) {
		//循环条件:<11
		//循环初始值:=0
		//循环发展方向:i++
	int i = 0;	
	while(i<10){
		System.out.println("行动是成功的阶梯");
		i++;
	}
	}
	}	
do while 循环
public class Test06 {
	public static void main(String[] args) {	
		//猜数字游戏
		//scanner类接收数据
		Scanner console = new Scanner(System.in);
		//Random类生成随机数
		//生成1--100之间的随机整数
		int num = (int)(Math.random()*100+1);
		//给用户提示
		System.out.println("猜吧");
		//开始猜数
		//定义用户输入的数据
		int guess;
		do { 		
		//接受用户输入的整数
			guess = console.nextInt();
			if(guess>num) {
				System.out.println("大了");
			}if(guess<num) {
					System.out.println("小了");	
			}if(guess==num) {
						System.out.println("相等");
			}
					
			}
		while (guess!=num);//猜测数据和随机数不相等		
}
		}

	
for循环

for循环是计次循环,固定此数循环结构,万能循环

  1. 循环变量初始值(在循环中反复被改变的那个数)
  2. 循环条件(以循环变量为基础)
  3. 循环方向(向着循环结束改变的)
  4. 循环体
    语法: for(变量的初始化值;条件;变量改变的方向){
    循环体;
    }
典型案例 九九乘法表
public class Test09 {
	public static void main(String[] args) {
		/*
		 * 输出1--9之间所有整数和9相乘的所有整数内容 循环变量:第一个乘数,int i = 1; 循环条件:i<=9
		 * 
		 */
		for (int i = 1; i < 10; i++) {
			for (int j = 1; j < i; j++) {
				System.out.print(j + "*" + i + "=" + i * j + "\t");// \t为占位符
			}
			System.out.println();
		}
	}
}
典型案例
//----------------   ----------------案例要求-------------------------------------------------------------	
/*
 * 随机加法的运算器 1.计算机随机生成两个1--100以内的整数作为两加数。 2.进行10次加法运算 3.用户输入运算后的答案(答题过程)
 * 4.判断用户的答案是否正确answer 5.如正确则加十分否则提示答错了 6.最终输出所有得分 score
 */

//------------------------------------代码展示----------------------------------------------	

public class Test10 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请猜数:");
		int score = 0;
		for (int i = 1; i < 11; i++) {
			int a = (int) (Math.random() * 100 + 1);
			int b = (int) (Math.random() * 100 + 1);
			System.out.println("("+1+")"+""+a+"+"+b+"=");
			int result = a+b;
			int answer = sc.nextInt();
			if (result==answer) {
				score +=10;	
			}
				System.out.println("得分是  "+score );
			}

		}
	}


break 和 continue
break的作用是终止代码的运行
public class Test11 {
	public static void main(String[] args) {
	int sum = 0;
		for(int i =0;i<101;i++) {
			  sum+=i;
			if(sum>4000) {
				System.out.println(i);
				break;
			}
		}
		System.out.println(sum);
continue的用途:跳过循环体中的剩余语句而执行下一循环

循环跳过100以内所有7的倍数和以7为结尾的数

for(int i = 1;i<=100;i++) {
			if(i%7==0||i%10==7) {
				continue;
			}
			System.out.println(i);
		}
		
		
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值