条件语句
java.lang 包中的 Math 类提供了一些用于数学计算的方 Math.random()该方法用于产生 0 到 1 区间的 double 类型的随机数,但是不包括 1。int i = (int) (6 * Math.random()); //产生:[0,5]之间的随机整数
if (条件语句){
}
else if (条件语句){
}
else {
}
switch (case 标签){
case 值1 :
语句;
break ;
case 值1 :
语句;
break ;
default :
语句;
}
public class square {
public static void main ( String [ ] args) {
double R = Math . random ( ) * 6 ;
double area = 3.14 * R * R ;
System . out. println ( area) ;
double circle = 2 * 3.14 * R ;
System . out. println ( circle) ;
if ( area> circle) {
System . out. println ( "面积大于周长" ) ;
}
else if ( area== circle) {
System . out. println ( "面积等于周长" ) ;
}
else {
System . out. println ( "面积小于周长" ) ;
}
}
}
循环语句
break continue if语句 读取键盘输入
import java. util. Scanner ;
public class Salary {
public static void main ( String [ ] args) {
Scanner s = new Scanner ( System . in) ;
double yearSalary;
while ( true ) {
System . out. print ( "请输入你的月薪:" ) ;
double salary = s. nextDouble ( ) ;
System . out. print ( "请输入一年多少薪:" ) ;
int severalMonth = s. nextInt ( ) ;
s. nextLine ( ) ;
System . out. print ( "请输入你的命令:(exti退出 next继续 任意字符继续)" ) ;
String command = s. nextLine ( ) ;
if ( "exti" . equals ( command) ) {
System . out. println ( "结束" ) ;
break ;
}
else if ( "next" . equals ( command) ) {
System . out. println ( "继续" ) ;
continue ;
}
else {
yearSalary = salary* severalMonth;
System . out. println ( "您的年薪是:" + yearSalary) ;
if ( yearSalary> 100000 & yearSalary< 200000 ) {
System . out. println ( "恭喜你年薪超过90%的国人" ) ;
}
else if ( yearSalary>= 200000 ) {
System . out. println ( "恭喜你年薪超过98%的国人" ) ;
}
}
}
}
}
循环嵌套
1. 正着的三角形
2. 倒着的三角形
3. 5*5方阵
4. 方法
public class Work {
public static void nnTable ( ) {
for ( int i= 1 ; i<= 9 ; i++ ) {
for ( int j= 1 ; j<= i; j++ ) {
System . out. print ( "" + i+ "*" + j+ "=" + ( i* j) + " " ) ;
}
System . out. println ( ) ;
}
}
public static void fnnTable ( ) {
for ( int i= 9 ; i> 0 ; i-- ) {
for ( int j= 1 ; j<= i; j++ ) {
System . out. print ( "" + i+ "*" + j+ "=" + ( i* j) + " " ) ;
}
System . out. println ( ) ;
}
}
public static void ffMat ( ) {
int b = 5 ;
for ( int i= 0 ; i<= 5 ; i++ ) {
if ( i% 2 == 0 ) {
for ( b= 5 ; b>= 1 ; b-- ) {
if ( b% 2 == 0 ) {
System . out. print ( "* " ) ;
}
else {
System . out. print ( "# " ) ;
}
}
}
else {
for ( b= 5 ; b>= 1 ; b-- ) {
if ( b% 2 == 0 ) {
System . out. print ( "# " ) ;
}
else {
System . out. print ( "* " ) ;
}
}
}
System . out. println ( ) ;
}
}
public static void main ( String [ ] args) {
int a = 5 ;
while ( ( a-- ) > 0 ) {
System . out. println ( a) ;
}
System . out. println ( "================================================================" ) ;
nnTable ( ) ;
System . out. println ( "================================================================" ) ;
fnnTable ( ) ;
System . out. println ( "================================================================" ) ;
ffMat ( ) ;
}
}
方法
1. **形式参数**:在方法声明时用于接收外界传入的数据
2. **实参**:调用方法时实际传给方法的数据
3. **返回值**:方法在执行完毕后返还给调用它的环境的数据
4. **返回值类型**:事先约定的返回值的数据类型,如无返回值,必须指定为 void。
public class MyWay {
public static void print ( ) {
System . out. println ( "阿聪" ) ;
}
public static int add ( int n1, int n2) {
int sum = n1 + n2;
return sum;
}
public static void main ( String [ ] args) {
int number1 = 10 ;
int number2 = 20 ;
int sum;
sum = add ( number1, number2) ;
System . out. println ( "sum = " + sum) ;
print ( ) ;
}
}
1. 实参的数目、数据类型和次序必须和所调用的方法声明的形式参数列表匹配
2. return 语句终止方法的运行并指定要返回的数据
3. Java 中进行方法调用中传递参数时,遵循值传递的原则( 传递的都是数据的副本) :
Ⅰ基本类型传递的是该数据值的 copy 值。
Ⅱ引用类型传递的是该对象引用的 copy 值,但指向的是同一个对象
方法重载
不同的含义:形参类型、形参个数、形参顺序不同 只有返回值不同不构成方法的重载如:int a(String str){}与 void a(String str){}不构成方法重载 只有形参的名称不同,不构成方法的重载如:int a(String str){}与 int a(String s){}不构成方法重
public class WayReload {
public static void main ( String [ ] args) {
public static int add ( int n1, int n2) {
int sum = n1 + n2;
return sum;
}
public static int add ( int n1, int n2, int n3) {
int sum = n1 + n2 + n3;
return sum;
}
public static double add ( double n1, int n2) {
double sum = n1 + n2;
return sum;
}
public static double add ( int n1, double n2) {
double sum = n1 + n2;
return sum;
}
}
递归
缺点:简单的程序是递归的优点之一。但是递归调用会占用大量的系统堆栈,内存耗用多 ,在递归调用层次多时速度要比循环慢的多,所以在使用递归时要慎重。 阶乘
public class RecFactorial {
static int b= 5 ;
public static int Fac1 ( int d) {
if ( d== 1 ) {
return 1 ;
}
return d* Fac1 ( d- 1 ) ;
}
public static void main ( String [ ] args) {
System . out. println ( Fac1 ( 5 ) ) ;
}
}
public class Fbnacci {
public static int FbncS ( int a) {
if ( a== 1 ) {
return 1 ;
}
else if ( a== 2 ) {
return 1 ;
}
return FbncS ( a- 1 ) + FbncS ( a- 2 ) ;
}
public static void times ( int times) {
for ( int i= 1 ; i<= times; i++ ) {
System . out. println ( FbncS ( i) ) ;
}
}
public static void main ( String [ ] args) {
times ( 10 ) ;
}
}