ประเภทบูลีน

มีอยู่
8
งาน
Java ไวยากรณ์,  ระดับบทเรียน
ล็อค
Describing numbers
Suppose we need to separate numbers according to some criteria. For example, based on whether they are even or odd, or based on the number of digits. Have you got a picture of that in your mind? And now for the work: we'll write a program that describes the number entered by the user (it must be in the range from 1 to 999). The description will indicate whether the number is even or odd, and how many digits it has.
4
งาน
Java ไวยากรณ์,  ระดับบทเรียน
ล็อค
Positive number
At the "Optimist" school, the scale used to assess knowledge has neither an upper nor lower bound. This pleases young gifted people (wouldn't you be happy to receive a score of a bazillion for a presentation?). All scores above zero are considered good. Scores below zero are bad. Students must pass final exams in three subjects. We need to enter three scores from the keyboard, and display the number of positive scores.
4
งาน
Java ไวยากรณ์,  ระดับบทเรียน
ล็อค
Positive and negative numbers
Data analysis starts with a simple concept: we need to understand what data matters and should be used, and what data should be discarded. Let's solve a simple data analysis problem. We enter three integers from the keyboard, then we'll analyze them and display the number of positive and negative numbers in the source set.
2
งาน
Java ไวยากรณ์,  ระดับบทเรียน
ล็อค
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and earn a little dark matter. What's more, you combat laziness.

ตัวอย่างการบรรยายพร้อมผู้ให้คำปรึกษาซึ่งเป็นส่วนหนึ่งของหลักสูตร Codegym University ลงทะเบียนสำหรับหลักสูตรเต็ม


"สวัสดี Amigo ให้ฉันบอกคุณเกี่ยวกับประเภทข้อมูลใหม่บูลีนตัวแปรประเภทนี้สามารถรับได้เพียงสองค่าเท่านั้น: จริงและเท็จ "

"เราจะใช้มันอย่างไร"

"ประเภทนี้ถูกใช้โดยปริยายในหลายๆ ที่เช่นเดียวกับการบวกใดๆ ที่สร้างตัวเลข ผลลัพธ์ของการเปรียบเทียบใดๆ ก็คือบูลีนนี่คือตัวอย่างบางส่วน:"

รหัส คำอธิบาย
1
boolean m;
นิพจน์ทั้งสองนี้มีค่าเท่ากัน ค่าเริ่มต้นของ ตัวแปร บูลีนเป็นเท็จ
2
boolean m = false;
3
if (a > b)
    System.out.println(a);
ผลลัพธ์ของการเปรียบเทียบ ( จริงหรือเท็จ)จะถูกกำหนดให้กับตัวแปร mเงื่อนไขจะเป็นไปตามถ้านิพจน์ประเมินเป็นtrue
4
boolean m = (a > b);
if (m)
    System.out.println(a);
5
boolean m = (a > b);
if (m == true)
    System.out.println(a);
ไม่จำเป็นต้องเปรียบเทียบตัวแปรตรรกะ ( บูลีน ) กับจริงหรือเท็จ ผลลัพธ์ของการเปรียบเทียบจะเป็นบูลีนที่ตรงกับตัวแปรอื่น ตัวอย่างเช่นtrue == true ประเมินเป็น true; true == false ประเมินเป็น false
6
boolean m = (a > b);
if (m)
    System.out.println(a);

"ตัวอย่างเพิ่มเติม:"

รหัส คำอธิบาย
1
public boolean isALessThanB (int a, int b)
{
    if (a < b)
        return true;
    else
        return false;
}
วิธีนี้ตรวจสอบว่าจำนวน a น้อยกว่าจำนวน b

ต่อไปนี้เป็นการเปรียบเทียบที่เท่าเทียมกันสี่รายการ อันสุดท้ายกระชับและถูกต้องที่สุด พยายามใช้เครื่องหมายย่อ เสมอ

2
public boolean isALessThanB (int a, int b)
{
   boolean m = (a < b);
    if (m)
        return true;
    else
        return false;
}
3
public boolean isALessThanB (int a, int b)
{
    boolean m = (a < b);
    return m;
}
4
public boolean isALessThanB (int a, int b)
{
    return a < b;
}

"ถ้าฉันต้องการเขียน 0<a<b"

"Java ไม่มีตัวดำเนินการเปรียบเทียบที่รับตัวถูกดำเนินการสามตัว ดังนั้นคุณจะต้องทำสิ่งนี้: (0<a) AND (a<b) "

"ฉันเขียนคำว่า AND หรือไม่"

"เดี๋ยวก่อน ฉันจะอธิบายให้ฟัง Java มีตัวดำเนินการเชิงตรรกะสามตัว: AND , ORและNOTคุณสามารถใช้มันเพื่อสร้างเงื่อนไขที่มีความซับซ้อนต่างกันได้ คุณสามารถใช้ตัวดำเนินการเหล่านี้กับ นิพจน์ บูลีน เท่านั้น ดังนั้น คุณไม่สามารถเขียน( a+1) AND (3)แต่(a>1)AND (a<3)ก็โอเค"

" ตัวดำเนินการ NOT ไม่เป็นเอกภาพ: มีผลกับนิพจน์ทางด้านขวาเท่านั้น มันเหมือนกับเครื่องหมายลบหน้าจำนวนลบมากกว่าเครื่องหมายคูณระหว่างตัวเลขสองตัว"

"คุณสามารถดำเนินการต่างๆ กับ ตัวแปร บูลีน (ตรรกะ)"

"เช่นอะไร?"

"มาดูกัน:"

ตัวดำเนินการเชิงตรรกะ สัญกรณ์ Java การแสดงออก ผลลัพธ์
และ && จริง  &&  จริง จริง
จริง &&  เท็จ เท็จ
เท็จ  &&  จริง เท็จ
เท็จ && เท็จ เท็จ
หรือ || จริง || จริง จริง
จริง || เท็จ จริง
เท็จ || จริง จริง
เท็จ || เท็จ เท็จ
ไม่ ! ! จริง เท็จ
! เท็จ จริง
ชุดค่าผสมและนิพจน์ทั่วไป ม && !ม เท็จ
ม || !ม จริง
! (ก && ข) !a || !ข
! (ก || ข) !a && !b

"คุณช่วยยกตัวอย่างเพิ่มเติมให้ฉันได้ไหม"

"แน่นอน:"

สัญกรณ์ Java สัญกรณ์ตรรกะ
(ก<3) && (ก>0) (ก < 3) และ (ก>0)
(ก>10) || (ก<100) (ก>10) หรือ (ก<100)
(ก<b) && (!(ค<=d)) (a<b) และ (ไม่ใช่ (c<=d))

"ตอนนี้ทำภารกิจบางอย่าง"

4
งาน
Java ไวยากรณ์,  ระดับบทเรียน
ล็อค
Labels and numbers
Not all labels are equally useful, but sometimes a lack of labels results in chaos. Let's do a task that dishes out labels: use the keyboard to enter an integer, and make the program indicate whether the number is negative or positive (or zero), and even or odd. Why do we need this? Just because, for practice. After all, we're here to learn!
ความคิดเห็น
  • เป็นที่นิยม
  • ใหม่
  • เก่า
คุณต้องลงชื่อเข้าใช้เพื่อแสดงความคิดเห็น
หน้านี้ยังไม่มีความคิดเห็นใด ๆ