Java 第四周上机练习

1..编写程序,输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。(知识点:if条件语句)

public class zxcvbn {

public static void show(int x) {

      if (x == 1 || x == 5 || x == 10) {
System.out.println("x=" + x);
} else {
System.out.println("x=none");
}
}
public static void main(String[] args) {
show(5);
}
}

2..编写程序, 输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。(知识点:switch条件语句)

     

           import java.util.Scanner;
            public class zxcvbn {
            public static void main(String[] args) {
            Scanner in=new Scanner(System.in);
             int a=in.nextInt();
             switch(a){
             case 1:
            case 5:
            case 10:
             System.out.println("x="+a);break;
             default:System.out.println("x=none");
          }
      }
}

3.判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整 除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)

import java.util.Scanner;
public class zxcvbn {
     public static void main(String[] args) {
          System.out.println("***********请输入一个整数*********");
          Scanner scanner = new Scanner(System.in);
          int value = scanner.nextInt();
          i f (value % 5 == 0 && value % 6 == 0) {
          System.out.println("输入的数字" + value + "能被5和6整除");
          } else if (value % 5 == 0) {
          System.out.println("输入的数字" + value + "能被5整除");
          } else if (value % 6 == 0) {
          System.out.println("输入的数字" + value + "能被6整除");
          } else {
          System.out.println("输入的数字不能被5或者6整除");
          }

       }
 }

     

4.输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印 A(90-100),B(80-89),C,D,E(知识点:条件语句if elseif)

import java.util.Scanner;
public class zxcvbn {
public static void main(String[] args) {
int x;
int grade = 0;
Scanner s = new Scanner(System.in);
System.out.print("请输入一个成绩: ");
x = s.nextInt();

if (x > 0 && x <= 100) {//判断成绩是否合法,如果合法,进行比较
grade = x/10;
switch(grade){
case 10:
case 9:System.out.println("等级为A");break;
case 8:System.out.println("等级为B");break;
case 7:System.out.println("等级为C");break;
case 6:System.out.println("等级为D");break;
default:System.out.println("等级为E");break;

}

} else {//判断成绩是否合法,如果非法,进行提示用户
System.out.println("成绩无效" );
}



}
}

5.

import java.util.Scanner;
public class zxcvbn {
public static void main(String[] args) {
         int x,y,z;
         Scanner in = new Scanner(System.in);
         System.out.print("请输入第一个数:");
         x = in.nextInt();
         System.out.print("请输入第二个数:");
         y = in.nextInt();
        System.out.print("请输入第三个数:");
         z = in.nextInt();

         ThreeNumberSort tns = new ThreeNumberSort(x,y,z);
         tns.NumberSorting();
         tns.ShowSortResult();
       }
   }
        class ThreeNumberSort
  {
         private int a,b,c;
         private int temp = 0;
        public ThreeNumberSort(int a,int b, int c)
   {
         this.a = a;
         this.b = b;
          this.c = c;
   }
        public void NumberSorting()
  {
       while(true)
     {
        if(a>b)
{
       temp = a;
       a = b;
      b = temp;
}

       if(b>c)
{
      temp = b;
      b = c;
     c=temp;
  }

if(a<b && b<c)
{
break;
}
}
}
public void ShowSortResult()
{
System.out.println("排序后的顺序为:" + a + ", " +b+", "+ c );
}

}

猜你喜欢

转载自www.cnblogs.com/fanbudufan/p/12573416.html