1.指定考试成绩,判断成绩的等级
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 100;
if(a>100||a<0) {
System.out.println("请输入正确的成绩");
}else if(a>=90&&a<=100) {
System.out.println("优秀");
}else if(a>=80&&a<90) {
System.out.println("好");
}else if(a>=70&&a<80) {
System.out.println("良");
}else if(a>=60&&a<70) {
System.out.println("及格");
}else{
System.out.println("不及格");
}
}
方法利用for循环找出100以内的偶数和:
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 0;
int sum = 0;
for(int i = 1;i<=100;i++) {
if(i%2==0) {
num++;
System.out.println("100内第"+num+"个偶数:"+i);
sum+=i;
}
}
System.out.println("100以内的偶数和是:"+sum);
}
方法练习:1.判断两个数字是否相等
public class DemoMethodSame {
public static void main(String[] args) {
System.out.println(same(15,49));
}
public static boolean same(int a,int b){
return a==b;
}
}
2.求两个数的累加和
public class DemoMethodadd {
public static void main(String[] args) {
System.out.println(add(1,100));
}
public static int add(int a,int b){
int i = 0;
int sum = 0;
for(i=a;i<b;i++){
sum=sum+i;
}
return sum;
}
}
3.打印指定数字的helloworld
public class DemoMethodprint {
public static void main(String[] args) {
printcount(4);
}
public static void printcount(int a){
for(int i=0;i<a;i++){
System.out.println("helloWorld");
}
}
}
4.数组的遍历
int[] array = new int[]{12,13,1,2,5,4};
for (int i1 = 0; i < array.length; i++) {
System.out.println(array[i1]);
}
4.1数组最大值
public class demoArrayMax {
public static void main(String[] args) {
System.out.println(max());
}
public static int max(){
int[] array = new int[]{10,20,200,300,400};
int max = array[0];
for (int i = 0; i < array.length; i++) {
if(array[i]>max)
max=array[i];
}
return max;
}
}
4.2数组元素反转
public class demoarrayParam {
public static void main(String[] args) {
param();
}
public static void param(){
int[] array = new int[]{10,0,30,60,50};
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println("==============================");
for (int min=0,max= array.length-1; min<max; max--,min++) {
int temp = array[max];
array[max]=array[min];
array[min]=temp;
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}