最容易做的网站类型,网站广告图片在线制作,免费做流程图的网站,软件开发培训班价格简介  
对于有了解#xff0c;但是了解不深的同学#xff0c;学习Java总是感觉一看就会#xff0c;一些就废。往往需要一些实操练习#xff0c;来夯实我们的学习结果。九九乘法表和计算器都是在编程学习领域比较经典的案例。本文为大家讲解一下两个基础练习涉及到一些基础知…简介  
对于有了解但是了解不深的同学学习Java总是感觉一看就会一些就废。往往需要一些实操练习来夯实我们的学习结果。九九乘法表和计算器都是在编程学习领域比较经典的案例。本文为大家讲解一下两个基础练习涉及到一些基础知识点。 Java基础练习 
九九乘法表 
public class Ceshi {public static void main(String[] args) {int y1 ;int x1;int result0;while (x10){while (yx){resultx*y;System.out.print(y * x  result,);y;}x;y1;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 y1 ;int x1;int result0;while (x10){while (yx){resultx*y;System.out.print(y * x  result,);y;}x;y1;System.out.println();}}
}//do-while  循环的九九乘法表public class Seshi1 {public static void main(String[] args) {int y1;int x1;int result0;do {do {resultx*y;System.out.print(y * x  result,);y;} while (yx);x;y1;System.out.println();} while (x10);}
}//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(num2num1);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:// 执行语句1break;case value2:// 执行语句2break;...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语言相似。不再详解 
实现效果