Introduction to Java Programming程式設計題5.29
/*
You rolled 2 + 1 = 3
You lose
You rolled 1 + 4 = 5
Point is 5
You rolled 5 + 1 = 6
You rolled 5 + 1 = 6
You rolled 1 + 5 = 6
You rolled 6 + 3 = 9
You rolled 1 + 4 = 5
You win
You rolled 5 + 2 = 7
You win
You rolled 5 + 4 = 9
Point is 9
You rolled 4 + 3 = 7
You lose
You rolled 6 + 5 = 11
You win
*/
public class GameDice {
public static void main(String[] args) {
int a, b, sum1 = 0, count = 0, sum2 = 0;
while (true) {
a = (int)(Math.random() * 6 + 1);
b = (int)(Math.random() * 6 + 1);
sum1 = a + b;
System.out.println("You rolled " + a + " + " + b + " = " + sum1);
if (count++ == 0) {
if (firstDice(sum1)) {
sum2 = sum1;
continue;
}
else
break;
}
if (isWin(sum1, sum2))
break;
}
}
public static boolean firstDice(int sum) {
if (sum == 3 || sum == 12)
System.out.println("You lose" );
else if (sum == 7 || sum == 11)
System.out.println("You win");
else {
System.out.println("Point is " + sum);
return true;
}
return false;
}
public static boolean isWin(int sum1, int sum2) {
if (sum2 == sum1)
System.out.println("You win");
else if (sum1 == 7)
System.out.println("You lose");
else
return false;
return true;
}
}
相關推薦
Introduction to Java Programming程式設計題5.29
/* You rolled 2 + 1 = 3 You lose You rolled 1 + 4 = 5 Point is 5 You rolled 5 + 1 = 6 You rolled 5
Introduction to Java Programming程式設計題5.32
/* You rolled 5 + 5 = 10 Point is 10 You rolled 6 + 2 = 8 Point is 8 You rolled 5 + 2 = 7 You lose 1******************** You rolled
Introduction to Java Programming程式設計題8.13
/* Enter the number of rows and columns of the array: 3 4 Enter the array: 11 33.5 88.1 4 0 -2.2 -10
[Java] Introduction to Java Programming 筆記 Chapter 9. 物件和類
如果一個檔案含有兩個類,只有一個類可以為public,public 類和檔名同名,但此檔案編譯後,將生成兩個.class 檔案,一個類對應一個class檔案。 匿名物件:建立一個物件,但是並不將其引用賦給變數 new Circle(); // or Sy
Udacity cs344Unit 5-Introduction to Parallel Programming筆記(超詳細,CUDA,並行,GPU)
1.優化的等級 2.應該有一個系統化的優化過程(類似於一個優化的週期,缺少哪一步效果都不好) 在真實資料集上跑很重要 不要“真空”優化 別忘了 思考你到底想實現啥 和在現實世界中執行收到反饋 3.具體過程 a.分析
Brief introduction to Java String Split 【簡單介紹下Java String Split】
a-z include cte eve class some sim string arr Split is a common function in Java. It split a full string to an array based on delimeter.
2018.09.22 上海大學技術分享 - An Introduction To Go Programming Language
針對 Language 社區 相對 基礎語法 ref 同學 master tree 老實說筆者學習 Go 的時間並不長,積澱也不深厚,這次因緣巧合,同組的同事以前是上海大學的開源社區推動者之一,同時我們也抱著部分宣傳公司和技術分享的意圖,更進一步的,也是對所學做一個總結,所
java介面程式設計題
1、建立Person介面(即“人”),它有setData()和getData()方法對“人”屬性name、sex和birthday賦值和獲得這些屬性組成的字串資訊。建立類Student實現Person介面,並對自己的“學生”屬性的成員變數sID、speciality設定值和獲得它們值所組成的字串
CSCI 1300 Introduction to Computer Programming
CSCI 1300作業代做、代寫TA/CA留學生作業、代做C/C++程式作業、C/C++課程設計作業代寫CSCI 1300 Introduction to Computer ProgrammingInstructor: FlemingHomework 9: Choose Project, Meet with
java資料庫程式設計(5) 使用preparedStatement
preparedStatement是一種帶有佔位符(?)的sql語句,它可以將預編譯的sql語句儲存起來,然後可以使用這個預編譯好的sql語句多次高效地執行傳入具體引數的sql語句。 例如執行 insert into table xxx values(1); insert i
Java基礎程式設計題—1
1.編寫一個圓類Circle,該類擁有:...,並列印輸出 package test; public class Circle { protected double radius; public Circle(){ radius = 0; } public Circle(
【搞定Java併發程式設計】第29篇:Executor 框架詳解
上一篇:Java中的執行緒池詳解 本文目錄: 1、Executor 框架簡介 1.1、Executor 框架的兩級排程模型 1.2、Executor 框架的結構與成員 2、ThreadPoolExecutor 詳解 2.1、FixedThreadPool 2.2、Sing
PAT-基礎程式設計題-5-38 數列求和-加強版
5-38 數列求和-加強版 (20分) 給定某數字AAA(1≤A≤91\le A\le 91≤A≤9)以及非負整數NNN(0≤N≤1000000\le N\le 1000000≤N≤100000),求數列之和S=A+AA+AAA+⋯+AA⋯AS = A +
java面試程式設計題(二叉樹相關)
題目: 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。 二叉樹結構為
Udacity cs344Unit 4-Introduction to Parallel Programming筆記(超詳細,CUDA,並行,GPU)
1.啥是緊密(compact):這裡的壓縮指的是過濾,filter,過濾出一個子集,也就是隻留我們想要的(比如一把撲克牌裡的方片) (只計算我們關心的物件才更有意義,計算代價較小,需要更少空間) 2. 3.密集運算好一點?為啥 第一個稀疏運算要啟動52個
Udacity cs344Unit 3-Introduction to Parallel Programming筆記(超詳細,CUDA,並行,GPU)
1.課程目標 如何分析GPU演算法的速度和效率(speed and efficiency) 三個新的基本演算法:歸約,掃描和直方圖(reduce,scan and histogram) 2. 3.再看之前講的例子 考慮兩件事
Java日期程式設計題
題目描述:計算從今天算起,150天之後是幾月幾號,並格式化成xxxx年xx月x日的形式打印出來。 提示:呼叫Calendar類的add方法計算150天之後的日期 呼叫Calendar類的getTim
Java基礎程式設計題(API階段測試)(答案)
第一題(程式設計題: 15分 )(答案) import java.util.Scanner; public class Test1 { /** * 模擬登入,給三次機會,並提示還有幾
java邏輯程式設計題
以下程式分析是老師給出的提示,解答思路是自己的思考 1、題目:有一對兔子,從出生後第三個月起每個月都升一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少 解答思路:兔子狀態分為newBorn(新生),oneM(一個月之後),adult
Java簡單程式設計題訓練一
字串反轉 import java.util.*; public class Main{ public static void main(String[] args){ Scanner in =new Scanner(System.in);