Java流程控制
1、Scanner
public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("使用next方式接收:");
if(sc.hasNext()){
String str = sc.next();
System.out.println("输出的内容为" + str);
}else{
System.out.println("输入的不是整数");
}
if(sc.hasNextFloat()){
float str = sc.nextFloat();
System.out.println("输出的内容为" + str);
}else{
System.out.println("输入的不是小数");
}
sc.close();
}
}
应用案例:简易计算器
package com.qiu.method;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a, b;
String c, flag;
while (true) {
a = enterLeftNum();
c = enterOperator();
b = enterRightNum();
calcu(a, b, c);
System.out.println("继续吗?继续Y,任意键退出");
flag = sc.next();
if (flag == "Y") {
continue;
} else {
break;
}
}
System.out.println("End");
sc.close();
}
public static double enterLeftNum() {
double right;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入 Dog 运算符 B 的A: ");
if (sc.hasNextLine()) {
if (sc.hasNextDouble()) {
right = sc.nextDouble();
return right;
} else {
System.out.println("输入错误,请重新输入!");
sc.next();
}
}
}
}
public static String enterOperator() {
Scanner sc = new Scanner(System.in);
String s;
while (true) {
System.out.println("请输入 Dog 运算符 B 的运算符: ");
s = sc.nextLine();
if (!s.equals("+") && !s.equals("-") && !s.equals("*") && !s.equals("/")) {
System.out.println("输入错误,请输入+ - * /中的一项!");
continue;
}
return s;
}
}
public static double enterRightNum() {
Scanner sc = new Scanner(System.in);
double right;
while (true) {
System.out.println("请输入 Dog 运算符 B 的B: ");
if (sc.hasNextDouble()) {
right = sc.nextDouble();
return right;
} else {
System.out.println("输入错误,请重新输入!");
sc.next();
}
}
}
public static void calcu(double a, double b, String c) {
switch (c) {
case "+":
add(a, b);
break;
case "-":
sub(a, b);
break;
case "*":
multi(a, b);
break;
case "/":
div(a, b);
break;
default:
System.out.println("运算符输入错误!");
break;
}
}
public static void add(double a, double b) {
System.out.println(a + b);
}
public static void sub(double a, double b) {
System.out.println(a - b);
}
public static void multi(double a, double b) {
System.out.println(a * b);
}
public static void div(double a, double b) {
if (b == 0) {
System.out.println("除数不能为0!");
return;
}
System.out.println(a / b);
}
}
2、循环
增强型for循环
public class AdvanceFor {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for(int x:numbers){
System.out.println(x);
}
}
}
3、选择
Switch
public class SwitchDemo01 {
public static void main(String[] args) {
String s = "345";
switch (s) {
case "123":
System.out.println("111");
break;
case "345":
System.out.print("123");
default:
System.out.println("null");
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!