//求素数 

for (int i = 2; i < 10; i++) {//外循环遍历所有数 

boolean flag= true;//用来标志是否为素数,初始化 

for (int j = 2; j < i; j++) {//内循环判断是否为素数,用求余=0则不是,返回false 

if ((i % j) == 0) { 

flag= false; 

break; 

} 

} 

if (flag) {//没直接说明flag是true还是false,表示flag为初始化时的值,即true.故也可写为flag==true(双等号) 

System.out.print(i+"\t"); 

} 

} 

 //方法调用 

// 格式:访问权限修饰符 返回值类型 方法名(参数列表){方法体} 

// main方法是程序的入口方法,首先执行 

public static void main(String[] args) { 

// 创建类的对象test 

Fangfa test = new Fangfa(); 

Scanner scanner = new Scanner(System.in); 

String flag = "y"; 

System.out.println("是否开门?(y/n)"); 

flag = scanner.next(); 

if(flag.equals("y")) { 

test.opendoor();//类的对象test调用开门方法(无参数) 

test.eat(); 

test.sleep(); 

test.eat();//可重复调用某方法 

} 

else { 

test.closedoor();// 类的对象test调用关门方法 

} 

} 



public void opendoor() {// 开门方法,void表示无返回值 

System.out.println("开门"); 

} 



public void closedoor() { 

System.out.println("关门"); 

} 

public void  eat() { 

System.out.println("吃个饭"); 

} 

public void sleep(){ 

System.out.println("睡个觉"); 

} 

 //参数及方法重载 

public static void main(String[] args) { 

String flag="n"; 

do{ 

Scanner scanner = new Scanner(System.in);// 键盘输入 

Canshu canshu = new Canshu(); 



canshu.add(4, 5);// 方法重载可以实现同一个方法多个功能 

canshu.add(4.00f, 5.00f);// 调用带参数的方法add,float类型要加f 

int a = 4;// 也可这样调用 

int b = 5; 

canshu.add(a, b); 



canshu.prinf("你好");// 调用带参数的prinf方法 

canshu.prinf("世界"); 

String str1 = "世界";//也可这样调用 

canshu.prinf(str1); 



canshu.cheng(4.0f, 5.0f); 

canshu.chu(4, 5); 



int data = canshu.sub(4, 5);// 减法,有参带返回值,定义一个值data来接收返回值 

System.out.println(data);// 打印接收的返回值 

 

System.out.println("请输入两个数:");// 键盘输入两个值,调用方法求值。 

float num = scanner.nextFloat(); 

float num1 = scanner.nextFloat(); 

canshu.add(num, num1); 

canshu.chu(num, num1); 

canshu.cheng(num,num1); 

canshu.prinf( "GTMDACE"); 

 

System.out.println("是否继续?(y/n)");//执行一遍程序后,提示是否再次执行一遍 

flag=scanner.next(); 

}while(flag.equals("y"));//do while控制程序是否再次执行,按y再次执行,按n则结束do while循环 

 

if(flag.equals("n")){//输入n时提示程序结束 

System.out.println("程序结束"); 

} 

} 

 

public void add(int x, int y) { 

int res = x + y; 

System.out.println("和为:"+res); 

} 



public void add(float x, float y) {// 方法重载,方法名一样,传入参数不一样 

float res = x + y; 

System.out.println("和为:"+res);//println就是典型的方法重载 

} 



public void prinf(String str) { 

System.out.println(str); 

} 



public void cheng(float x, float y) { 

float res = x * y; 

System.out.println("积为:"+res); 

} 



public void chu(float x, float y) { 

float res = x / y; 

System.out.println("商为:"+res); 

} 



