第4次作业

1.输入商品单价,数量,求总价。活动8折后抹零,计算活动价。
输入付款金额,计算找零并输出

package dsczy;

import java.util.Scanner;

public class Test1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入商品的单价");
		double dj = input.nextDouble();
		System.out.println("请输入商品的数量");
		int sl = input.nextInt();
		double zj = sl * dj;
		System.out.println("总价是" + zj);
		double hdj = zj * 0.8;
		System.out.println("折后价是" + hdj);
		System.out.println("请输入付款金额");
		double fkje = input.nextDouble();
		double zl = fkje - hdj;
		System.out.println("找零是" + zl);
	}

}

  

 

 



2.输入一个三位数,求个位,十位和百位

package dsczy;

import java.util.Scanner;

public class Test2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		System.out.println("请输入一个三位数");
		int a=input.nextInt();
		int gewei=a%10;
		int shiwei=a%100/10;
		int baiwei=a/100%10;
				System.out.println("个位是"+gewei);
		System.out.println("十位是"+shiwei);
		System.out.println("百位是"+baiwei);
	}

}

  

 

 


3.输入大写字母,转成对应的小写字母

package dsczy;

import java.util.Scanner;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		System.out.print("输入大写字母:");
		char ch = scan.next().charAt(0);
		while (ch < 'A' || ch > 'Z') {
			System.out.print("输入错误,请重新输入:");
			ch = scan.next().charAt(0);
		}
		ch = (char) (ch + 32);
		System.out.print("小写字母:" + ch);
	}

}

  

 

 



4.输入2个数,用三目运算符,输出较大数

package dsczy;

import java.util.Scanner;

public class Test3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("输入两个数");
		int a = input.nextInt();
		int b = input.nextInt();
		a = a > b ? a : b;
		System.out.println("最大的数是"+a);
	}

}

  

 5. 输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)

package zy3;

import java.util.Scanner;

public class Text1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个年份");
		int year = input.nextInt();
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
			System.out.println("该年份是闰年");
		} else {
			System.out.println("该年份不是闰年");
		}
	}

}

  

 

 6.输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.

package zy3;

import java.util.Random;
import java.util.Scanner;

public class Text1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入四位四位会员卡号");
		int a = input.nextInt();
		Random r = new Random();
		int b = r.nextInt(10);
		System.out.println("百位随机数是" + b);
		if (a % 10 / 100 == b) {
			System.out.println("您是幸运会员");
		} else {
			System.out.println("您不是幸运会员");
		}

	}

}

  

  

 

 7..已知函数,输入x的值,输出对应的y的值.

           x + 3    ( x > 0 )
y =      0          ( x = 0 )
           x*2 –1  ( x < 0 )

package zy3;

import java.util.Random;
import java.util.Scanner;

public class Text1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入x的值");
		int x = input.nextInt();
		if (x > 0) {
			System.out.println(x + 3);

		} else if (x == 0) {
			System.out.println(0);
		} else {
			System.out.println(x * 2 - 1);
		}
	}

}

  

 

8.输入三个数,判断能否构成三角形(任意两边之和大于第三边)

package zy3;

import java.util.Random;
import java.util.Scanner;

public class Text1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		int a = input.nextInt();
		int b = input.nextInt();
		int c = input.nextInt();
		if (a + b > c && a + c > b && b + c > a) {
			System.out.println("是三角形");
		} else {
			System.out.println("不是三角形");
		}
	}

}

  

 

 

 9. 输入年份月份,输出该月的天数(闰年2月29天,条件参考上机练习1)

package zy3;

import java.util.Random;
import java.util.Scanner;

public class Text1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入年份");
		int year = input.nextInt();
		System.out.println("请输入月份");
		int month = input.nextInt();
		int days = 0;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			System.out.println(month + "月" + "有31天");
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			System.out.println(month + "月" + "有30天");
		case 2:

			if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
				days = 29;
			} else {
				days = 28;

			}
			System.out.println("二月" + "有" + days + "天");
			break;
		default:
			break;
		}
	}

}

  

  

 

 10.给定一个成绩a,使用switch结构求出a的等级

package zy3;

import java.util.Random;
import java.util.Scanner;

public class Text1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入成绩");
		int a = input.nextInt();
		switch (a / 10) {
		case 10:
			System.out.println("A");
			break;
		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;
		}
	}

}

  

 

 11.输入一个数字,判断是一个奇数还是偶数 

package zy3;

import java.util.Scanner;

public class Text2a {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个数字");
		int a = input.nextInt();
		if (a % 2 == 0) {
			System.out.println("这个数是偶数");
		} else {
			System.out.println("这个数是奇数");
		}
	}

}

  

 

 12.编写程序, 判断一个变量x的值,如果是1, 输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。

package zy3;

import java.util.Scanner;

public class Text2a {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个数字");
		int a = input.nextInt();
		if (a == 1) {
			System.out.println("1");
		} else if (a == 5) {
			System.out.println("5");
		} else if (a == 10) {
			System.out.println("10");
		} else
			System.out.println("none");
	}

}

  

 

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

package zy3;

import java.util.Scanner;

public class Text2a {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个数字");
		int a = input.nextInt();
		if (a % 5 == 0 && a % 6 == 0) {
			System.out.println("能被5和6整除");
		} else if (a % 5 == 0 && a % 6 != 0) {
			System.out.println("能被5整除");
		} else if (a % 5 != 0 && a % 6 == 0) {
			System.out.println("能被6整除");
		} else
			System.out.println("不能被5和6整除");
	}

}

  

 

posted @ 2021-03-27 12:04  倪楠  阅读(96)  评论(0编辑  收藏  举报