標題:
[作業]猜數字遊戲(改編)
[打印本頁]
作者:
李泳霖
時間:
2022-2-12 15:24
標題:
[作業]猜數字遊戲(改編)
請修改猜數字遊戲,如圖
[attach]12805[/attach]
[attach]12806[/attach]
作者:
盧禹廷
時間:
2022-2-12 16:45
本帖最後由 盧禹廷 於 2022-2-19 14:25 編輯
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int ans=(int)(Math.random()*99+1);
int n,a=1,b=99,x=0;
while(true)
{
x++;
System.out.println("猜一個"+a+"~"+b+"之間的數字:");
n=sc.nextInt();
if(n>ans)
{
if(n>b || n<a)
{
System.out.println("問號");
continue;
}
else
{
System.out.println("你猜得太大了");
b=n-1;
}
}
else if(n<ans)
{
if(n>b || n<a)
{
System.out.println("問號");
continue;
}
else
{
System.out.println("你猜得太小了");
a=n+1;
}
}
else
{
System.out.println("恭喜你猜對了");
System.out.println("你猜了"+x+"次");
break;
}
if(a==b)
{
System.out.println("太可惜了,答案是"+ans);
return;
}
}
}
}
複製代碼
作者:
秉宏1
時間:
2022-2-12 18:08
import java.util.Scanner;
public class CH01 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int ans=(int)(Math.random()*99+1);
int n,a=1,b=99,x=0;
while(true)
{
x++;
System.out.println("猜一個"+a+"~"+b+"之間的數字:");
n=sc.nextInt();
if(n>ans)
{
if(n>b || n<a)
{
System.out.println("問號");
continue;
}
System.out.println("你猜得太大了");
b=n-1;
}
else if(n<ans)
{
if(n>b || n<a)
{
System.out.println("問號");
continue;
}
System.out.println("你猜得太小了");
a=n+1;
}
else
{
System.out.println("恭喜你猜對了");
System.out.println("你猜了"+x+"次");
break;
}
if(a==b)
{
System.out.println("太可惜了,答案是"+ans);
return;
}
}
}
}
複製代碼
作者:
夏子甯
時間:
2022-2-19 10:22
本帖最後由 夏子甯 於 2022-2-19 14:19 編輯
import java.util.Scanner;
public class Ch01
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=1,b=99,ans,n=0;
int r=(int)(Math.random()*99+1);
while(true)
{
n++;
if(a==b)
{
System.out.println("可惜了,答案是"+r);
break;
}
System.out.println("猜一個"+a+"~"+b+"之間的數字:");
ans=sc.nextInt();
if(ans<r && ans>=a && ans<=b)
{
System.out.println("猜得太小!");
a=ans+1;
}
else if(ans>r && ans>=a && ans<=b)
{
System.out.println("猜得太大!");
b=ans-1;
}
else if(ans<a || ans>b)
{
System.out.println("問號?");
}
else
{
System.out.println("答對了!");
System.out.println("總共猜了"+n+"次");
break;
}
}
}
}
複製代碼
作者:
林佑豪
時間:
2022-2-19 14:26
import java.lang.System;
import java.util.Scanner;
public class Ch01
{
static Scanner s=new Scanner(System.in);
public static void main(String[] args)
{
int x,y,z,a,b;//x=答案、y=最低值、z=最高值、a=次數、b=答案
x=(int)(Math.random()*99+1);
y=1;
z=99;
a=0;
while(true)
{
a+=1;
if(y==z)
{
System.out.println("可惜了,答案是"+x);
break;
}
System.out.print("猜一個"+y+"~"+z+"之間的數字:");
b=s.nextInt();
if(b>x)
{
if(b>z)
{
System.out.println("問號!");
}
else
{
System.out.println("猜得太大了!");
z=b-1;
}
}
else if(b<x)
{
if(b<y)
{
System.out.println("問號!");
}
else
{
System.out.println("猜得太小了!");
y=b+1;
}
}
else
{
System.out.println("恭喜你猜對了!");
System.out.println("總共猜了"+a+"次!");
break;
}
}
}
}
複製代碼
作者:
林孟霆
時間:
2022-2-19 14:31
import java.util.Scanner;
public class Ch01
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=1,b=99,ans,n=0;
double r=(int)(Math.random()*99+1);
while(true)
{
n++;
if(a==b)
{
System.out.println("可惜了,答案是"+r);
break;
}
System.out.println("猜一個"+a+"~"+b+"之間的數字: ");
ans=sc.nextInt();
if(ans>r)
{
if(ans>b)
{
System.out.println("問號?!");
}else
{
System.out.println("猜得太大!");
b=ans-1;
}
}
else if(ans<r)
{
if(ans<a)
{
System.out.println("問號?!");
}else
{
System.out.println("猜得太小!");
a=ans+1;
}
}
else
{
System.out.println("答對了!");
break;
}
}
System.out.println("總共猜了"+n+"次");
}
}
複製代碼
作者:
蔡坤辰
時間:
2022-2-19 14:34
import java.util.Scanner;
public class Ch01
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=1,b=99,ans,n=0;
double r=(int)(Math.random()*99+1);
while(true)
{
n++;
if(a==b)
{
System.out.println("可惜了,答案是"+r);
break;
}
System.out.println("猜一個"+a+"~"+b+"之間的數:");
ans=sc.nextInt();
if(ans<r)
{
if(ans<a)
{
System.out.println("是在哈囉?");
}
else
{
System.out.println("猜得太小");
a=ans+1;
}
}
else if(ans>r)
{
if(ans>b)
{
System.out.println("是在哈囉?");
}
else
{
System.out.println("猜得太大");
b=ans-1;
}
}
else
{
System.out.println("答對了");
break;
}
}
System.out.println("總共猜了"+n+"次");
}
}
複製代碼
作者:
睿呈
時間:
2022-2-26 12:00
import java.util.Scanner;
import java.util.Random;
class Ch01
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int a=1, b=99,ans,n=0;//a= lowest value, b = highest value, ans is the variable we are trying to guess, n is the guesscount
int r=(int)(Math.random()*99+1); //value we are trying to guess
while(true)
{
n++;
if(a==b)//if the range for the answer is still the same you print the answer
{
System.out.println("Oof the correct answer is "+r);
break;
}
System.out.println("guess a value between "+a+"~"+b);
ans=sc.nextInt(); //input for the guessed number
if(ans<r)//
{
a=ans+1;
if(ans<a)
{
System.out.println("?! What");
}
else
{
System.out.println("The value you guessed was too small ");
a=ans+1;
}
}
else if(ans>r)//50>30
{
if(ans>b)
{
System.out.println("?! what");
}
else
{
System.out.println("the value is too big");
b=ans-1;
}
b=ans-1;
}
else
{
System.out.println("You got the answer right! ");
break;
}
}
System.out.println("You guessed in total of"+n+" times");;
}
}
複製代碼
作者:
鄞美旭
時間:
2022-2-28 20:45
import java.util.Scanner;
public class Ch01
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=1,b=99,ans,n=0;
double r=(int)(Math.random()*99+1);
while(true)
{
n++;
if(a==b)
{
System.out.println("可惜了,答案是"+r);
break;
}
System.out.print("猜一個"+a+"~"+b+"之間的數字: ");
ans=sc.nextInt();
if(ans<r)
{
if(ans<a)
{
System.out.println("問號!?");
}
else
{
System.out.println("猜得太小!");
a=ans+1;
}
}
else if(ans>r)
{
if(ans>b)
{
System.out.println("問號!?");
}
else
{
System.out.println("猜得太大!");
b=ans-1;
}
}
else
{
System.out.println("答對了!");
break;
}
}
System.out.println("總共猜了"+n+"次");
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2