2/21-3/1

商城登录页面

完善了一下,注册下密码核对部分

  1. package com.woniuxu.day03;
  2. import java.util.Scanner;
  3. public class Demo1 {
  4. public static void main(String [] args){
  5. Scanner scanner = new Scanner(System.in);
  6. //1.模拟一个登录注册退出的功能实现
  7. loadUI();
  8. //2.使用switch……case实现
  9. String option = scanner.nextLine();
  10. switch(option){
  11. case "1":
  12. loginUI();//调用外部代码
  13. mainUI();
  14. break;
  15. case "2":
  16. registerUI();
  17. break;
  18. case "0":
  19. System.out.println("正在推出程序,请稍后……");
  20. System.exit(5);
  21. break;
  22. default:
  23. System.out.println("您的输入有误");
  24. }
  25. scanner.close();
  26. }
  27. /**
  28. * 开机ui
  29. */
  30. public static void loadUI(){
  31. System.out.println("*******欢迎登陆*******");
  32. System.out.println("\t1.登录");
  33. System.out.println("\t2.注册");
  34. System.out.println("\t0.退出");
  35. System.out.println("———————————————————");
  36. }
  37. /**
  38. * 1.登录验证的方法
  39. */
  40. public static void loginUI(){
  41. Scanner scanner = new Scanner(System.in);
  42. System.out.println("---欢迎进入商城登陆系统---");
  43. while(true){
  44. System.out.println("请输入用户名");
  45. String username = scanner.nextLine();
  46. System.out.println("请输入密码");
  47. String password = scanner.nextLine();
  48. //进行登录逻辑验证
  49. if("admin".equals(username) && "123".equals(password)){
  50. break;
  51. }else{
  52. System.out.println("您的输入有误,请重新输入");
  53. }
  54. }
  55. System.out.println("登录成功");
  56. scanner.close();
  57. }
  58. /**
  59. * 2.注册验证的方法
  60. */
  61. public static void registerUI(){
  62. Scanner scanner = new Scanner(System.in);
  63. System.out.println("欢迎进入商城注册页面");
  64. System.out.println("请输入您的用户名:");
  65. String username = scanner.nextLine();
  66. String psw;
  67. String psw1;
  68. while(true){
  69. System.out.println("请输入您的密码:");
  70. psw = scanner.nextLine();
  71. System.out.println("请再次输入您的密码");
  72. psw1 = scanner.nextLine();
  73. if(psw.equals(psw1)){
  74. break;
  75. }else{
  76. System.out.println("您俩次输入的密码不一致,请重新输入");
  77. }
  78. }
  79. System.out.println("您已注册成功");
  80. System.out.println("您的用户名为:"+username+"您的密码为:"+ psw);
  81. scanner.close();
  82. }
  83. /**
  84. * 商城主页面
  85. */
  86. public static void mainUI(){
  87. System.out.println("欢迎进入商城页面");
  88. }
  89. }

输入年月日,计算这是此年第几天

  1. package com.woniuxy.demo_if;
  2. import java.util.Scanner;
  3. import java.util.InputMismatchException;
  4. public class Test4 {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7. int year = 0;
  8. System.out.println("请您输入一个年份:");
  9. while(true){
  10. try{
  11. year = scanner.nextInt();
  12. break;
  13. }catch(InputMismatchException e){
  14. System.out.println("您输入的格式有误请重新输入");
  15. scanner.next();//两种理解①将nextInt中的错误输出清除②next清空缓存输入新的内容作为下一次的nextInt的输入
  16. }
  17. }
  18. boolean flag = isLeapYear(year);//默认非闰年
  19. int month = 0;
  20. System.out.println("请输入一个月份:");
  21. while(true){
  22. try{
  23. month = scanner.nextInt();
  24. if(month < 0 || month >12) {
  25. System.out.println("您输入的月份有误,请重新输入:");
  26. continue;
  27. }
  28. break;
  29. }catch(InputMismatchException e){
  30. System.out.println("您输入的格式有误请重新输入");
  31. scanner.next();
  32. }
  33. }
  34. int day = 0;
  35. System.out.println("请输入一个日期:");
  36. while(true){
  37. try{
  38. day = scanner.nextInt();
  39. if(day < 0) {
  40. System.out.println("日期必须时正,请重新输入");
  41. continue;
  42. }else if(month == 2 && flag == true && day >= 30) {
  43. System.out.println("闰年2月只有29天,请重新输入");
  44. continue;
  45. }else if(month == 2 && flag == false && day >= 29){
  46. System.out.println("非闰年2月只有28天,请重新输入");
  47. continue;
  48. }else if(month == 4 || month == 6 || month == 9 || month == 11 && day >=31) {
  49. System.out.println(month+"月只有30天,请重新输入");
  50. continue;
  51. }else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 && day >=32){
  52. System.out.println(month+"月只有31天,请重新输入");
  53. continue;
  54. }
  55. break;
  56. }catch(InputMismatchException e){
  57. System.out.println("您输入的格式有误请重新输入");
  58. scanner.next();
  59. }
  60. }
  61. int days = 0;
  62. switch(month){
  63. default:
  64. System.out.println("您输入的有误");
  65. case 12:
  66. days += 30;
  67. case 11:
  68. days += 31;
  69. case 10:
  70. days += 30;
  71. case 9:
  72. days += 31;
  73. case 8:
  74. days += 31;
  75. case 7:
  76. days += 30;
  77. case 6:
  78. days += 31;
  79. case 5:
  80. days += 30;
  81. case 4:
  82. days += 31;
  83. case 3:
  84. if(flag){
  85. days += 29;
  86. }else{
  87. days += 28;
  88. }
  89. case 2:
  90. days += 31;
  91. case 1:
  92. }
  93. days = days + day;
  94. System.out.println(year+"年"+month+"月"+day+"日,共有"+days+"天");
  95. scanner.close();
  96. }
  97. public static boolean isLeapYear(int year){//判断是否为闰年
  98. if(year % 4 == 0 && year % 100 ==0 || year % 400 == 0 ){
  99. return true;
  100. }else{
  101. return false;
  102. }
  103. }
  104. }

