2017.11.20T19_5

package com.xdf;

public class WhileTest01 {

/**
* while循环
*
* while(循环条件){
* 循环体(循环操作)
* }
*
* 特点:
* 先判断条件,如果满足则进入循环体!
*
*/
public static void main(String[] args) {
System.out.println("好好学习,天天向上1!");
System.out.println("好好学习,天天向上2!");
System.out.println("好好学习,天天向上3!");
System.out.println("好好学习,天天向上4!");
System.out.println("好好学习,天天向上5!");
System.out.println("*******************************************");
// 定义一个变量 用来接收循环条件
int num = 1;
while (num <= 100) {
System.out.println("好好学习,天天向上" + num);
num++;
}
System.out.println("程序结束" + num);

}

}

package com.xdf;

import java.util.Scanner;

public class WhileLogin02 {

/**
* 需求:
* 01.获取用户的输入
* 02.和事先定义好的用户名密码进行比较
* 03.用户最多输入错误三次 之后退出系统
*
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userName = "admin";
String password = "123";
System.out.println("请您输入用户名:");
String name = input.next();
System.out.println("请您输入密码:");
String pwd = input.next();
// 定义一个变量 用来接收用户输入错误的次数
int count = 2;
while (count > 0) {
if (name.equals(userName) && pwd.equals(password)) {
System.out.println("登录成功!");
break; // 跳出循环体
} else {
System.out.println("用户名或者密码错误!您还有" + (count - 1) + "次机会");
System.out.println("请您输入用户名:");
name = input.next();
System.out.println("请您输入密码:");
pwd = input.next();
count--; // 输入错误的次数-1
}
}

/**
* 以上代码存在的问题:
* 只有满足循环条件之后才能进入循环操作!
* 获取用户名和密码输入,出现了两次!
*/

}

}

package com.xdf;

import java.util.Scanner;

public class DoWhileTest03 {

/**
*
* do{
* 循环体操作
* }while(循环条件);
*
* 特点:
* 先执行一次循环体操作,之后在进行判断!
*
*/

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userName = "admin";
String password = "123"; // 事先定义用户名和密码
// 定义输入错误的次数
int count = 3;
do {
System.out.println("请您输入用户名:"); // 获取用户的输入
String name = input.next();
System.out.println("请您输入密码:");
String pwd = input.next();
// 判断用户的输入
if (name.equals(userName) && pwd.equals(password)) {
System.out.println("登录成功!");
break;
}
System.out.println("输入错误!您还有" + (count--) + "次机会!");
} while (count > 0);

}
}

package com.xdf;

public class FlagTest04 {

/**
* 标记:
*
* 01.如果说有一个箱子,箱子中装有5本书 (1,2,3,4,A)
* 02.现在需要让我们去 箱子中查找书名为A的书
* 03.按照之前的方式
* int num=0;
while(num<5){
if("A".equals(书名)){
System.out.println("找到了");
break;
}else{
System.out.println("没找到");
}
}
04.上诉代码会输出 4次 没找到! 最终结果是 找到了!
05.不符合我们实际要求
06.使用标记
int num=0;
boolean flag=false;

while(num<5){
if("A".equals(书名)){
flag=true;
break;
}
}
//判断标记
if (flag) {
System.out.println("找到了");
}else{
System.out.println("没找到");
}

* 07.直接显示最终的结果!
*
*/
public static void main(String[] args) {
/**
* 数组???????
*/

}
}

posted @ 2017-11-21 09:26  水墨&丹青  阅读(75)  评论(0编辑  收藏  举报