Scanner对象和while,do…while,for循环语句的用法附打印三角形案例
基本语法
Scanner scanner = new Scanner(System.in);
//凡是属于IO流的类如果不关闭就会一直占用资源,要养成好习惯用完关闭
scanner.close(); //关闭
next()与nextLing()区别
顺序结构
java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。任何一个算法都离不开的一种基本算法结构。
public static void main(String[] args) {
System.out.println("hello");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
选择结构(通过选择进行判断)
-
if单选结构
//如果布尔表达式为true将执行语句否则false
//equals #判断字符串是否相等
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容:"); String s = scanner.nextLine(); if(s.equals("Hello")){ System.out.println(s); } System.out.println("end");
-
if双选结构
public static void main(String[] args) { //考试分数大于60及格,反之不及格 Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); int score = scanner.nextInt(); if (score>=60){ System.out.println("及格"); }else { System.out.println("不及格"); } scanner.close();
-
if多选结构
如果布尔表达式1为true则执行
如果布尔表达式2为true则执行
如果布尔表达式3为true则执行
如果以上都不是true执行
public static void main(String[] args) { System.out.println("请输入成绩:"); Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); if (score==100){ System.out.println("恭喜满分"); }else if (score<100 && score>=85){ System.out.println("恭喜优秀"); }else if (score<85 && score>=70) { System.out.println("恭喜良好"); }else if (score<70 && score>=60){ System.out.println("及格"); }else if (score<60 && score>=0){ System.out.println("不及格"); }else { System.out.println("数据不合法"); } scanner.close(); }
-
嵌套的if结构
在if语句或else if 语句中使用if或else if
-
switch多选择结构
switch case语句 //判断一个变量与一系列值中某个值是否相等,每个值称为一个分支
switch(expression){ case value : //语句 break;//可选 case value : //语句 break;//可选、 //你可以有任意数量case语句 default://可选 //语句 }
变量类型可以是
int byte char
支持String
标签必须是字符串常量或字面量(1-8,a-d)
public static void main(String[] args) {
//输入具体值
char grade = 'A';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;//可选
default:
System.out.println("错误等级");
}
循环结构
-
while循环
-
do…while循环
-
for循环
java5增强型for循环
whlie循环
whlie结构
while(){
//循环内容
}
//列
public static void main(String[] args) {
//输出1-100
int i = 0;
while (i<100) {
i++;
System.out.println(i);
}
}
//例
public static void main(String[] args) {
//计算1+2+3+...+100
int i = 0;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
5050
只要布尔表达式为true,循环会一直执行
主意:避免死循环,导致程序死机
do…while循环
do{
//代码语句
}while(布尔表达式);
//例
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);
}
while与do…while区别
while先判断后执行,do…while是先执行后判断
do…while总是保证循环体至少执行一次,这是他们的主要差别
for循环
for(初始化; 布尔表达式; 更新){
//初始化 条件判断 迭代
//代码语句
}
for循环可以使循环语句结构变得简单
*for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环语句
for循环执行次数是在执行前就确定的
//练习0-100奇数和和偶数和
package scanner;
public class ForDemo02 {
public static void main(String[] args) {
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if (i%2!=0){ //奇数
oddSum+=i; //oddsum = oddsum + i;
}else {
evenSum+=i;
}
}
System.out.println("奇数运算和为:"+oddSum);
System.out.println("偶数运算和为:"+evenSum);
}
}
print与println区别
print输出完不会换行
println输出完会换行
//九九乘法表练习
package scanner;
public class ForDemo04 {
public static void main(String[] args) {
/**九九乘法表思路
* 1.首先打印第一例
* 2.我们把固定的的1再用一个循环包起来
* 3.去掉重复项 i《=j
* 调整显示
*/
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i) + "\t");
}
System.out.println();
}
}
}
增强for循环
package scanner;
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10,20, 30, 40, 50};
//遍历数组元素
for (int i = 0;i<5; i++){
System.out.println(numbers[i]);
}
System.out.println("=======================");
for (int x:numbers){
System.out.println(x);
}
}
}
break continue
break用于强行退出循环,不执行循环语句中剩余的语句。(switch语句同样实用)
//代码案例
package scanner;
public class BreakDemo01 {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==30){
break;
}
}
System.out.println("请输入数字:");
}
}
continue语句用于循环语句中,用于终止某次循环过程,跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
//代码案例
package scanner;
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;
}
System.out.println(i);
}
}
}
拓展
打印三角形
package scanner;
public class Sjx {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j=5; j>=i; j--){
System.out.print("");
}
for (int j=1; j<=i; j++){
System.out.print("*");
}
for (int j=1; j<i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j=5; j>=i; j–){
System.out.print(“”);
}
for (int j=1; j<=i; j++){
System.out.print(““);
}
for (int j=1; j<i; j++){
System.out.print(””);
}
System.out.println();
}
}
}