06Java数组

Posted Kefi的个人博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了06Java数组相关的知识,希望对你有一定的参考价值。

动手动脑:

 

 

import java.io.*;

 

public class QiPan

{

     //定义一个二维数组来充当棋盘

     private String[][] board;

     //定义棋盘的大小

     private static int BOARD_SIZE = 15;

     public void initBoard()

     {

            //初始化棋盘数组

            board = new String[BOARD_SIZE][BOARD_SIZE];

            //把每个元素赋为"╋",用于在控制台画出棋盘

            for (int i = 0 ; i < BOARD_SIZE ; i++)

            {

                   for ( int j = 0 ; j < BOARD_SIZE ; j++)

                   {

                          board[i][j] = "╋";

                   }

            }

     }

     //在控制台输出棋盘的方法

     public void printBoard()

     {

            //打印每个数组元素

            for (int i = 0 ; i < BOARD_SIZE ; i++)

            {

                   for ( int j = 0 ; j < BOARD_SIZE ; j++)

                   {

                          //打印数组元素后不换行

                          System.out.print(board[i][j]);

                   }

                   //每打印完一行数组元素后输出一个换行符

                   System.out.print("\\n");

            }

     }

    public static void main(String[] args)throws Exception

    {

        QiPan gb = new QiPan();

            gb.initBoard();

            gb.printBoard();

            //这是用于获取键盘输入的方法

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            String inputStr = null;

                System.out.println("请输入您下棋的座标,应以x,y的格式:");

            //br.readLine():每当在键盘上输入一行内容按回车,刚输入的内容将被br读取到。

            while ((inputStr = br.readLine()) != null)

            {

                   //将用户输入的字符串以逗号(,)作为分隔符,分隔成2个字符串

                   String[] posStrArr = inputStr.split(",");

                   //将2个字符串转换成用户下棋的座标

                   int xPos = Integer.parseInt(posStrArr[0]);

                   int yPos = Integer.parseInt(posStrArr[1]);

                   //把对应的数组元素赋为"●"。

                   gb.board[xPos - 1][yPos - 1] = "●";                          

                   /*

                    电脑随机生成2个整数,作为电脑下棋的座标,赋给board数组。

                    还涉及

                          1.座标的有效性,只能是数字,不能超出棋盘范围

                          2.如果下的棋的点,不能重复下棋。

                          3.每次下棋后,需要扫描谁赢了

                    */

                   gb.printBoard();

                   System.out.println("请输入您下棋的座标,应以x,y的格式:");

            }

    }

}

 

 

 

public class Num2Rmb

{

     private String[] hanArr = {"零" , "壹" , "贰" , "叁" , "肆" ,

            "伍" , "陆" , "柒" , "捌" , "玖"};

     private String[] unitArr = {"十" , "百" , "千","万","十万","百万"};

 

    

 

     /**

      * 把一个四位的数字字符串变成汉字字符串

      * @param numStr 需要被转换的四位的数字字符串

      * @return 四位的数字字符串被转换成的汉字字符串。

      */

     private String toHanStr(String numStr)

     {

            String result = "";

            int numLen = numStr.length();

            //依次遍历数字字符串的每一位数字

            for (int i = 0 ; i < numLen ; i++ )

            {

                   //把char型数字转换成的int型数字,因为它们的ASCII码值恰好相差48

                   //因此把char型数字减去48得到int型数字,例如\'4\'被转换成4。

                   int num = numStr.charAt(i) - 48;

                   //如果不是最后一位数字,而且数字不是零,则需要添加单位(千、百、十)

                   if ( i != numLen - 1 && num != 0)

                   {

                          result += hanArr[num] + unitArr[numLen - 2 - i];

                   }

                   //否则不要添加单位

                   else

                   {

                         

                          //上一个数是否为“零”,不为“零”时就添加

                          if(result.length()>0 && hanArr[num].equals("零") && result.charAt(result.length()-1)==\'零\')

                                 continue;

                          result += hanArr[num];

                   }

            }

            //只有个位数,直接返回

            if(result.length()==1)

                   return result;

           

            int index=result.length()-1;

            while(result.charAt(index)==\'零\'){

                   index--;

            }

            if(index!=result.length()-1)

                   return result.substring(0,index+1);

            else {

                   return result;

            }

     }

 

    public static void main(String[] args)

    {       

            Num2Rmb nr = new Num2Rmb();

            System.out.println("只支持整数(0~百万)");

            //测试把一个四位的数字字符串变成汉字字符串

           

            System.out.println(nr.toHanStr("123"));

            System.out.println(nr.toHanStr("105"));

            System.out.println(nr.toHanStr("1000"));

            System.out.println(nr.toHanStr("1100"));

            System.out.println(nr.toHanStr("1110"));

    

           

    }

}

 

 

 

 

源代码:

 

 

 

import java.util.Scanner;

 

public class Bigint {

 

    private int [] big = new int[20];

    

    public void init()

    {

        for(int i = 0; i < 20; i++)

            big[i] = 0;

    }

    

    public void input(String s)

    {

        for(int i = 0 ; i < s.length(); i++)

            big[s.length() - i - 1] = s.charAt(i) - \'0\';

    }

 

    public void show()

    {

        int al = 0;

        for(int i = 19;i >= 0 ; i--)

            if(big[i] != 0)

            {

                al = i;

                break;

            }

            

        for(int j = 0;j <= al; j++)

            System.out.print(big[al - j]);

    }

    

    public boolean bigger(Bigint a,Bigint b)

