Java課後練習6

來源:互聯網
上載者:User

標籤:9.png   rac   哪些   ring   his   cep   算術   except   integer   

動手動腦1:閱讀QiPan.java樣本程式瞭解如何利用二維數組和迴圈語句繪製五子棋盤

1)原始碼:
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的格式:");
  }
    }
}

2)輸出結果:

動手動腦2:請編寫一個程式將一個整數轉換為漢字讀法字串。比如“1123”轉換為“一千一百二十三”。
1)原始碼:

package ceshi;


public class bai
{
 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)
 {
  bai nr = new bai();
  //測試把一個四位的數字字串變成漢字字串
  System.out.println(nr.toHanStr("1123"));

 }
 }

2)輸出結果:

動手動腦3:

前面幾講介紹過JDK所提供的BigInteger能完成大數計算,如果不用它,直接使用數組表達大數,你能實現相同的功能嗎?
要求:
(1)用你的大數類實現加和減兩個功能
(2)閱讀BigInteger類源碼,弄清楚它是使用什麼演算法實現加減乘除四種運算的?
(3)通過互連網尋找大數運算的相關資料,給你的大數類添加乘、除、求階乘等其它功能。

1)BigInteger曆史介紹
在java中,存在很多種類的資料類型,例如byte short char int float double long,而BigInteger屬於其中一個比較特殊的資料類型,也是本教程關注的重點。BigInteger在JDK1.1中就已經存在了,屬於java.math包的類。從名字來看,BigInteger比Integer表示數值的範圍更大一些。BigInteger類的基本結構如下所示:
java.lang.Object
|_java.lang.Number
|_java.math.BigInteger
BigInteger已實現的介面:Serializable, Comparable<BigInteger>

2)BigInteger是不可變的任意精度的整數。所有操作中,都以二進位補碼形式表示 BigInteger(如 Java 的基本整數類型)。BigInteger 提供所有 Java 的基本整數操作符的對應物,並提供 java.lang.Math 的所有相關方法。另外,BigInteger 還提供以下運算:模算術、GCD 計算、質數測試、素數產生、位操作以及一些其他動作。

3)BigInteger屬性分析
下面看看BigInteger有哪些重點的屬性,主要的有下面三個:
1.final int signum
signum屬性是為了區分:正負數和0的標誌位,JDK注釋裡面已經說的很明白了:
The signum of this BigInteger: -1 for negative, 0 for zero, or 1 for positive. Note that the BigInteger zero must have a signum of 0. This is necessary to ensures that there is exactly one representation for each BigInteger value.
2.final int[] mag
mag是magnitude的縮寫形式,mag數組是儲存BigInteger數值大小的,採用big-endian的順序,也就是高位位元組存入低地址,低位位元組存入高地址,依次排列的方式。JDK原文注釋如下:
The magnitude of this BigInteger, in big-endian order: the zeroth element of this array is the most-significant int of the magnitude. The magnitude must be "minimal" in that the most-significant int (mag[0]) must be non-zero. This is necessary to ensure that there is exactly one representation for each BigInteger value. Note that this implies that the BigInteger zero has a zero-length mag array.
3.final static long LONG_MASK = 0xffffffffL;
This mask is used to obtain the value of an int as if it were unsigned。

原始碼:

package ceshi;


import java.math.BigInteger;
public class bai
{
 public static void main(String[]args)
 {
  // TODO Auto-generated method stub
  BigInteger aa=new BigInteger("100");
  BigInteger bb=new BigInteger("25");
  BigInteger sub=aa.subtract(bb);//大整數的減
  BigInteger add=aa.add(bb);//大整數的加
  BigInteger mul=aa.multiply(bb);//大整數的乘
  BigInteger div=aa.divide(bb);//大整數的除
  System.out.println("大整數的減:"+sub.toString());
  System.out.println("大整數的加:"+add.toString());
  System.out.println("大整數的乘:"+mul.toString());
  System.out.println("大整數的除:"+div.toString());
  }
 }

輸出結果:

課後作業1:隨機產生10個數,填充一個數組,然後用訊息框顯示數組內容,接著計算數組元素的和,將結果也顯示在訊息框中。

設計思想:首先用random結構產生10個隨機數,再將10個數一次輸入數組,再計算10個數的和,最後輸出10個數和和

原始碼:

 

package ceshi;
import javax.swing.*;

public class bai {
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  String output= "10個隨機數為:\n";
  int sum=0;
  int a []=new int [10];
  for(int i = 0;i<10;i++)
  {
   a[i]=(int) (Math.random()*10);
   output += " "+a[i];
   sum += a[i];
   }

  output +="\n\n十個數的和是:"+sum;

  JOptionPane.showMessageDialog(null,output,"結果",
    JOptionPane.PLAIN_MESSAGE);
  }
 }

輸出結果:

編程總結:利用random結構能產生隨機數再用  JOptionPane.showMessageDialog(null,output,"結果",
JOptionPane.PLAIN_MESSAGE);輸出數。

 

Java課後練習6

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.