简介
对于有了解,但是了解不深的同学,学习Java总是感觉一看就会,一些就废。往往需要一些实操练习,来夯实我们的学习结果。九九乘法表和计算器都是在编程学习领域比较经典的案例。本文为大家讲解一下两个基础练习涉及到一些基础知识点。
Java基础练习
九九乘法表
public class Ceshi {
    public static void main(String[] args) {
        int y=1 ;
        int x=1;
        int result=0;
        while (x<10){
            while (y<=x){
                result=x*y;
                System.out.print(y +"*"+ x +"=" +result+",");
                y++;
            }
            x++;
            y=1;
            System.out.println("");
        }
    }
}
 
注:
以上九九乘法表的中涉及到使用了一个while 循环的一个案列,
在Java中,有三种主要类型的循环:while循环、do-while循环和for循环。
//while循环:while循环会一直执行代码块,直到给定的条件不再满足为止
    int i = 1;
while (i <= 5) {
        System.out.println(i);
        i++;
        }
//do-while循环:do-while循环与while循环类似,但它会先执行一次代码块,然后再检查条件
int i = 1;
do {
    System.out.println(i);
    i++;
} while (i <= 5);
//for循环:for循环通常用于遍历数组或集合。
int[] array = {1, 2, 3, 4, 5};
for (int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
} 
以上九九乘法表可以在此三种循环中进行互换写法增强对三种循环体的练习。
//while  循环的九九乘法表
public class Ceshi {
    public static void main(String[] args) {
        int y=1 ;
        int x=1;
        int result=0;
        while (x<10){
            while (y<=x){
                result=x*y;
                System.out.print(y +"*"+ x +"=" +result+",");
                y++;
            }
            x++;
            y=1;
            System.out.println("");
        }
    }
}
//do-while  循环的九九乘法表
public class Seshi1 {
    public static void main(String[] args) {
        int y=1;
        int x=1;
        int result=0;
        do {
            do {
                result=x*y;
                System.out.print(y +"*"+ x +"=" +result+",");
                y++;
            } while (y<=x);
            x++;
            y=1;
            System.out.println("");
        } while (x<10);
    }
}
//for 循环的九九乘法表
public class S {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " * " + i + " = " + (i * j) + "\t");
            }
            System.out.println();
        }
    }
}
 
这里在for循环中增加一个\t (制表符)可以使数据对齐。
System.out.print(j + " * " + i + " = " + (i * j) + "\t");
System.out.print(); System.out.println(); 两种打印语法中 第二个是换行打印,也可以在第一个中增加\n 进行换行,可以根据自己的写作系统增加在 System.out.print(); 增加自己所需要的正则表达式。
实现效果

计算器
使用java语法打印一个可以实现 “加、减,乘,除” 的class
//switch  实现示例 
import java.util.Scanner ;
public class jsq {
    public static void main(String[] args) {
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("请输入数字:");
        int num1 = scanner1.nextInt();
        String x = scanner1.next();
        int num2 = scanner1.nextInt();
        int result;
        switch (x) {
            case "*":
                System.out.println(num2*num1);
                break;
            case "+":
                System.out.println(num2+num1);
                break;
            case "-":
                System.out.println(num1-num2);
                break;
            case "/":
                System.out.println(num1/num2);
                break;
            default:
                System.out.println("请重新输入");
        }
    }
}
//if-else 实现示例
 
import java.util.Scanner;
public class jsq {
    public static void main(String[] args) {
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("请输入数字:");
        int num1 = scanner1.nextInt();
        String x = scanner1.next();
        int num2 = scanner1.nextInt();
        int result = 0;
        if (x.equals("*")) {
            result = num2 * num1;
        } else if (x.equals("+")) {
            result = num2 + num1;
        } else if (x.equals("-")) {
            result = num1 - num2;
        } else if (x.equals("/")) {
            result = num1 / num2;
        } else {
            System.out.println("请重新输入");
        }
        System.out.println(result);
    }
} 
注:
在计算器代码的练习中,考察了我们的对象的创建,以及判断表达式的练习
java.util.Scanner 是Java自带的包,主要是键盘输入的相关功能
//创建的固定语法  
Scanner scanner1 = new Scanner(System.in); 
对象名  新建对象名 = new 对象名();
//System.in是一个特殊的对象,它代表了标准输入流。在创建Scanner对象时,你需要提供一个InputStream对象作为参数,System.in就是一个InputStream对象,它提供了从键盘读取数据的功能。次数需要了解对象的使用情况,多数情况是不需要增加特定参数。 
本文还涉及到switch 判断语法:switch语句用于根据表达式的值选择一个代码块来执行。
关于语法讲解,练习时我们可以试着将switch 的语法与if-else 的语法进行互换练习
//switch  语法讲解
 switch (expression) {
    case value1:
        // 执行语句1
        break;
    case value2:
        // 执行语句2
        break;
    ...
    default:
        // 执行默认语句
        break;
}
//在这个示例中,expression是要比较的表达式,value1、value2等是可能的值,case后面的代码块是当expression的值等于value1、value2等时要执行的语句,default后面的代码块是当expression的值不等于任何一个value1、value2等时要执行的语句。break语句用于退出switch语句,否则switch语句会继续执行下一个case后面的代码块。
//if-else  语法讲解
if (expression) {
    // 执行语句1
} else if{
    // 执行语句2
} else {
    // 执行语句3
}
//expression是要比较的表达式,运行逻辑与数据库SQL语言相似。不再详解 
实现效果



















![[架构之路-237]:目标系统 - 纵向分层 - 网络通信 - DNS的递归查询和迭代查询](https://img-blog.csdnimg.cn/f870c591ebdc4c4a96c27aa6db3baf37.png)
