java中06的動手動腦

來源:互聯網
上載者:User

標籤:public   分行符號   程式   二維   使用者   exce   static   [1]   package   

任務一:

代碼:

package Shi;

 

import javax.swing.JOptionPane;

 

public class Shi6d1 {

 

public static void main(String[] args) {

 

int array[] = new int[10];

for(int m=0;m<10;m++)

{array[m] = (int)(Math.random()*100);}

String str="";

for(int m=0;m<10;m++)

{str=str+array[m]+" ";}

int b=0;

for(int m=0;m<10;m++)

{b=b+array[m];}

str=str+"的和為:"+b;

JOptionPane.showMessageDialog(null,str,"",JOptionPane.PLAIN_MESSAGE);

}

 

}

                                              

思路:

先定義一個長度為10的數組,然後通過for迴圈逐一隨機賦值,然後定義一個int類型變數b求出數組中10個數的和,然後將數組中各個數加上空格賦值給一個String類型的str,在最後加上和,然後通過對話方塊輸出。

程式流程圖:

 

動手動腦:

棋盤:

代碼:

package Shi;

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的格式:");

}

    }

}

    

數字轉化大寫:

代碼:

package Shi;

 

import javax.swing.JOptionPane;

 

public class Shi6d2 {

 

public static void main(String[] args) {

String as=JOptionPane.showInputDialog("輸入數字");

double number=Double.parseDouble(as);

number=number*100;

int n=0,num=(int)number; //分後全部捨棄

int array[]=new int[9];

for(int i=0;i<10;i++){

if(num==0)

break;

array[i]=num%10;

num=num/10;

n++;

}

String str[]={"分","角","元","拾","佰","千","萬","十","佰"};

String str1[]={"零","壹","貳","三","肆","伍","陸","柒","捌","玖"};

for(int i=n-1;i>=0;i--){

System.out.print(str1[array[i]]);

System.out.print(str[i]);}}}

:       

   

大數:

代碼:

加法

import javax.swing.*;

public class Big {

public static void main(String[] args) {

String num1=JOptionPane.showInputDialog("第一個數字");

String num2=JOptionPane.showInputDialog("第一個數字");

int nu1[]=new int[10];

int nu2[]=new int[10];

int nu3[]=new int[10];

int j=0;

for(int i=num1.length()-1;i>=0;i--){

nu1[j++]=num1.charAt(i)-‘0‘;

}

j=0;

for(int i=num2.length()-1;i>=0;i--){

nu2[j++]=num2.charAt(i)-‘0‘;

}

int next=0;

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

nu3[i]=(nu1[i]+nu2[i]+next)%10;

next=(nu1[i]+nu2[i]+next)/10;

}

for(int i=nu3.length-1;i>=0;i--){

System.out.print(nu3[i]);

}

}

}

減法

package Array;

Import javax.swing.*;

public class Big {

public static void main(String[] args) {

String num1=JOptionPane.showInputDialog("第一個數字");

String num2=JOptionPane.showInputDialog("第一個數字");

int nu1[]=new int[10];

int nu2[]=new int[10];

int nu3[]=new int[10];

int j=0;

for(int i=num1.length()-1;i>=0;i--){

nu1[j++]=num1.charAt(i)-‘0‘;

}

j=0;

for(int i=num2.length()-1;i>=0;i--){

nu2[j++]=num2.charAt(i)-‘0‘;

}

//兩數字比較大小

int flag=0;

for(int i=nu1.length-1;i>=0;i--){

if(nu1[i]>nu2[i]){

flag=1;break;//1表示1數字大

}

if(nu1[i]<nu2[i]){

flag=2;break;//2表示2數字大

}

}

int next=0;

if(flag==1){

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

nu3[i]=nu1[i]-nu2[i]+next;

if(nu3[i]<0){

next=-1;

nu3[i]=nu3[i]+10;

}

else

next=0;

}

for(int i=nu3.length-1;i>=0;i--){

System.out.print(nu3[i]);

}}

if(flag==2){

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

nu3[i]=nu2[i]-nu1[i]+next;

if(nu3[i]<0){

next=-1;

nu3[i]=nu3[i]+10;

}

else

next=0;

}

System.out.print("-");

for(int i=nu3.length-1;i>=0;i--){

System.out.print(nu3[i]);

}

if(flag==0){

System.out.println("0");

}}

}

}

 

java中06的動手動腦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.