Introduction to Java Programming编程题7.11<硬币游戏:显示正反面>

本文介绍了一个简单的Java程序,该程序能够将输入的0到511之间的整数转换为由'H'和'T'组成的三维硬币矩阵。通过将数字转换为其二进制表示形式,并进一步映射为硬币的正面和反面来实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
Enter a number beteewn 0 and 511: 7
H H H
H H H
T T T

Enter a number beteewn 0 and 511: 511
T T T
T T T
T T T

 */
import java.util.Scanner;

public class Coin {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number beteewn 0 and 511: ");
        int number = input.nextInt();

        showMatrix(number);

    }

    public static void showMatrix(int n) {
        int[] b = toBinary(n);
        char[] ch = new char[b.length];
        for (int i = 0; i < b.length; i++) {
            if (b[i] == 0)
                ch[i] = 'H';
            else
                ch[i] = 'T';
        }
        showCoinMatrix(ch);
    }

    public static int[] toBinary(int n) {
        int digits = 9;
        int[] binary = new int[digits];
        while (n != 0) {
            binary[--digits] = n % 2;
            n /= 2;
        }

        return binary;
    }

    public static void showCoinMatrix(char[] ch) {
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i] + " ");
            if ((i+1) % 3 == 0)
                System.out.println();
        }
    }
}
使用Oracle 设计DB存储如下xml 中数据,<msgBody><msgId>AMAT_1A19Y_02829.C00666.UR0ER6.20221020130718</msgId><eqpId>ASSORA-01</eqpId><carrierId>C00666</carrierId><runMode>Y</runMode><batchID>AMAT_1A19Y_02829</batchID><eapUserId>EAPAUTO</eapUserId><fabId>ZH</fabId><function>WAFERSTART</function><sorterAction>WAFERSTART</sorterAction><actionFlag>T</actionFlag><slotMapping>018,UR0ER6.18,C00666,001,ZA0162T.01,C00682;007,UR0ER7.07,C00674,002,ZA0162T.02,C00682;008,UR0ER7.08,C00674,003,ZA0162T.03,C00682;009,UR0ER7.09,C00674,004,ZA0162T.04,C00682;010,UR0ER7.10,C00674,005,ZA0162T.05,C00682</slotMapping><carrierArriveTime>2022-10-20 14:59:50</carrierArriveTime><clampTime>2022-10-20 15:00:32</clampTime><loadStartTime>2022-10-20 15:10:33</loadStartTime><loadCompleteTime>2022-10-20 15:20:33</loadCompleteTime><unloadCompleteTime>2022-10-20 15:40:40</unloadCompleteTime><carrierRemoveTime>2022-10-20 17:00:33</carrierRemoveTime><carrierList><carrierID>C00666</carrierID><carrierType>P</carrierType><lotIDList><lotID>UR0ER6</lotID></lotIDList></carrierList><carrierList><carrierID>C00674</carrierID><carrierType>P</carrierType><lotIDList><lotID>UR0ER7</lotID></lotIDList></carrierList><carrierList><carrierID>C00682</carrierID><carrierType>C</carrierType><lotIDList><lotID>ZA0162T.01</lotID></lotIDList></carrierList><sorterJobList><jobID>WAFERSTART-C00666-C00682</jobID><lotIDList><lotID>UR0ER6</lotID><lotID>ZA0162T.01</lotID></lotIDList><origLP>LP1</origLP><destnLP>LP2</destnLP><transferSlotMap><fromTo>018-001</fromTo></transferSlotMap><origCarrier><carrierID>C00666</carrierID><lotIDList><lotID>UR0ER6</lotID></lotIDList><category>C</category><carrierType>C</carrierType><location/><mesMapping>0000000000000000011110000</mesMapping><pSlotInfo><slotList><slotId>018</slotId><waferId>UR0ER6.18</waferId></slotList></pSlotInfo><cSlotInfo><slotList><slotId>019</slotId><waferId>UR0ER6.19</waferId></slotList><slotList><slotId>020</slotId><waferId>UR0ER6.20</waferId></slotList><slotList><slotId>021</slotId><waferId>UR0ER6.21</waferId></slotList></cSlotInfo></origCarrier><destnCarrier><carrierID>C00682</carrierID><lotIDList><lotID>ZA0162T.01</lotID></lotIDList><category>C</category><carrierType>C</carrierType><location>WS</location><mesMapping>0000000000000000000000000</mesMapping><pSlotInfo/><cSlotInfo><slotList><slotId>001</slotId><waferId>ZA0162T.01</waferId></slotList><slotList><slotId>002</slotId><waferId>ZA0162T.02</waferId></slotList><slotList><slotId>003</slotId><waferId>ZA0162T.03</waferId></slotList><slotList><slotId>004</slotId><waferId>ZA0162T.04</waferId></slotList><slotList><slotId>005</slotId><waferId>ZA0162T.05</waferId></slotList></cSlotInfo></destnCarrier></sorterJobList><sorterJobList><jobID>WAFERSTART-C00674-C00682</jobID><lotIDList><lotID>UR0ER7</lotID><lotID>ZA0162T.01</lotID></lotIDList><origLP>LP1</origLP><destnLP>LP2</destnLP><transferSlotMap><fromTo>007-002</fromTo><fromTo>008-003</fromTo><fromTo>009-004</fromTo><fromTo>010-005</fromTo></transferSlotMap><origCarrier><carrierID>C00674</carrierID><lotIDList><lotID>UR0ER7</lotID></lotIDList><category>C</category><carrierType>C</carrierType><location>RR</location><mesMapping>000000111111111111110000</mesMapping><pSlotInfo><slotList><slotId>007</slotId><waferId>UR0ER7.07</waferId></slotList><slotList><slotId>008</slotId><waferId>UR0ER7.09</waferId></slotList><slotList><slotId>009</slotId><waferId>UR0ER7.09</waferId></slotList><slotList><slotId>010</slotId><waferId>UR0ER7.10</waferId></slotList></pSlotInfo><cSlotInfo><slotList><slotId>011</slotId><waferId>UR0ER7.11</waferId></slotList><slotList><slotId>012</slotId><waferId>UR0ER7.12</waferId></slotList><slotList><slotId>013</slotId><waferId>UR0ER7.13</waferId></slotList><slotList><slotId>014</slotId><waferId>UR0ER7.14</waferId></slotList><slotList><slotId>015</slotId><waferId>UR0ER7.15</waferId></slotList><slotList><slotId>016</slotId><waferId>UR0ER7.16</waferId></slotList><slotList><slotId>017</slotId><waferId>UR0ER7.17</waferId></slotList><slotList><slotId>018</slotId><waferId>UR0ER7.18</waferId></slotList><slotList><slotId>019</slotId><waferId>UR0ER7.19</waferId></slotList><slotList><slotId>020</slotId><waferId>UR0ER7.20</waferId></slotList></cSlotInfo></origCarrier><destnCarrier><carrierID>C00682</carrierID><lotIDList><lotID>ZA0162T.01</lotID></lotIDList><category>C</category><carrierType>C</carrierType><location>WS</location><mesMapping>0000000000000000000000000</mesMapping><pSlotInfo/><cSlotInfo><slotList><slotId>001</slotId><waferId>ZA0162T.01</waferId></slotList><slotList><slotId>002</slotId><waferId>ZA0162T.02</waferId></slotList><slotList><slotId>003</slotId><waferId>ZA0162T.03</waferId></slotList><slotList><slotId>004</slotId><waferId>ZA0162T.04</waferId></slotList><slotList><slotId>005</slotId><waferId>ZA0162T.05</waferId></slotList></cSlotInfo></destnCarrier></sorterJobList><lotInfo><lotId>UR0ER6</lotId><CarrierID>C00666</CarrierID><lotType>Material</lotType><trackingUnit>Lot</trackingUnit><productId>SPAW000001.0</productId><stageId>00146000M1-WAT1</stageId><planId/><planType/><subPlanId/><subPlanType/><stepSeq/><stepId/><stepType/><stepDesc/><slotMap>0000000000000000011110000</slotMap><ppParameter/><isMonitor>N</isMonitor><processState>WaitForConsume</processState><processingStatus>Inventory</processingStatus><isEngJIP>F</isEngJIP><lotPriority>1</lotPriority><lotStartDate>2022-06-01 14:00:00</lotStartDate><lotContaminationLevel>3</lotContaminationLevel><carrierContaminationFlag>C</carrierContaminationFlag><angle/><mesBatchId>MESBatchID1111</mesBatchId><jobPrepOPId>User123</jobPrepOPId><jobPrepType>Auto</jobPrepType><jobInTime>2022-10-20 15:15:30</jobInTime><lotStartTime>2022-10-20 16:30:33</lotStartTime><lotEndTime>2022-10-20 16:50:33</lotEndTime><jobOutTime>2022-10-20 16:57:38</jobOutTime></lotInfo><lotInfo><lotId>UR0ER7</lotId><CarrierID>C00674</CarrierID><lotType>Material</lotType><trackingUnit>Lot</trackingUnit><productId>SPAW000001.0</productId><stageId>00146000M1-WAT1</stageId><planId/><planType/><subPlanId/><subPlanType/><stepSeq/><stepId/><stepType/><stepDesc/><slotMap>0000001111111111111100000</slotMap><ppParameter/><isMonitor>N</isMonitor><processState>WaitForConsume</processState><processingStatus>Inventory</processingStatus><isEngJIP>F</isEngJIP><lotPriority>1</lotPriority><lotStartDate>2022-06-01 14:00:00</lotStartDate><lotContaminationLevel>3</lotContaminationLevel><carrierContaminationFlag>C</carrierContaminationFlag><angle/><mesBatchId>MESBatchID1111</mesBatchId><jobPrepOPId>User123</jobPrepOPId><jobPrepType>Auto</jobPrepType><jobInTime>2022-10-20 15:15:30</jobInTime><lotStartTime>2022-10-20 16:30:33</lotStartTime><lotEndTime>2022-10-20 16:50:33</lotEndTime><jobOutTime>2022-10-20 16:57:38</jobOutTime></lotInfo><lotInfo><lotId>ZA0162T.01</lotId><CarrierID>C00682</CarrierID><lotType>ZL</lotType><trackingUnit>Lot</trackingUnit><productId>EH0650A06B.0</productId><stageId>APSORT11.0</stageId><planId>AEEH0650A02.0</planId><planType>Normal</planType><subPlanId>AEEH0650AGAN03.0</subPlanId><subPlanType>Normal</subPlanType><stepSeq>000100.10.0100</stepSeq><stepId>APSORT11.0</stepId><stepType>ScheduledSorter</stepType><stepDesc/><slotMap>0000001111111111111100000</slotMap><ppParameter/><isMonitor>N</isMonitor><processState>WaitForJobPrep</processState><processingStatus>Active</processingStatus><isEngJIP>F</isEngJIP><lotPriority>1</lotPriority><lotStartDate>2022-06-01 14:00:00</lotStartDate><lotContaminationLevel>3</lotContaminationLevel><carrierContaminationFlag>C</carrierContaminationFlag><angle/><mesBatchId>MESBatchID1111</mesBatchId><jobPrepOPId>User123</jobPrepOPId><jobPrepType>Auto</jobPrepType><jobInTime>2022-10-20 15:15:30</jobInTime><lotStartTime>2022-10-20 16:30:33</lotStartTime><lotEndTime>2022-10-20 16:50:33</lotEndTime><jobOutTime>2022-10-20 16:57:38</jobOutTime></lotInfo></msgBody>
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值