Home > Java > javaTutorial > Share a few typical examples of Java programming

Share a few typical examples of Java programming

怪我咯
Release: 2017-06-25 10:10:35
Original
1903 people have browsed it

1. Simulate the hotel room management system, you need to follow the following functions:

1, 1 in room number guest name, check -in function

1, 2 OUT room number check -out function

1, 3 Search Room Number to query the room status. If the room number is -1, all the room status is output

1, 4 quit or exit Exit

Tip: Two-dimensional array to implement

The code is implemented as follows:

import java.util.Scanner;

public class HotelDemo {
    //写在类里面,则每个方法都可以访问到,避免了参数传递的繁琐;
    static int h=5,w=10;
    static String[][] rooms=new String[5][10];
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner s=new Scanner(System.in);
         while(true){
             System.out.println("请输入  in,out,search,quit:");
             String temp=s.next();
             int room=0;
             if("in".equals(temp)){//防止出现空指针异常;
                 System.out.println("输入房间号:");
                 room=s.nextInt();
                 System.out.println("输入名字:");
                 String name=s.next();
                 if(in(room,name)) System.out.println("入住完成!");
                 System.out.println("room"+room+"name"+name);
             }else if("out".equals(temp)){
                 System.out.println("输入房间号:");
                 room=s.nextInt();
                 if(out(room)) System.out.println("退房完成!");
                 System.out.println("out"+room);
             }else if("search".equals(temp)){
                 System.out.println("输入房间号(-1代表全部):");
                 room=s.nextInt();
                 search(room);
             }else if("quit".equals(temp)||"exit".equals(temp)){
                 break;
             }else{
                 System.out.println("命令错误!");
             }
         }
     }

     private static boolean search(int room) {
         if(room==-1){
             //打印所有的信息;
             for(int i=0;i<h;i++){
                 for(int j=0;j<w;j++){
                     int room2=(i+1)*100+j+1;
                     System.out.print(room2+"\t");
                 }
                 System.out.println();
                 for(int k=0;k<w;k++){
                     System.out.print(rooms[i][k]==null?"empty":rooms[i][k]);
                     System.out.print("\t");
                 }
                 System.out.println();
                 System.out.println();
             }
             return true;

         }else{
             int r=room/100-1;
             int c=room%100-1;
             if(r<0||r>=h||c<0||c>=w){
                 System.out.println("房间号错误!");
                 return false;
             }
             System.out.println(rooms[r][c]==null?"empty":rooms[r][c]);
             return true;
         }
     }

     private static boolean out(int room) {
         int r=room/100-1;
         int c=room%100-1;
         if(r<0||r>=h||c<0||c>=w){
             System.out.println("房间号错误!");
             return false;
         }
         if(rooms[r][c]==null||"".equals(rooms[r][c])){//
             System.out.println("此房间没有人!");
             return false;
         }
         rooms[r][c]=null;
         return true;
     }

     private static boolean in(int room, String name) {
         int r=room/100-1;
         int c=room%100-1;
         if(r<0||r>=h||c<0||c>=w){
             System.out.println("房间号错误!");
             return false;
         }
         if(rooms[r][c]!=null){//
             System.out.println("此房间已经有人!");
             return false;
         }

         rooms[r][c]=name;
         return true;

     }

 }
Copy after login

2. Spiral matrix Example

import java.util.Scanner;
 
public class SpiralSquare01{
 
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner s=new Scanner(System.in);
        System.out.println("请输入螺旋方阵的长");
        int indexY=s.nextInt();
        System.out.println("请输入螺旋方阵的宽");
        int indexX=s.nextInt();
        if(indexX<=0||indexY<=0){
            System.out.println("输入的数字不合法!");
            return;
        }
        int[][] square=new int[indexX][indexY];
        int x=0;
        int y=0;
        for(int i=1;i<=indexX*indexY;){
            while(y<square[x].length-1&&square[x][y+1]==0){
                square[x][y++]=i++;
            }
            while(x<square.length&&square[x][y]==0){
                square[x++][y]=i++;
            }
            while(y>0&&square[x-1][y-1]==0){
                square[x-1][--y]=i++;
            }
            --x;
            while(x>1&&square[x-1][y]==0){
                square[--x][y]=i++;
            }
            y++;
        }
        for(int i=0;i<square.length;i++){
            for(int j=0;j<square[i].length;j++){
                System.out.print(square[i][j]+"\t");
            }
            System.out.println();
        }
    }
}
Copy after login

Running result:

3. Classic mathematics problem: a variation of the Hundred Chicken Problem

Problem description: There are 36 people and 36 bricks. Each person moved it once and just finished moving it. Among them, men each move 4 pieces each time, women each move 3 pieces each time, and two children move one piece each time. Ask: How many men, women and children are there?

