练手——Java又小又简单的抽奖小项目

/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:李晓凯
 * 完成日期:2019年 4 月 28 日
 * 版 本 号:v1.0
 *
 * 问题描述:
 * 输入描述:
 * 程序输出:

*/
       太无聊,就是写着练手玩,哈哈,一个又小又简单的抽奖小程序,可以注册,登录,抽奖(要在登录之后才可以)。

代码:

package project;

import java.util.*;

public class Homeworktest {
	static Boolean STATUE = false; // 状态,false-未登录 true-已登陆
	static String user; // 账号
	static String password; // 密码

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("**************");
		System.out.println("1.注册\n2.登录\n3.抽奖\n0.退出");
		System.out.println("**************");
		int key;
		while (true) {
			System.out.println("请输入关键字:");
			key = input.nextInt();
			if (key == 0) {
				System.out.println("感谢您的使用,欢迎下次光临!");
				break;
			} else if (key == 1) {
				register();	//注册
			} else if (key == 2) {
				login();	//登录
			} else if (key == 3) {
				lucky();	//抽奖
			}
		}
	}

	// 抽奖
	private static void lucky() {
		// TODO Auto-generated method stub
		if(!STATUE) {
			System.out.println("请先登录!");
		}else {
			Scanner input = new Scanner(System.in);
			int[] myscore = new int[3];	//自选抽奖号码
			int[] score = new int[3];	//本期中奖号码
			int count = 0;   //记录中奖的号码个数
			Random random = new Random();
			System.out.println("请输入3个0~9的抽奖号码:");
			for(int i = 0 ; i < 3 ; i++) {
				myscore[i] = input.nextInt();
				score[i] = random.nextInt(10);
			}
			for(int i = 0 ; i < 3 ; i++) {
				if(myscore[i] == score[i])
					count ++;
			}
			System.out.println("本期中奖号码为:\n" + 
					score[0] + " " + score[1] + " " + score[2]);
			if(count == 3)
				System.out.println("一等奖呀!你真是太牛X了");
			else if(count == 2)
				System.out.println("二等奖!撒花,撒花!");
			else if(count == 1)
				System.out.println("三等奖!蚊子肉也是肉啊!");
			else if(count == 0)
				System.out.println("运气攒着以后中大的!");
		}
	}

	// 登录
	private static void login() {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		while (true) {
			System.out.println("请输入账号:");
			String lUser = input.nextLine();
			if (!lUser.equals(user)) {
				System.out.println("该账号不存在!");
				break;
			} else {
				System.out.println("请输入密码:");
				String lPassword = input.nextLine();
				if (!lPassword.equals(password)) {
					System.out.println("密码错误!");
					continue;
				} else {
					System.out.println("登录成功!");
					STATUE = true;	//改为登录状态
					break;
				}
			}
		}
	}

	// 注册
	private static void register() {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		String confirm_password;
		System.out.println("请输入账号:");
		user = input.nextLine();
		System.out.println("请输入密码:");
		password = input.nextLine();
		System.out.println("请确认密码:");
		for (int i = 4; i > 0; i--) {
			confirm_password = input.nextLine();
			if (confirm_password.equals(password)) {
				System.out.println("注册成功!\n您的账号是:" + user + " ,密码是:" + password);
				break;
			} else if (i > 1) {
				System.out.println("输入错误,您还有" + (i - 1) + "次机会!");
			} else {
				System.out.println("注册失败,请重启系统!");
			}
		}

	}
}

运行截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值