public int sub(int x, int y) {// 有参带返回值,返回类型int 

int res = x - y; 

return res;// 返回值 

} 





 package com.zbj.Arry; 

 //遍历数组 

 public class Arry { 

// 两种数组格式:数据类型[] 数组名 / 数据类型 数组名[] 

public static void main(String[] args) { 

int score = 60; 

int score1 = 80; 

int score2 = 90; 

int score3 = 100; 

int score4 = 250; 

// 声明一个int型数组,静态初始化,声明数组时就把数据放进去 

// int score0[]={60, 80,90,100,250};//也可用下面的方式声明 

int score0[] = { score, score1, score2, score3, score4 }; 



// 动态初始化 

int age[] = new int[5];// 定义一个有5个元素的数组下标从0到4,没有5; 

// System.out.println(age[5]);//没有5,超出数组范围,报错 

System.out.println(age[2]);// 刚定义的未初始化的int数组,默认输出0; 

String str[] = new String[3]; 

System.out.println(str[2]);// 未给String型数组初始化,默认输出null 

age[0] = 18; 

age[1] = 20; 

age[2] = 21; 

age[3] = 22; 

age[4] = 30; 

System.out.println(age[2]);// 初始化后的数组,输出的就是自己定义的值了 



// for循环遍历数组所有元素 

for (int i = 0; i < score0.length; i++) {// length方法可以返回长度,数组名.length 

System.out.print(score0[i]+"\t"); 

} 

System.out.println(); 

// foreach遍历数组所有元素(打出for时alt+/即可调出foreach) 

for (int i : score0) { 

System.out.print(i+"\t");//括号里的是i不是score0[i] 

} 

 

} 



 } 



 //更改及交换数组数据 

 import java.util.Scanner; 

 public class Array2 { 

public static void main(String[] args) { 

// 更改数组数据 

int score[] = { -1, 1, 2, 3, 4 }; 

score[0] = 0; 

for (int j = 0; j < score.length; j++) { 

System.out.print(score[j]+"\t");//-1改成了0 

} 

System.out.println(); 

 

// 交换数组数据 

int temp = score[1]; 

score[1] = score[4]; 

score[4] = temp; 

for (int j = 0; j < score.length; j++) { 

System.out.print(score[j]+"\t");//1与4交换了位置 

} 

System.out.println(); 

 

// 调用方法交换数组数据 

Array2 test = new Array2(); 

test.swap(0, 1, score); 

for (int j = 0; j < score.length; j++) { 

System.out.print(score[j]+"\t");//0与4交换了位置 

} 

System.out.println(); 

 

//键盘输入交换下标 

Scanner scanner=new Scanner(System.in); 

System.out.println("输入交换下标"); 

int index1=scanner.nextInt(); 

int index2=scanner.nextInt(); 

test.swap(index1, index2, score); 

for (int j = 0; j < score.length; j++) { 

System.out.print(score[j]+"\t"); 

} 

System.out.println(); 

} 

 

// 交换数组数据方法 

public void swap(int index1, int index2, int data[]) {// index数组下标 

int temp = data[index1]; 

data[index1] = data[index2]; 

data[index2] = temp; 

} 

 } 



 //输出数组最大最小值 

 public class Array3 { 

public static void main(String[] args) { 

// 输出数组中最大值 

int score[] = { 60, 80, 70, 90, 50 }; 

int max = score[0];// 从数组第一个值开始遍历比较 

for (int i = 0; i < score.length; i++) { 

if (score[i] > max) { 

max = score[i]; 

} 

} 

System.out.println(max); 

//输出最小值 

int min = score[0]; 

for (int i = 0; i < score.length; i++) { 

if (score[i]<min) { 

min=score[i]; 

} 

} 

System.out.println(min); 

//调用寻找最大值方法 

Array3 test=new Array3(); 

test.findMax(score); 

System.out.println(max); 

} 

//把寻找最大值写成一个方法 

public void findMax(int data[]){ 

int max = data[0]; 

for (int i = 0; i < data.length; i++) { 

if(data[i]>max){ 

max=data[i]; 

} 

} 

} 

 } 



 冒泡排序 

 import java.util.Arrays; 

 public class Array4 { 

public static void main(String[] args) { 

int score[] = { 60, 80, 70, 90, 50 }; 

 // 
 Arrays.sort(score);//系统自带的由小到大排序方法  

 // 
 for (int j = 0; j < score.length; j++) 

 //            { System.out.print(score[j]+"\t"); } 
    

// 冒泡排序,由小到大 

for (int i = 0; i < score.length - 1; i++) {// 外循环控制对比趟数,观察得次数为length-1次 

for (int j = 0; j < score.length - 1 - i; j++) {//内层循环,进行两两比对,比对次数随i的增加而减少,一趟的对比次数为length-1-i 

if (score[j] > score[j + 1]) {//前一个数比后一个数大时,交换位置 

int temp = score[j]; 

score[j] = score[j + 1]; 

score[j + 1] = temp; 

} 

} 

} 

//输出数组 

for (int j = 0; j < score.length; j++) { 

System.out.print(score[j] + "\t"); 

} 

System.out.println(); 

//由大到小排序,只需>改成< 

for (int i = 0; i < score.length - 1; i++) {// 外循环控制对比趟数,观察得次数为length-1次 

for (int j = 0; j < score.length - 1 - i; j++) {//内层循环,进行两两比对,比对次数随i的增加而减少,一趟的对比次数为length-1-i 

if (score[j] < score[j + 1]) {//前一个数比后一个数大时,交换位置 

int temp = score[j]; 

score[j] = score[j + 1]; 

score[j + 1] = temp; 

} 

} 

} 

for (int j = 0; j < score.length; j++) { 

System.out.print(score[j] + "\t"); 

} 

} 

 }