標題:
[回家作業] 猜數字修正版
[打印本頁]
作者:
周政輝
時間:
2016-11-12 12:14
標題:
[回家作業] 猜數字修正版
修正上課所提到BUG
假設一開始範圍是0-100
當輸入50 如果太小 範圍會是 50-100
但這時候如果輸入20
那各位的程式會從20-100
也就是說 自己所輸入的數值如果 大於 或是小於 範圍數值的話
就會產生BUG出現
請各位修正這個錯誤
package tw.mis.kuas.edu.tw;
import java.util.Scanner;
public class Msin {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
int max =99;
int min = 0;
int yournum = 0;
int count = 0;
int random_num = (int)(Math.random()*100+1);
Scanner scanner = new Scanner(System.in);
while(true)
{
count++;
System.out.print("Please input a number: ");
yournum = scanner.nextInt();
if(!(yournum<max && yournum>min))
{
System.out.println("Error 請輸入數值範圍內的數");
continue;
}
if(yournum==random_num)
{
System.out.println("You win");
System.out.println("共猜了"+count+"次");
}
else if(yournum<random_num)
{
System.out.println("Too Short");
System.out.println(yournum + "~" + max);
System.out.println("****************");
min = yournum;
}
else if(yournum>random_num)
{
System.out.println("Too Big");
System.out.println( min + "~" + yournum);
System.out.println("****************");
max = yournum;
}
}
}
}
複製代碼
作者:
王彥甯
時間:
2016-11-12 12:36
此帖僅作者可見
作者:
張健勳
時間:
2016-11-13 20:07
此帖僅作者可見
作者:
蔡季樺
時間:
2016-11-18 20:34
此帖僅作者可見
作者:
蔡庭豪
時間:
2016-11-19 09:56
此帖僅作者可見
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2