À§ÀÇ ¹®Á¦¿¡¼ ¿ì¸®°¡
flour >= 2°¡ falseÀÎ °ÍÀ» È®ÀÎ Çß´Ù¸é, ´ÙÀ½ ÁÖ°ÇÀÎ
sugar >= 2´Â ±»ÀÌ Æò°¡ÇÒ Çʿ䵵 ¾ø´Ù. sugar ÀÌ ÂüÀÌ°Ç °ÅÁþÀÌ°Ç °á°ú´Â ¹«Á¶°Ç °ÅÁþÀ̶ó´Â °É ¾Ë ¼ö Àձ⠶§¹®ÀÌ´Ù.
flour >= 4 && sugar >= 2
------------ ------------
false && dos not matter
--------------------
false
±×·¯¹Ç·Î ÇÁ·Î±×·¡¸Ó´Â ±»ÀÌ µÎ ¹øÀÇ ºñ±³ ¿¬»êÀ» ÇÒ ÇÊ¿ä ¾øÀÌ, ÇѹøÀÇ ¿¬»êÀ¸·Î ±× °á°ú¸¦ È®ÀÎÇϵµ·Ï ÇÁ·Î±×·¥À»
ÃÖÀûÈÇÒ ¼öµµ ÀÖ´Ù. µÎ¹ø ºñ±³ÇÏ´Â °Í º¸´Ù Çѹø ºñ±³Çϸé ÇÁ·Î±×·¥ÀÌ »¡¸® µ¹¾Æ°¥ °ÍÀÓÀº ¸í¹éÇÏ´Ù. ÀÌ¿¡ ´ëÇÑ ÁÖÁ¦´Â ³ªÁß¿¡ »ìÆìº¸µµ·Ï ÇÒ °ÍÀÌ´Ù.
´ÙÀ½Àº ¿ÏÀüÇÑ cookie ÇÁ·Î±×·¥ÀÌ´Ù.
// Cookie Ingredients Checker
//
import java.util.Scanner;
class CookieChecker
{
public static void main (String[] args)
{
// Declare a Scanner and two integer variables
Scanner scan = new Scanner( System.in );
int sugar, flour;
// get the number of cups of flour
System.out.println("How much flour do you have?");
flour = scan.nextInt();
// get the number of cups of sugar
System.out.println("How much sugar do you have?");
sugar = scan.nextInt();
// check that there are enough of both ingredients
if ( flour >= 4 && sugar >= 2 )
System.out.println("Enough for cookies!" );
else
System.out.println("sorry...." );
}
}
´Ù¾çÇÑ ¹æ½ÄÀ¸·Î ÇÁ·Î±×·¥À» Å×½ºÆ® Çϸé¼, && ¿¬»êÀÚ¸¦ È®½ÇÈ÷ ÀÌÇØÇϵµ·Ï ÇÏÀÚ.
ÀÚµ¿Â÷ ´ë¿© ÇÁ·Î±×·¥
´ç½ÅÀº ÀÚµ¿Â÷ ´ë¿© °ü·Ã ¾÷¹«¸¦ º¸°í ÀÖ´Ù. ÀÚµ¿Â÷¸¦ ´ë¿©Çϱâ À§Çؼ´Â ´ÙÀ½°ú °°Àº Á¶°ÇÀ» ¸¸Á·ÇØ¾ß ÇÑ´Ù.
- 21»ì ÀÌ»óÀÌ µÇ¾î¾ß ´ë¿©ÇÒ ¼ö ÀÖ´Ù.
- ÀÓÂ÷ÀÎÀº ¹Ýµå½Ã 10,000ºÒ ÀÌ»ó »ç¿ë°¡´ÉÇÑ ½Å¿ë Ä«µå°¡ ÀÖ¾î¾ß ÇÑ´Ù.
ÀÌ ÇÁ·Î±×·¥Àº ´ÙÀ½°ú °°´Ù. ÄíŰ ÇÁ·Î±×·¥°ú ¸Å¿ì ºñ½ÁÇØ º¸ÀδÙ.
// Rental Car Checker
//
import java.util.Scanner;
class RenterChecker
{
public static void main (String[] args)
{
// Declare a Scanner and two integer variables
Scanner scan = new Scanner( System.in );
int age, credit;
// get the age of the renter
System.out.println("How old are you?");
age = scan.nextInt() ;
// get the credit line
System.out.println("How much credit do you have?");
credit = scan.nextInt() ;
// check that both qualifications are met
if ( _________________ && __________________ )
System.out.println("Enough to rent this car!" );
else
System.out.println("Have you considered a bicycle?" );
}
}
¹®Á¦
¹ØÁÙÀ» ä¿ö¼ ¿ÏÀüÇÑ ÇÁ·Î±×·¥À¸·Î ¸¸µé¾î¶ó.
if (age >= 21 && credit >= 10000)
°í°´Àº ¾çÂÊ Á¶°ÇÀ» ¸ðµÎ ¸¸Á·½ÃÄѾ߸¸ ÇÑ´Ù. ¸ðµÎ ¸¸Á·½ÃÄÑ¾ß ÇϹǷÎ
&&¿¬»êÀÚ¸¦ ÀÌ¿ëÇØ¼ ¾çÂÊ ¸ðµÎ°¡
trueÀÎÁö¸¦ Æò°¡Çϰí ÀÖ´Ù.
´Ù¾çÇÑ °í°´À» ÃøÁ¤Çϱâ
24»ìÀÇ ½Å¿ë Ä«µå Çѵµ°¡ $0ÀÎ °í°´
age >= 21 && credit >= 10000
--------- ---------------
true false
---------------------------
false
19»ìÀ̰í $5000000ÀÇ ½Å¿ëÄ«µå Çѵµ¸¦ °¡Áø °í°´
age >= 21 && credit >= 10000
--------- ---------------
false true
---------------------------
false
¹üÀ§ ÃøÁ¤ Çϱâ
º¹½Ì¼±¼ö´Â ü±Þ¿¡ ¸Â´Â ¸ö¹«°Ô¸¦ À¯ÁöÇØ¾ß ÇÑ´Ù. À£ÅÍ±Þ ¼±¼öÀÇ °æ¿ì 136 ¿¡¼ 147 ÆÄ¿îµå »çÀÌÀÇ ¸ö¹«°Ô¸¦ À¯ÁöÇØ¾ß¸¸, ½ÃÇÕÀ» ÇÒ ¼ö ÀÖ´Ù. ¿©±â º¹½Ì¼±¼öÀÇ ¸ö¹«°Ô¸¦ ÃøÁ¤ÇÏ´Â ÇÁ·Î±×·¥ÀÌ ÀÖ´Ù. ÀÌ ÇÁ·Î±×·¥Àº º¹½Ì¼±¼öÀÇ ¸ö¹«°Ô¸¦ ÀԷ¹޾Ƽ, ½ÃÇÕÀ» ÇÒ ¼ö ÀÖ´ÂÁö¸¦ °Ë»çÇÑ´Ù.
// Ringside Weigh-in
//
// Boxer must weigh between 136 and 147 pounds
//
import java.util.Scanner;
class Ringside
{
public static void main (String[] args)
{
Scanner scan = new Scanner( System.in );
int weight;
// get the weight
System.out.print("How heavy is the boxer? ");
weight = scan.nextInt();
// check that the weight is within range
if ( )
System.out.println("In range!" );
else
System.out.println("Out of range." );
}
}