java开发入门学习五-流程控制
流程控制语句
if, if...else, if..else if..else
与前端相同 略
switch case
与前端不同的是case不能使用表达式,使用表达式会报错
class TestSwitch {
public static void main(String[] args) {
// switch 表达式只能是特定的数据类型: byte short char int 枚举(JDK5.0新增) String(JDK8.0新增)
// case 后跟的场景,使用表达式与这些常量做相等的判断,不行
// break 与前端相同,break不写会执行所有语句
int number = 10;
switch(number) {
case 10:
System.out.println("10");
break;
default:
System.out.println("default");
break;
}
char c1 = '你';
switch(c1) {
case '你':
System.out.println("你");
break;
case '我':
System.out.println("我");
break;
default:
System.out.println("default");
break;
}
switch(number) {
case number > 10: // 错误
System.out.println("大于10");
break;
default:
System.out.println("default");
break;
}
}
}
if和switch的对比
if: 条件是一个布尔类型的值,if表达式可以用于范围判断,也可以用于等值判断,范围更广
switch语句条件是一个常量,使用范围更狭窄,但switch更具有穿透性
for,while, do while
与前端的语法都是一致的,break跳出当个for循环;continue跳出当次循环
class TestFor {
public static void main(String[] args) {
int sum = 0, count = 0;
for(int i = 1; i <= 100; i++) {
if(i % 2 == 0) {
sum += i;
count += 1;
}
}
System.out.println("输入出"+ count + "输出" + sum);
}
}
相关测试案例
键盘输入功能case
// 引入包
import java.util.Scanner;
public class TestCase {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入性别:男\\女");
// 获取字符串第一个字段
String gender = scanner.next();
System.out.println("请输入年龄");
// 获取字符串第一个字段
int age = scanner.nextInt();
System.out.println("请输入体重:");
double weight = scanner.nextDouble();
System.out.println("请输入是否单身:单身true;不单身 false");
boolean isSingle = scanner.nextBoolean();
// == 用于比较两个变量是否引用同一个对象(内存存储地址)
if(gender == "男") {
System.out.println("男");
}
// 比较字符串值是否相等
if(gender.equals("男")) {
System.out.println("男1");
}
}
}
随机数
class RandomNum {
public static void main(String[] args) {
// 会返回[0.0,1.0)范围的double类型的随机数
double d1 = Math.random();
// 获取[0, 100]范围的随机整数
int i1 = (int)(Math.random() * 101); // [0, 100)
// 获取一个[1,100]范围的随机整数
int i2 = ((int)(Math.random() * 100)) + 1; // [0, 100)
// 获取一个[a,b] 范围的随机整数
// (int)(Math.random() * (b-a+1)) + a
}
}
输出*
class TestFor1 {
public static void main(String[] args) {
int maxStar = 5;
for(int i = 1; i <= 3; i++) {
String str = "";
// *的个数
int num = (i * 2) - 1;
// 空格数
int space = (maxStar - num) / 2;
for(int j = 1; j <= maxStar; j++) {
if(j <= space || j > space + num) {
str += "-";
}
if(j > space && j <= space + num) {
str += "*";
}
}
System.out.println(str);
}
}
}
输出99乘法表
class Testfor2 {
public static void main(String[] args) {
for(int i = 1; i <= 9; i++) {
String expression = "";
for(int j = 1; j <= i; j++) {
expression += i + "*" + j + "=" + (i * j) + "\t";
}
System.out.println(expression);
}
}
}
找出100内所有质数
class Testfor3 {
public static void main(String[] args) {
int count = 0;
for(int i = 2; i <= 100; i++) {
boolean isNum = false;
for(int j = 2; j < i; j++) {
if(i%j == 0) {
isNum = true;
break;
}
}
if(!isNum) {
count += 1;
System.out.println(i);
}
}
System.out.println("总数:" + count);
}
}
优化算法
class Testfor4 {
public static void main(String[] args) {
int count = 0;
for(int i = 2; i <= 100; i++) {
boolean isNum = false;
// 小于i的平方根内计算,优化算法
for (int i = 2; i <= Math.sqrt(i); i++) {
if (i % j == 0) {
isNum = true;
}
}
if(!isNum) {
count += 1;
System.out.println(i);
}
}
System.out.println("总数:" + count);
}
}
记账本
通过键盘输入输出,写一个简单的记账本
public class AccountSoft {
public static void main(String[] args) {
boolean isFlag = true;
int initMoney = 1000;
String info = "";
while(isFlag) {
System.out.println("----记账软件----");
System.out.println("1 收支明细");
System.out.println("2 登记收入");
System.out.println("3 登记支出");
System.out.println("4 退出");
System.out.println("请选择(1-4):");
char select = Utility.readMenuSelection();
switch(select) {
case '1':
System.out.println("----收支明细---");
System.out.println("余额\t收支\t收支说明");
System.out.println(info);
break;
case '2':
System.out.println("----登记收入----");
int money1 = Utility.readNumber();
if(money1 > 0){
initMoney += money1;
}
System.out.println("请输入说明");
String addDesc = Utility.readString();
info += initMoney + "\t" + money1 + "\t" + addDesc + "\n";
break;
case '3':
System.out.println("----登记支出----");
int money2 = Utility.readNumber();
if(money2 > 0 && initMoney >= money2){
initMoney -= money2;
}
System.out.println("请输入说明");
String minusDesc = Utility.readString();
info += initMoney + "\t" + money2 + "\t" + minusDesc + "\n";
break;
default:
System.out.println("请确认是否退出(Y/N)");
char isExit = Utility.readConfirmSelection();
if(isExit == 'Y') {
isFlag = false;
}
break;
}
}
}
}
Utility
import java.util.Scanner;
public class Utility {
public static Scanner scanner = new Scanner(System.in);
// char为返回格式
public static char readMenuSelection() {
char c;
for(;;) {
String str = readKeyBoard(1);
c = str.charAt(0);
if(c != '1' && c != '2' && c!='3' && c != '4') {
System.out.print("选择错误请重新输入:");
}else {
break;
}
}
return c;
}
public static int readNumber() {
int n;
for (; ; ) {
String str = readKeyBoard(4);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("请输入数值");
}
}
return n;
}
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("请输入正确选项");
}
}
return c;
}
public static String readString() {
String str = readKeyBoard(8);
return str;
}
public static String readKeyBoard(int limit) {
String line = "";
while(scanner.hasNext()) {
line = scanner.nextLine();
if(line.length() < 1 || line.length () > limit) {
System.out.print("输出超过限制");
continue;
}
break;
}
return line;
}
}