昨天见一兄弟发的纯手打登录信息加游戏,今天也试着做一个玩玩,搞了一晚自习的,也算给憋出来了…略有不同~开学一周,所学尚浅,不知道IO流学了以后能不能把注册的信息保存在文件里,登录时候在读取~纯手打,纯属娱乐…
import java.util.*;
class xiaoyouxi_1 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
HashMap<String,String> hm=new HashMap<String,String>();//用HashMap存信息
while(true){
sop("1 注册 2 登录 3 退出游戏");
int kaishi=in.nextInt();
if(kaishi==1){
zhuce(hm,in);
}else if(kaishi==2){
if(denglu(hm,in)){
sop("开始游戏吧~");
hehe:while(true){
sop("1 水仙花 2 猜数 3 心形 4 退出" );
int index=in.nextInt();
switch (index) {
case 1:
new YouXi().shuixianhua();//水仙花
break;
case 2:
YouXi.caishu(); //猜数
break;
case 3:
YouXi.xinxing(); //心形
break;
case 4:
break hehe; //退出
}
//选择游戏
}
}else{
sop("请选择:");
continue;
}
}
else if(kaishi==3){
System.exit(1);
}
else{
sop("重新选择:");
}
}
}
public static void zhuce(HashMap<String,String> hm,Scanner in){
//<用户信息>输入账号名和密码,并用HashMap接收,
sop("先注册吧 \n账号:");
String zhanghao=in.next();
if (hm.containsKey(zhanghao)) {
sop("账号已存在,请重新注册:");
}
while(true){
sop("密码:");
String mima=in.next();
sop("确认密码:");
String mima1=in.next();
if(mima.equals(mima1)){
hm.put(zhanghao,mima);
sop("注册成功,请选择:");
break;
}
else {
sop("密码不一致,请重新输密码:");
continue;
}
}
}
public static boolean denglu(HashMap<String,String> hm,Scanner in){
int count=0;
while(true){
sop("输入账号:");
String zhanghao=in.next();
if(hm.containsKey(zhanghao)){
while (true) {
sop("输入密码:");
String mima=in.next();
if(mima.equals(hm.get(zhanghao)))
return true;
else {
sop("密码错误,请重新输入:");
count++;
if(count==3){
sop("输入密码错误三次,重新开始吧~:");
return false;
}
}
}
}
else{
sop("账号不存在!");
return false;
}
}
}
public static void sop(Object a){
System.out.println(a);
}
}
class YouXi {
static Scanner sc=new Scanner(System.in);
public void shuixianhua(){
//Scanner sc=new Scanner(System.in);
System.out.println("输入水仙花数值上限:(如:1000或10000或更大的数)");
long asd=sc.nextInt();
System.out.println("从100到"+asd+"之间的水仙花数是:");
shuixian(asd);
}
public void shuixian(long a){//循环判断水仙花数
int count=0;
for (long i=100;i<a ;i++ ) {
if (fenhe(i)==i) {
System.out.println(i);
count++;
}
}
System.out.println("总个数是"+count);
}
public long fenhe(long a){//n个数字中每个数字n次幂的和
long sum=0;
int geshu1 = geshu(a);
int[] asd=new int[geshu1];
int q=0;
while(a>0){
asd[q]=(int)(a%10);
a/=10;
q++;
}
for(int j=0;j<geshu1;j++){
sum+=mi(asd[j],geshu1);
}
return sum;
}
public int geshu(long a){//输入数字的个数
int count1=0;
long a1=a;
while (a1>0) {
a1/=10;
count1++;
}
return count1;
}
public int mi(int a,int b){ //求幂
int mii=1;
for (int i=0; i<b;i++ ) {
mii*=a;
}
return mii;
}
public static void caishu(){
//产生随机数
int rand=34;
System.out.println("猜个数:");
while (true) {
int nicai=sc.nextInt();
if(nicai>rand){System.out.print("大了~再猜:");}
else if(nicai<rand){System.out.print("小了~再猜:");}
else{
System.out.println("\t\t猜对了~\n\n");
break;
}
}
}
public static void xinxing(){
System.out.println("游戏加载中……");
System.out.println(" ");
System.out.println(" * * * * ");
System.out.println(" * * * * ");
System.out.println(" * * * ");
System.out.println(" * * ");
System.out.println(" * * ");
System.out.println(" * * ");
System.out.println(" * * ");
System.out.println(" * ");
}
} |