public class TestBrick {
    public static void main(String[] args) {
        int manNum=0;
        int womanNum=0;
        for(int i=0;i<=9;i++){
            for(int j=0;j<12;j++){      
                if(((i*4+j*3+(36-i-j)/2)==36)&&((36-i-j)%2==0)){
                    //注意:孩子的人数必须是偶数,否则会出现一个孩子一次也没有搬的情况,不符合题意
                    manNum=i;
                    womanNum=j;
                    System.out.println("男的的人数是"+manNum);
                    System.out.println("女的的人数是"+womanNum);
                    System.out.println("孩子的人数是"+(36-manNum-womanNum));
                }
            }
        }  
    }
 
}
Copy after login

4. Countdown algorithm: Enter a number of seconds and convert it to the format of XX hours, XX minutes and XX seconds and output it

import java.util.Scanner;
 
public class TestTime {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        @SuppressWarnings("resource")
        Scanner s=new Scanner(System.in);
        System.out.println("请输入秒数:");
        int second =s.nextInt();
        int hour=second/3600;
        int minite=second%3600/60;
        int sec=second%60;
 
        System.out.println("转换后为:"+hour+"小时"+minite+"分"+sec+"秒");
         
    }
 
}
Copy after login

5. Automatic password generator: The password consists of uppercase letters/lowercase letters/numbers, and generates a six-digit random password;

//密码的自动生成器:密码由大写字母/小写字母/数字组成,生成六位随机密码;
import java.util.Random;
 
public class TestPassword {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
               char[] pardStore=new char[62];
                 //把所有的大写字母放进去
                   for(int i=0;i<20;i++){
                       pardStore[i]=(char)(&#39;A&#39;+i);
                   }
                 //把所有的小写字母放进去
                   for(int i=26;i<52;i++){
                       pardStore[i]=(char)(&#39;a&#39;+i);
                   }
                    
                   //吧0到9放进去
                   for(int i=52;i<62;i++){
                       pardStore[i]=(char)(&#39;0&#39;+(i-52));
                   }
                    
                   //生成6位随机密码
                   Random r=new Random();
                   for(int i=0;i<6;i++){
                       int n=r.nextInt(62);
                       System.out.print(pardStore[n]);
                   }
               }
    }
Copy after login

#6. Write a lottery generation code: 1-33 Randomly select 7 non-repeating numbers;

import java.util.Random;

//写一个彩票的生成代码: 1-33随机选7个不重复的数字;
public class TestLuckyTicket {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         int[] luckTickets=new int[7];
         
         Random r=new Random();
         for(int i=0;i<luckTickets.length;i++){
             luckTickets[i]=r.nextInt(8)+1;
             for(int j=0;j<i;j++){
                 if(luckTickets[i]==luckTickets[j]){
                     i--;
                     break;
                 }
             }
         }
         for(int i=0;i<luckTickets.length;i++){
             System.out.print(luckTickets[i]+",");
         }
    }

}
Copy after login

7. Define a string variable String str="There is bright moonlight in front of the bed, suspected to be frost on the ground. Look up at the moon, bow your head and miss your hometown.". Print it in the following format:

low raise doubts bed

头头头是前

思愿地明

therefore 明上月

Township Moon Frost Light

. , . ,

public class TestPoet {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            String str="床前明月光,疑是地上霜。举头望明月,低头思故乡。";
            char[] poet=str.toCharArray();
            int l=18;
            boolean flag=true;
            int i=0;
            while(flag){
                for(int j=l;j>=(0+i);){
                    System.out.print(poet[j]);
                    j=j-6;
                }
                System.out.println();
                l++;
                i++;
                if(l==24){flag=false;}
            }
    }
 
}
Copy after login

8. The output of the nine-square grid: the nine-square grid means that the sum of the numbers in each row, each column, the diagonal column and the reverse diagonal column are equal; the most basic It is three rows and three columns = 9 grids, which is the famous nine-square grid; it can also be extended to 5*5=25 grids; as long as the number of rows and columns are equal and an odd number;

import java.util.Scanner; public class JiuGongGe {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         @SuppressWarnings("resource")
         Scanner s=new Scanner(System.in);
         System.out.println("请输入一个大于等于3的奇数");
         int length=s.nextInt();
         if(length<3||length%2==0){
             System.out.println("输入的数字不合法!");
             return;
         }
         int[][] nineTable=new int[length][length];
         int indexX=0;
         int indexY=0;
         indexY=(nineTable.length-1)/2;
         nineTable[indexX][indexY]=1;
         for(int i=1;i<nineTable.length*nineTable.length;i++){
             indexX--;
             indexY++;
             if(indexY>=nineTable.length&&indexX>=0){
                 indexY=0;
             }else if(indexX<0&&indexY<nineTable.length){
                 indexX=nineTable.length-1;
             }else if(indexY>=nineTable.length&&indexX<0){
                 indexY--;
                 indexX=indexX+2;
             }else if(nineTable[indexX][indexY]!=0){
                 indexY--;
                 indexX=indexX+2;
             }
             nineTable[indexX][indexY]=i+1;

         }
         for(int i=0;i<nineTable.length;i++){
             for(int j=0;j<nineTable[i].length;j++){
                 System.out.print(nineTable[i][j]+"   ");
             }
             System.out.println();
             System.out.println();
         }

     }

     }
Copy after login


The above is the detailed content of Share a few typical examples of Java programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template