java新手笔记3 运算符&循环
1.包

2.运算符
public class Operator {
public static void main(String[] args) {
int a = 5;
System.out.println("a = " + a);
//a = -a; //+ -
System.out.println("a = " + a);
//+ 字符串链接
System.out.println("影分身" + "软件开发");
System.out.println(3 + 5 + "软件开发" + 6 + 8);
double b = a / 2;
System.out.println("a / 2 = " + (a / 2));
System.out.println("b = " + b);
int weith = 10;
int height = 6;
double area = 1.0 / 2 * weith * height;
System.out.println("area = " + area);
int c = a % 3;//取余数
System.out.println("c = " + c);
//int d = c++;// c++ c = c + 1; 先赋值 后自增
int d = ++c;//先自增 后赋值
System.out.println("d = " + d);
System.out.println("c = " + c);
//赋值
int e = 10; //-= *= /= %=
e += 1;// e = e + 1;
System.out.println("e = " + e);
//比较 > >= 5 >= 5 5 > 5 < <= ==
boolean isEquals = (1 == 1);
System.out.println("isEquals = " + isEquals);
int year = 2012;
boolean isLeap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
System.out.println(year +" 是否是闰年 " + isLeap);
System.out.println("1 != 2 ? " + (1 != 2));
//逻辑
boolean isTrue = false;
System.out.println("isTrue ? " + isTrue);
System.out.println("!isTrue ? " + !isTrue);
System.out.println("true || false ? " + (true || false));//或运算
System.out.println("true && false ? " + (true && false));//与运算
int score = 85;
//score > 60 及格 > 70 良 > 85 优秀
// javase > 60 && oracle > 70 && xml > 70
// javase > 60 || oracle > 60 || xml > 60
int move = 16;
System.out.println("move = " + (move >> 3));//100 10
System.out.println("move = " + (move << 1));
}
}
3.选择语句
import java.util.Random;//导入类
public class IfDemo { public static void main(String[] args) {
Random ran = new Random();//new 创建对象
int score = ran.nextInt(101);//ran.nextInt(41) + 60;//随机数 0 ~ 100
System.out.println("score = " + score);
/*
if(score > 60) {//满足条件执行
System.out.println("============");
System.out.println(" 及格了..");
System.out.println("============");
}
*/
//if的第二种形式
/*
if(score > 60) {//满足条件执行
System.out.println("============");
System.out.println(" 及格了..");
System.out.println("============");
} else { //不满足条件执行
System.out.println("============");
System.out.println(" 很遗憾..");
System.out.println("============");
}
*/
//多分支 匹配其中一个条件
/*
if( score >= 90) {
System.out.println("成绩: A");
} else if ( score >= 80 /*&& score < 90) {
System.out.println("成绩: B");
} else if ( score >= 60 /*&& score < 80) {
System.out.println("成绩: C");
} else {
System.out.println("成绩: D");
}
*/ switch(score / 10) {//如果匹配 下面代码执行
case 10: //System.out.println("成绩: A");break;
case 9: System.out.println("成绩: A");break;
case 8: System.out.println("成绩: B");break;
case 7: System.out.println("成绩: C");break;
case 6: System.out.println("成绩: D");break;
default : System.out.println("成绩: 不及格"); }
//三目运算符
boolean isPass = ( score > 60 ? true : false); System.out.println("成绩 === " + isPass); String passed = score > 60 ? "及格" : "挂了"; System.out.println("成绩 ==== " + passed); } }
4.while循环
import java.util.Random;
public class LoopDemo { public static void main(String[] args) { int i = 0;//计数器
//循环 不满足条件不执行 可能执行0次
while( i < 10 ){
System.out.println( i + " Hello World!...");//执行代码
i++;//运行时 i值每次发生改变 i = 10
} System.out.println( "i = " + i ); i = 0; // 0 9 do{//先执行 后判断 至少执行一次
System.out.println( i + " 快乐!...");
i++;
}
while (i < 10); int a = ran.nextInt(101);
System.out.println( "a = " + a );
int i = 2;
while( i < a) {//67
if( a % i == 0) {
break;//跳出循环
}
i++;
} System.out.println( "i = " + i ); if(i >= a)//正常退出循环
System.out.println( a +"是素数");
else
System.out.println( a +"不是素数"); } }
5.for循环
import java.util.Random;
public class LoopDemo2 { public static void main(String[] args) {
int k = 10; //方法中的变量 作用域 在方法中使用
//i 局部变量 for(int i = 0;i < 10 ; i++ ) {//三条语句
System.out.println( i + " Hello World!..."); } //循环嵌套
/*
for(int i = 0; i < 5; i++) {
//k = k + i;
for(int j = 0; j < 3; j++) { System.out.print(" * ");
}
System.out.print("\n");//输出换行
}
*/ for(int i = 1; i < 6; i++) { for(int j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = "+ i * j + " ");
}
System.out.print("\n");
} int sum = 0;
int i;
for(i = 1; i < 200; i++) { sum += i;
if(sum >= 200){
break;
} }
System.out.println("sum = " + sum);
System.out.println("i = " + i);
} }
6.标记循环
import java.util.Random;
public class LoopDemo3 { public static void main(String[] args) { int sum = 0;
int i,k = -1;
loopi: for(i = 1; i < 200; i++) {
for(int j = 1; j < 200; j++) {
sum += j;
if(sum >= 200){
k = j;
break loopi;//指定跳出位置
} }
System.out.println("k = " + k);
System.out.println("i = " + i);
}
System.out.println("sum = " + sum);
System.out.println("k = " + k); for(int a = 1; a < 101; a++ ) { if( a % 3 != 0){//不能被3整除
continue;//结束本次后面代码执行
}
System.out.print( a + "\t");
} } }
7.双循环
public class LoopDemo4 {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i < 11; i++) {//控制的是行
for(int j = 1; j <= i; j++) {//控制的是列
System.out.print(" * ");
/**/
if(j > 5){ //1 2 3 4 5 6 7 8 9 10
break;//跳出内层循环
}
}
System.out.println();
}
}
}
java新手笔记3 运算符&循环的更多相关文章
- Java学习笔记四——运算符
算术运算符 加减乘除(+.-.*./)就不说了. 求余运算符% 描述:第一个操作数除以第二个操作数,得到一个整除的结果后剩下的值就是余数 注意:求余预算的结果不一定总是整数,当操作数是浮点数时,结果可 ...
- Java学习笔记13---一个循环程序的设计范例
package welcome; import java.util.Scanner; /* * 一个循环程序的设计范例 * 首先编写仅执行一次的程序(当无循环时) * 循环的设计步骤: * 1.确定程 ...
- java新手笔记26 Frame
0.Calculater package com.yfs.javase; import java.awt.BorderLayout; import java.awt.Button; import ja ...
- java新手笔记4 数组
1.数组 import java.util.Random; public class ArrayDemo1 { public static void main(String[] args) { int ...
- 【原】Java学习笔记004 - 运算符
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 运算符:对常量 或 ...
- Python学习笔记----数据类型 运算符 循环 条件判断
1. Python安装 在官网www.python.org下载安装程序,可以支持的操作系统linux,windows,mac. Python版本:2.x和3.x,分别有x86和x64. 在Window ...
- JAVA新手笔记 Intent对象和Bundle对象
Intent对象和Bundle对象 功能主要是在 MainActivity中定义了2个EditText,当用户输入内容,把他传入到第二个活动, 自己新创的活动中,MyActivity中 放在MainA ...
- java新手笔记34 连接数据库
1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; i ...
- java新手笔记33 多线程、客户端、服务器
1.Mouse package com.yfs.javase; public class Mouse { private int index = 1; private boolean isLive = ...
随机推荐
- HDU3727 - Jewel(主席树)
题目大意 对一个序列进行以下四种操作: 1.Insert x 在序列尾部插入x 2.Query_1 s t k 查询区间[s,t]的第k小 3.Query_2 x 查询x的在序列中排名 4.Query ...
- tail -f logfile.log 一直监控某个文件,若该文件有改动,立即在屏幕上输出
tail -f logfile.log 可以一直监控某个文件,只要文件有改动,就立即在屏幕上输出
- CentOS的Redis内存分配策略配置
安装了一主两从节点,启动之后发现有一个警告: 大概是说overcommit_memory设置成了0,在低内存环境下后台保存可能会失败,设置成1重启可解决. 然后,不太懂这个配置的含义,google一把 ...
- 【转】Vim 常用命令总结
使用 Vim 的时间不长,但如今已经离不开熟悉的 Vim 编辑模式了. Vim 的学习曲线是非常陡的,一开始学习的时候,面对很多的操作命令要去记住,常常望而却步. 其实,只要记住一些常用的命令,加之在 ...
- 教程-Delphi MSComm 实时串口通讯
Delphi MSComm 实时串口通讯 MSComm控件具有丰富的与串口通信密切相关的属性,提供了对串口进行的多种操作,进而使串行通信变得十分简便.MSComm的控件属性较多,常用的属性如下:1) ...
- mongodb集成spring
1:首先需要下载mongodb的java驱动包 https://github.com/mongodb/mongo-java-driver/downloads 2:需要下载spring集成mongodb ...
- windows批量创建用户
一.建立用户的命令行语法: 建立用户:net user 用户名 密码 /add (如:net user test 123 /add) 提升权限:net localgro ...
- 【转】Spring的WebServiceTemplate访问WebService的方法及其本质原理
WebService客户端调用的本质就是将SAOP格式的XML通过通信协议发送到WebService的服务器端,然后接收服务器端返回的XML. 本文简单介绍一下如何通过Spring提供的WebServ ...
- PowerShell脚本传递参数
在编写PowerShell脚本的时候,可以通过给变量赋值的方法输出想要的结果,但这样的话,需要改动脚本内容.其实也可以在脚本中定义参数,然后再在执行脚本的时候对参数赋值,而无需改动脚本内容. 在Pow ...
- [Javascript] Lodash: Refactoring Simple For Loops (_.find, _.findLast, _.filter)
This lesson shows how to refactor your old loops into using a simpler and more powerful lodash-style ...