本文主要是介绍java实训班作业,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
老师写了一个双色球系统。代码如下:
package com.main;
import java.util.Random;
import java.util.Scanner;
public class SystemTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.存储账号,密码 null
String[][] userArray = new String[100][2];
/*
[["Lily", "123"],
["Jack", "123"],
]
*/
//2.存储彩票结果 0
int[][] dataArray = new int [100][7];
/*
[ [2, 17, 15, 22, 34, 8, 6]
[2, 17, 15, 22, 34, 8, 6]
[2, 17, 15, 22, 34, 8, 6] ]
*/
//3.定义输入对象 scanner
Scanner scanner = new Scanner(System.in);
//4.定义bool类型变量 用于循环系统
boolean isRun = true;
//5.定义用于记录登陆状态的变量
boolean isLogin = false;
//进入彩票系统
while (isRun) {
System.out.println("欢迎进入坑你一次双色球系统");
System.out.println("1.注册");
System.out.println("2.登陆");
System.out.println("3.机选");
System.out.println("4.自选");
System.out.println("5.查看");
System.out.println("6.开奖");
System.out.println("7.退出系统");
System.out.println("输入你的选择:");
int result = scanner.nextInt();
switch (result) {
case 1:
//注册
//1.用户名不能重复
//2.两次输入密码一致
//3.存储用户信息:账号,密码
//存储用户名状态
boolean hasName = true;
//存储用户名
String name = "";
//控制台输入用户名
System.out.println("请输入用户名:");
while(hasName){
name = scanner.next();
//遍历,判断
for(int i = 0; i < userArray.length; i++){
//如果名字重复 就不能注册
if(userArray[i][0] != null && userArray[i][0].equals(name) ){
System.out.println("用户名已存在,请重新输入:");
hasName = true;
break;
}else {
hasName = false;
}
}
}
//判断密码
//定义变量 判断密码是否一致
boolean isEqual = true;
String pwd = null;
while(isEqual){
System.out.println("请输入密码:");
pwd = scanner.next();
System.out.println("请再次输入密码:");
String repeat = scanner.next();
//判断密码
if(pwd.equals(repeat)){
//两次一致
isEqual = false;
}else {
System.out.println("两次输入的密码不一致!");
isEqual = true;
}
}
//----------------------------------
//添加用户信息
for(int i = 0; i < userArray.length; i++){
if(userArray[i][0] == null){
//添加信息
userArray[i][0] = name;
userArray[i][1] = pwd;
System.out.println("注册成功!棒棒哒!");
break;
}
}
break;
case 2:
//登陆
if(isLogin){
System.out.println("已登录!");
break;
}
// 未登录!
System.out.println("请输入用户名:");
String name1 = scanner.next();
System.out.println("请输入密码:");
String pwd1 = scanner.next();
//遍历判断!!!
for(int i = 0; i < userArray.length; i++){
if(userArray[i][0] != null && userArray[i][0].equals(name1) && userArray[i][1].equals(pwd1)){
System.out.println("登陆成功!");
isLogin = true;
break;
}else{
isLogin = false;
}
}
if(isLogin == false){
System.out.println("登陆失败!");
}
break;
case 3:
//机选
if(isLogin == false){
System.out.println("请登陆!");
break;
}
//执行此处!说明已登录!开始机选!
System.out.print("请输入机选的注数:");
int count = scanner.nextInt();
Random random = new Random();
//外层控制行数 也就是彩票的注数
for(int i = 0; i < count; i++){
int num[] = new int[7];
//内层开始随机前6个红球
for(int j = 0; j < 6; j++){
int temp = random.nextInt(33) + 1;
//定义变量存储此球是否重复
boolean isExist = false;
//此循环用来遍历数组!!!!
for(int k = 0; k < 6; k++){
if(temp == num[k]){
isExist = true;
j--;
break;
}
}
if(isExist == false){
//数字不存在 就添加
num[j] = temp;
}
}
//给红球升序排序
for(int m = 0; m < 6 - 1; m++){
for(int n = 0; n < 6 - 1 - m; n++){
if(num[n] > num[n+1]){
int t = num[n];
num[n] = num[n + 1];
num[n + 1] = t;
}
}
}
//添加最后的蓝色球
num[6] = random.nextInt(16) + 1;
//--------------------------------------
//查看结果
System.out.println("机选号码为:");
for(int m = 0; m < 7; m++){
if(num[m] < 10){
System.out.print("0" + num[m] + " ");
}else{
System.out.print( num[m] + " ");
}
}
System.out.println();
//将结果放入二位数组中!!!!
for(int j = 0; j < dataArray.length; j++){
if(dataArray[j][0] == 0) {
for(int k = 0; k < 7; k++){
dataArray[j][k] = num[k];
}
break;
}
}
}
break;
case 4:
//自选
break;
case 5:
//查看
if(isLogin == false){
System.out.println("请登陆!");
break;
}
//输出二位数组!!!
for(int i = 0; i < dataArray.length; i++){
if(dataArray[i][0] == 0){
break;
}
//循环其中的一维数组
for(int j = 0; j < 7; j++){
if(dataArray[i][j] < 10){
System.out.print("0" + dataArray[i][j] + " ");
}else{
System.out.print( dataArray[i][j] + " ");
}
}
System.out.println();
}
break;
case 6:
//开奖
break;
case 7:
//退出登陆
isRun = false;
break;
default:
break;
}
}
}
}
这篇关于java实训班作业的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!