冒泡排序

package com.woniuxy.demo2;
import java.util.Scanner;
import java.util.InputMismatchException;

public class Test2 {
    public static void main(String[] args) {
//        int[] a = {12,34,23,56,69,38,47};
        int[] a = buildArr();
        for(int i=0; i<a.length-1; i++) {
            for(int j = 0; j<a.length-i-1; j++) {
                 exchangeArr(a, j, j+1);//外部定义了一个叫exchange的方法用于交换数组内容
            }
        }
        for(int i:a) {
            System.out.print(i + " ");
        }
    }
    public static int[] exchangeArr(int[] Arr,int index_a, int index_b) {//交换数组下的数据
    int temp = 0;
    if(Arr[index_a] > Arr[index_b]) {
        temp = Arr[index_a];
        Arr[index_a] = Arr[index_b];
        Arr[index_b] = temp;
    }
    return Arr;
    }
    public static int[] buildArr() {//创建一个数组
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要创建的数组大小:");
        int num = 0;
        while(true) {
            try {
                num = scanner.nextInt();
                break;
            }catch(InputMismatchException e){
                System.out.println("您输入的不是整数,请重新输入:");
                scanner.nextLine();
            }
        }
        System.out.println("现在录入数组");
        int[] Arr = new int[num];
        for(int i = 0; i < num; i++) {
            System.out.println("请输入一个整数:");
            try {
                Arr[i] = scanner.nextInt();
            }catch(InputMismatchException e) {
                System.out.println("您输入的不是整数,请重新输入:");
                scanner.nextLine();
                i--;
            }
        }
        scanner.close();
        return Arr;
    }
}

猜拳游戏

要求三局两胜

package com.woniuxy.Demo1;
import java.util.Random;
import java.util.Scanner;

public class Test1 {
    static Scanner scanner = new Scanner(System.in);
    static int flag = 0;//标记输-1赢1平0

    public static void main(String[] args) {
        fingerGame();//猜拳
        scanner.close();
    }

    public static void fingerGame() {    
        Random rand = new Random();
        int count = 0;
        while(count <3 ) {//三局
            UI();
            int player = scanner.nextInt();
            scanner.nextLine();
            int computer = rand.nextInt(3)+1;
            single(player, computer);
            if(flag == 2){//连赢两把
                break;
            }
            if(flag == -2){//连输两把
                break;
            }
            count++;
        }
        if(flag > 0) {
            System.out.println("您赢了");
        }
        else if(flag < 0) {
            System.out.println("您输了");
        }else {
            System.out.println("平局");//平平平,输1赢1平1
        }    
    }    

    public static void UI() {
        System.out.println("------- 欢迎进入猜拳游戏 --------");
        System.out.println("\t    1.剪刀");
        System.out.println("\t    2.石头");
        System.out.println("\t    3.布");
        System.out.println("------请玩家按对应数字完成出拳  ----");
    }

    public static int single( int player, int computer) {
        if(computer == 1) {
            System.out.println("电脑出了剪刀");
            switch(player) {
                case 1://剪刀
                    System.out.println("这一局平局");
                    break;
                case 2://石头
                    System.out.println("您这一局赢了");
                    flag++;
                    break;
                case 3://布
                    System.out.println("您这一局输了");
                    flag--;
                    break;
            }
        }else if(computer == 2) {
            System.out.println("电脑出了石头");
            switch(player) {
                case 1://剪刀
                    System.out.println("您这一局输了");
                    flag--;
                    break;
                case 2://石头
                    System.out.println("这一局平局");
                    break;
                case 3://布
                    System.out.println("您这一局赢了");
                    flag++;
                    break;
            }
        }else if(computer == 3) {
            System.out.println("电脑出了布");
            switch(player) {
                case 1://剪刀
                    System.out.println("您这一局赢了");
                    flag++;
                    break;
                case 2://石头
                    System.out.println("您这一局输了");
                    flag--;
                    break;
                case 3://布
                    System.out.println("这一局平局");
                    break;
            }
        }
        return flag;
    }
}

