คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 3
ข้อ 1 การใส่ +1 คือการต้องการให้สุ่มตั้งแต่ 1-10 ครับ ถ้าไม่ใส่ +1 การสุ่มจะได้ 0-9 หากแก้ไขเป็น int nummer = random.nextInt(11); จะได้การสุ่มเลข 0 - 10
ข้อ 2 ดูจากโปรแกรม raten คือตัวเช็คว่า ใส่เลขสุ่มตรงกับเลขที่โปรแกรมสุ่มหรือไม่ จะเป็นเลข 0 หรือ -1 ก็ได้ แล้วที่หยุด Process ผมลองแล้วไม่เป็นนะครับ
ตัวอย่าง
import java.util.*;
class RandomNextInt_usr
{
public static void main(String[] args)
{
int raten = -1;
int intCountKey = 5;
boolean boolWinloser = false;
Random random = new Random();
//using scanner for getting input (numbers) from a user.
Scanner scanner = new Scanner(System.in);
//genarating a random number between 1-10
int nummer = random.nextInt(10)+1;
//Loop until user can find a right number
while (intCountKey > 0){
System.out.print("\n Please enter numbers ?: ");
raten = scanner.nextInt();
if (raten != nummer) {
System.out.print("Incorrect !! ");
}
else{
boolWinloser = true;
intCountKey = 0;
}
intCountKey--;
}
if(boolWinloser){
System.out.print("\n\n You Winner !!\n");
}
else{
System.out.print("\n\n Answer : " + nummer +"\n");
}
}
}
ข้อ 2 ดูจากโปรแกรม raten คือตัวเช็คว่า ใส่เลขสุ่มตรงกับเลขที่โปรแกรมสุ่มหรือไม่ จะเป็นเลข 0 หรือ -1 ก็ได้ แล้วที่หยุด Process ผมลองแล้วไม่เป็นนะครับ
ตัวอย่าง
import java.util.*;
class RandomNextInt_usr
{
public static void main(String[] args)
{
int raten = -1;
int intCountKey = 5;
boolean boolWinloser = false;
Random random = new Random();
//using scanner for getting input (numbers) from a user.
Scanner scanner = new Scanner(System.in);
//genarating a random number between 1-10
int nummer = random.nextInt(10)+1;
//Loop until user can find a right number
while (intCountKey > 0){
System.out.print("\n Please enter numbers ?: ");
raten = scanner.nextInt();
if (raten != nummer) {
System.out.print("Incorrect !! ");
}
else{
boolWinloser = true;
intCountKey = 0;
}
intCountKey--;
}
if(boolWinloser){
System.out.print("\n\n You Winner !!\n");
}
else{
System.out.print("\n\n Answer : " + nummer +"\n");
}
}
}
แสดงความคิดเห็น
ขอถามนัก Programmer (Java)
แต่ยังไม่เข้าใจดีตรงบางช่วงค่ะ โค้ดตัวอย่าง
Random random = new Random();
//using scanner for getting input (numbers) from a user.
Scanner scanner = new Scanner(System.in);
//genarating a random number between 1-10
int nummer = random.nextInt(10)+1;
int raten = -1;
//Loop until user can find a right number
while (raten != nummer) {
System.out.print("\nAn Welche Zahl denken Sie?: ");
raten = scanner.nextInt();
if (raten<nummer){
System.out.print("Die Zahl ist zu klein. Versuchen Sie es nochmal! ");
# raten ตัวนี้เราตั้งเป็น การกรอกตัวเลขสุ่มของผู้เล่น
คำถามแรก int nummer ) random.nextlnt(10)+1;
ตรงช่วงบรรทัดนี้ สงสัยค่ะว่าทำไมถึงมี +1 เข้ามา
ตามความเข้าใจคือ int nummer = random.nextInt(10)+1; ตัวเลขที่ระบบจะสุ่มมีจำนวน 0-9 การ +1 เข้าไปเพื่อที่ให้ระบบสุ่มตัวเลขจนถึง 10
หากแก้ไข ตัวเลขจาก เป็น int nummer = random.nextInt(11); แทนได้ไหมคะ แล้วระบบจะสุ่มจำนวนตัวเลข 0-10 รึป่าวคะ
คำถามที่สอง int raten = -1;
ทำไหมถึงใส่ -1 คะ ลองใส่ 0 แล้ว ระบบหยุด Process ไม่แสดงข้อความในบรรทัดถัดไป
คำถามสุดท้าย พอดีว่าอยากจะทำโปรแกรมเกมสุ่มทายเลข ซึ่งอยากให้เลขที่สุ่มได้ อยู่ที่จำนวน 1-10 และหากเป็นไปได้ ควรกำหนดขอบเขตตัวเลขยังไงเพื่อไม่ให้เริ่มที่ 0 คะ ..... ซึ่งการทายนี้ ผู้เล่นสามารถทายเลขสุ่มได้จนกว่าจะทายถูก ซึ่งใช้โค้ด while(!=) เข้ามาช่วย
*****แต่ถ้าอยาก กำหนดครั้ง ซึ่งผู้เล่นสามารถสุ่มทายตัวเลขได้สูงสุด 5 ครั้ง เท่านั้นควรใช้ โค้ดตัวไหนคะ
รบกวนผู้รู้ช่วยอธิบายทีค่ะ
ปล.ขออภัยด้วยนะคะ หากเขียนภาษาไทยสับสน