    {

        int al = 0,bl = 0;

        for(int i = 19;i >= 0 ; i--)

            if(a.big[i] != 0)

            {

                al = i;

                break;

            }

        

        for(int i = 19;i >= 0 ; i--)

            if(b.big[i] != 0)

            {

                bl = i;

                break;

            }

        if(al > bl)

            return true;

        else if(al < bl)

            return false;

        else

        {

            if(a.big[al] > b.big[al])

                return true;

            else if(a.big[al] < b.big[al])

                return false;

            else

            {

                while(a.big[al] == b.big[al])

                {

                    if(al != 0)

                    {

                        if(a.big[al - 1] > b.big[al - 1])

                            return true;

                        else if(a.big[al - 1] < b.big[al - 1])

                            return false;

                        else

                            al --;

                    }

                    else

                        return true;

                }

            }

        }

        return false;

    }

    

    public void add(Bigint a,Bigint b)

    {

        Bigint result = new Bigint();

        result.init();

        

        if(a.bigger(a, b))

        {

            int l = 0,temp;

            for(int i = 19;i >= 0 ; i--)

                if(a.big[i] != 0)

                {

                    l = i;

                    break;

                }

            l++;

            for(int j = 0;j < l; j++)

            {

                temp = a.big[j] + b.big[j];

                if(temp > 9)

                {

                    result.big[j] = (temp - 10);

                    result.big[j + 1] += 1;

                }

                else

                    result.big[j] = temp;

            }

        }

        else

        {

            int l = 0,temp;

            for(int i = 19;i >= 0 ; i--)

                if(b.big[i] != 0)

                {

                    l = i;

                    break;

                }

            l++;

            for(int j = 0;j < l; j++)

            {

                temp = a.big[j] + b.big[j];

                if(temp > 9)

                {

                    result.big[j] = (temp - 10);

                    result.big[j + 1] += 1;

                }

                else

                    result.big[j] = temp;

            }

        }

        result.show();

        System.out.print("\\n");

    }

    

    public void sub(Bigint a,Bigint b)

    {

        Bigint result = new Bigint();

        result.init();

        

        if(a.bigger(a, b))

        {

            int l = 0,temp;

            for(int i = 19;i >= 0 ; i--)

                if(a.big[i] != 0)

                {

                    l = i;

                    break;

                }

            

            for(int j = 0;j < l; j++)

            {

                temp = a.big[j] - b.big[j];

                if(temp < 0)

                {

                    result.big[j] = (temp + 10);

                    result.big[j + 1] -= 1;

                }

                else

                    result.big[j] = temp;

            }

        }

        else

        {

            System.out.print("-");

            int l = 0,temp;

            for(int i = 19;i >= 0 ; i--)

                if(b.big[i] != 0)

                {

                    l = i;

                    break;

                }

            

            for(int j = 0;j < l; j++)

            {

                temp = a.big[j] - b.big[j];

                if(temp < 0)

                {

                    result.big[j] = (temp + 10);

                    result.big[j + 1] -= 1;

                }

                else

                    result.big[j] = temp;

            }

        }

        result.show();

        System.out.print("\\n");

    }

    

    public static void main(String[] args) {

        Bigint b1 = new Bigint();

        b1.init();

        Bigint b2 = new Bigint();

        b2.init();

        

        System.out.println("请输入两个正的大数:");

        Scanner s = new Scanner(System.in);

        String str1 = s.next();

        String str2 = s.next();

        s.close();

        b1.input(str1);

        b2.input(str2);

        

        System.out.print(str1 + "+" + str2 + "=");

        b1.add(b1, b2);

        System.out.print(str1 + "-" + str2 + "=");

        b1.sub(b1, b2);

    }

 

}

 

 

 

 

程序设计思路:首先产生十个随机数,然后赋值到一个数组中,计算这十个数的和,然后把这十个数以及计算的和结果以消息框的形式显示出去。

 
   

 

 

流程图:

 

 

 

源程序:

/*

 * 对数组中的元素赋上随机数,然后求和,以消息框的形式输出

 * 作者:徐浩军   日期;2016.11.3  天气:还可以

 */

 

 

package 数组;

 

import java.util.Arrays;

 

import javax.swing.*;

 

public class Add {

 

     public static void main(String[] args) {

            // TODO Auto-generated method stub

           

            int a[]=new int [10];  //创建一个整数型数组,长度为10

           

            int sum=0; //和

           

            String output="";        //用于输出

           

            output+="数组中的元素有:\\n";  

                         

            for(int i=0;i<a.length;i++){

                   a[i]=(int)(Math.random()*50+1);   //为数组中的每一个元素赋一个随机数

                   sum+=a[i];

                  

            }

            output+=Arrays.toString(a);   //输出数组中的元素

           

            output+="\\n求和的结果是:   "+sum;        //把运算结果输出去

           

            JTextArea textarea=new JTextArea(5,10);     //创建一个文本区域,用于显示结果

           

            textarea.setText(output);

           

            //以消息框的形式显示结果

            JOptionPane.showMessageDialog(null,textarea,"显示数组元素以及求和结果",JOptionPane.INFORMATION_MESSAGE);

           

        

        System.exit(0);

       

     }

 

}

程序结果截图:

 

以上是关于06Java数组的主要内容,如果未能解决你的问题,请参考以下文章

Java数组06二维数组

java06-数组动手动脑

java基础语法学习day06---初学者必看详解

java基础语法学习day06---初学者必看详解

06Java数组

java,list数组转换成map