第四周上机作业

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

package chap1;

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        System.out.println("请输入x的值");
        Scanner s = new Scanner(System.in);
        int x = s.nextInt();
        if (x == 1 || x == 5 || x== 10)
        {
            System.out.println("x=" + x);
        }
        else
        {
            System.out.println("x=none");
        }
    }
}

  第四周上机作业_第1张图片

 

 2.用switch结构实现第1题

package chap1;

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        System.out.println("请输入x的值");
        Scanner s = new Scanner(System.in);
        int x = s.nextInt();
        switch (x)
        {
            case 1:
                System.out.println("x=1");
                break;
            case 5:
                System.out.println("x=5");
                break;
            case 10:
                System.out.println("x=10");
                break;
            default:
                System.out.println("x=none");
        }
    }
}

第四周上机作业_第2张图片

 

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

package chap1;

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        System.out.println("请输入x的值");
        Scanner s = new Scanner(System.in);
        int x = s.nextInt();
        boolean five = false, six = false;
        if (x % 5 == 0)
            five = true;
        if (x % 6 == 0)
            six = true;
        if (five)
        {
            System.out.println("能被5整除");
        }
        if (six)
        {
            System.out.println("能被6整除");
        }
        if (!five && !six)
        {
            System.out.println("不能被5或6整除");
        }
    }
}

第四周上机作业_第3张图片

 

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

package chap1;

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        System.out.println("请输入分数");
        Scanner s = new Scanner(System.in);
        int score = s.nextInt();
        if (score < 0 || score > 100)
        {
            System.out.println("分数无效");
        }
        else
        {
            if (score > 90)
            {
                System.out.println("A");
            }
            else if (score > 80)
            {
                System.out.println("B");
            }
            else if (score > 70)
            {
                System.out.println("C");
            }
            else if (score > 60)
            {
                System.out.println("D");
            }
            else
            {
                System.out.println("E");
            }
        }
    }
}

第四周上机作业_第4张图片

 

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

package chap1;

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        System.out.println("请输入三个数");
        Scanner s = new Scanner(System.in);
        int x = s.nextInt();
        int y = s.nextInt();
        int z = s.nextInt();
        if (x < y && x < z)
        {
            if (y < z)
            {
                System.out.println(x);
                System.out.println(y);
                System.out.println(z);
            }
            else
            {
                System.out.println(x);
                System.out.println(z);
                System.out.println(y);
            }
        }
        else if (y < x && y < z)
        {
            if (x < z)
            {
                System.out.println(y);
                System.out.println(x);
                System.out.println(z);
            }
            else
            {
                System.out.println(y);
                System.out.println(z);
                System.out.println(x);
            }
        }
        else if (z < x && z < y)
        {
            if (x < y)
            {
                System.out.println(z);
                System.out.println(x);
                System.out.println(y);
            }
            else
            {
                System.out.println(z);
                System.out.println(y);
                System.out.println(x);
            }
        }
    }
}

第四周上机作业_第5张图片

 

你可能感兴趣的:(第四周上机作业)