课堂练习1

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

package calsswork;
import java.util.Scanner;
public class text1 {
    public static void main(String[] args) {
        System.out.println("请输入a的值");
        Scanner input =new Scanner(System.in);
        int a=input.nextInt();
        if(a==1){
        System.out.println("a="+a);
        }else if(a==5){
            System.out.println("a="+a);
        }else if(a==10) {
            System.out.println("a="+a);
        }else {
            System.out.println("a=none");
        }
    }
}

 

 2.用switch结构实现第1题

package calsswork;
import java.util.Scanner;
public class text1 {
    public static void main(String[] args) {
        System.out.println("请输入a的值");
        Scanner input =new Scanner(System.in);
        int a=input.nextInt();
        switch(a){
        case 1:
            System.out.println("a=1");
            break;
        case 5:
            System.out.println("a=5");
            break;
        case 10:
            System.out.println("a=10");
            break;
        default:
            System.out.println("a=none");
        }
    }

}

 

 

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

package calsswork;
import java.util.Scanner;
public class text1 {
    public static void main(String[] args) {
        System.out.println("输入a");
        Scanner input =new Scanner(System.in);
        int a=input.nextInt();
        if(a%5==0&&a%6==0) {
             System.out.println("同时被5和6整除");
            }else if(a%5==0) {
             System.out.println("被5整除");
            }else if(a%6==0) {
             System.out.println("被6整除");
            }else {
             System.out.println("不能被5和6整除");
            }
           }

          }

 

 

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

package calsswork;
import java.util.Scanner;
public class text1 {
    public static void main(String[] args) {
        System.out.println("请输入一个0-100的数");
        Scanner input =new Scanner(System.in);
        double x = input.nextDouble();
        if(x>100||x<0) {
         System.out.println("分数无效");
        }else if(x>=90&&x<=100) {
         System.out.println("A");
        }else if(x>=80&&x<=89) {
         System.out.println("B");
        }else if(x>=80&&x<=79) {
         System.out.println("C");
        }else if(x>=70&&x<=69) {
         System.out.println("D");
        }else {
         System.out.println("E");
        }
    }
}

 

 

5.输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句)

package calsswork;
import java.util.Scanner;
public class text1 {
    public static void main(String[] args) {
        System.out.println("请输入三个整数,由小到大排列");
        Scanner input=new Scanner(System.in);
        int a=input.nextInt();
        int b=input.nextInt();
        int c=input.nextInt();
        int max,middle,min;
        max = a;
        if(b > a && b > c ){
        max = b;
        } 
        if(c > a && c > b){
        max = c;
        }
        min = a;
        if(b < a && b < c){
        min = b;
        }
        if(c < a && c < b){
        min = c;
        }
        middle = (a + b + c) - (max + min);
        System.out.println(min + ","+ middle + "," + max);
       }
      }

 

posted @ 2020-03-26 12:26  纪明  阅读(216)  评论(0编辑  收藏  举报