JAVA基础学习笔记 day003_作业(4位数中奖小程序)

import java.util.Scanner;

public class WorkDemo {

	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);
		
		String number = input.nextLine();//输入String类型
		
		int e = (number).length();//计算输入内容字符数量
		
		System.out.println("您输入了" + e + "位数");
		
		int number1 = Integer.parseInt(number);//原输入String类型转换成int类型
		
		//当String类型字符数量为4时,才执行位数求和计算,否则提示错误
		if(e == 4 ){			
			int a = number1 % 10;//个位
			int b = number1 / 10 % 10;//十位
			int c = number1 / 100 % 10;//百位
			int d = number1 / 1000;//千位
			
			int sum = a+b+c+d;
			
			String result = sum > 20 ? "恭喜您中奖!!!":"很抱歉!您未中奖.";
			System.out.println("卡号正确!");
			System.out.println(result);
		}else{
			System.out.println("对不起,卡号错误!请输入正确的四位数卡号!");
			
		}
	}
}

超纲内容:1.输入String类型字符内容

                2.判断String类型字符内容字符数量

                3.将字符数量设定为if结构判断条件

学习内容:1.数据类型及运算符的使用方法

                2.尽可能完善需求

猜你喜欢

转载自blog.csdn.net/qq_42801561/article/details/87920723