java迴圈練習:石頭剪刀布

來源:互聯網
上載者:User

標籤:java   迴圈練習   石頭   

package practiceGO;import java.util.Scanner;/* * 8.用迴圈方式實現和電腦玩猜拳的程式 (設定勝出條件--輸3次或贏3次即退出) * 0-石頭 1-布 2-剪刀 */public class Cto {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int i = 0;int j = 0;while(true){System.out.println("請出拳:0-石頭 1-剪刀 2-布");int perNum = sc.nextInt();if (perNum>2 || perNum<0) {System.out.println("輸入錯誤");continue;}int sysNum = ((int)(Math.random()*10))%3;System.out.println("電腦出拳"+sysNum);if (perNum-sysNum==-1 || perNum-sysNum==2) {i++;System.out.println("玩家勝"+i+"次");}else if(perNum-sysNum==1 || perNum-sysNum==-2){j++;System.out.println("電腦勝"+j+"次");}else{System.out.println("平局。");}if (i==3) {System.out.println("玩家勝");break;}else if (j==3) {System.out.println("電腦勝");break;}}}}

測試結果:

請出拳:0-石頭 1-剪刀 2-布0電腦出拳0平局。請出拳:0-石頭 1-剪刀 2-布0電腦出拳2電腦勝1次請出拳:0-石頭 1-剪刀 2-布0電腦出拳1玩家勝1次請出拳:0-石頭 1-剪刀 2-布0電腦出拳1玩家勝2次請出拳:0-石頭 1-剪刀 2-布0電腦出拳1玩家勝3次玩家勝
請出拳:0-石頭 1-剪刀 2-布1電腦出拳0電腦勝1次請出拳:0-石頭 1-剪刀 2-布1電腦出拳0電腦勝2次請出拳:0-石頭 1-剪刀 2-布1電腦出拳2玩家勝1次請出拳:0-石頭 1-剪刀 2-布1電腦出拳2玩家勝2次請出拳:0-石頭 1-剪刀 2-布1電腦出拳0電腦勝3次電腦勝


java迴圈練習:石頭剪刀布

聯繫我們

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