要求一定要决出胜负

//10.猜拳(不限次数,但一定会决出结果)
package com.woniuxy.HomeWork;

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

public class homeWork2 {
    static Scanner scanner = new Scanner(System.in);
    static int win = 0;//记录赢了几局
    static int lose = 0;//记录输了几局
    static int dogfall = 0;//记录平了几局

    public static void main(String[] args) {
//        石头剪刀布
        fingerGame();
        scanner.close();
    }

    public static void fingerGame() {    
        Random rand = new Random();
        while(win != 2 && lose != 2 ) {//只有输赢,没有平局
            UI();
            int player = scanner.nextInt();
            scanner.nextLine();
            int computer = rand.nextInt(3)+1;
            single(player, computer);
        }
        if(win>=2) {
            System.out.println("您赢了");
        }
        else if(lose >= 2) {
            System.out.println("您输了");
        }else {
            System.out.println("平局");
        }    
    }    

    public static void UI() {
        System.out.println("------- 欢迎进入猜拳游戏 --------");
        System.out.println("\t    1.剪刀");
        System.out.println("\t    2.石头");
        System.out.println("\t    3.布");
        System.out.println("------请玩家按对应数字完成出拳  ----");
    }

    public static void single( int player, int computer) {
        if(computer == 1) {
            System.out.println("电脑出了剪刀");
            switch(player) {
                case 1://剪刀
                    System.out.println("这一局平局");
                    dogfall++;
                    break;
                case 2://石头
                    System.out.println("您这一局赢了");
                    win++;
                    break;
                case 3://布
                    System.out.println("您这一局输了");
                    lose++;
                    break;
            }
        }else if(computer == 2) {
            System.out.println("电脑出了石头");
            switch(player) {
                case 1://剪刀
                    System.out.println("您这一局输了");
                    lose++;
                    break;
                case 2://石头
                    System.out.println("这一局平局");
                    dogfall++;
                    break;
                case 3://布
                    System.out.println("您这一局赢了");
                    win++;
                    break;
            }
        }else if(computer == 3) {
            System.out.println("电脑出了布");
            switch(player) {
                case 1://剪刀
                    System.out.println("您这一局赢了");
                    win++;
                    break;
                case 2://石头
                    System.out.println("您这一局输了");
                    lose++;
                    break;
                case 3://布
                    System.out.println("这一局平局");
                    dogfall++;
                    break;
            }
        }
    }
}

10.//(三局两胜制猜拳)
package com.woniuxy.Demo1;
import java.util.Random;
import java.util.Scanner;

public class Test1 {
    static Scanner scanner = new Scanner(System.in);
    static int flag = 0;//标记输-1赢1平0

    public static void main(String[] args) {
        fingerGame();//猜拳
        scanner.close();
    }

    public static void fingerGame() {    
        Random rand = new Random();
        int count = 0;
        while(count <3 ) {//三局
            UI();
            int player = scanner.nextInt();
            scanner.nextLine();
            int computer = rand.nextInt(3)+1;
            single(player, computer);
            if(flag == 2){//连赢两把
                break;
            }
            if(flag == -2){//连输两把
                break;
            }
            count++;
        }
        if(flag > 0) {
            System.out.println("您赢了");
        }
        else if(flag < 0) {
            System.out.println("您输了");
        }else {
            System.out.println("平局");//平平平,输1赢1平1
        }    
    }    

    public static void UI() {
        System.out.println("------- 欢迎进入猜拳游戏 --------");
        System.out.println("\t    1.剪刀");
        System.out.println("\t    2.石头");
        System.out.println("\t    3.布");
        System.out.println("------请玩家按对应数字完成出拳  ----");
    }

    public static int single( int player, int computer) {
        if(computer == 1) {
            System.out.println("电脑出了剪刀");
            switch(player) {
                case 1://剪刀
                    System.out.println("这一局平局");
                    break;
                case 2://石头
                    System.out.println("您这一局赢了");
                    flag++;
                    break;
                case 3://布
                    System.out.println("您这一局输了");
                    flag--;
                    break;
            }
        }else if(computer == 2) {
            System.out.println("电脑出了石头");
            switch(player) {
                case 1://剪刀
                    System.out.println("您这一局输了");
                    flag--;
                    break;
                case 2://石头
                    System.out.println("这一局平局");
                    break;
                case 3://布
                    System.out.println("您这一局赢了");
                    flag++;
                    break;
            }
        }else if(computer == 3) {
            System.out.println("电脑出了布");
            switch(player) {
                case 1://剪刀
                    System.out.println("您这一局赢了");
                    flag++;
                    break;
                case 2://石头
                    System.out.println("您这一局输了");
                    flag--;
                    break;
                case 3://布
                    System.out.println("这一局平局");
                    break;
            }
        }
        return flag;
    }
}