home > 写経編 > 柴田望洋『明解C言語 入門編』 > 3. プログラムの流れと分岐 >

ForNext

Only Do What Only You Can Do

022. 読み込んだ月の季節を表示 (論理演算子)

VBScript

JScript

Perl

更新日 : 2010.10.18
print "何月ですか:";
chomp($month = <STDIN>);

if (3 <= $month && $month <= 5)
{
    printf "春です。\n";
}
elsif (6 <= $month && $month <= 8)
{
    printf "夏です。\n";
}
elsif (9 <= $month && $month <= 11)
{
    printf "秋です。\n";
}
elsif($month == 1 || $month == 2 || $month == 12)
{
    printf "冬です。\n";
}
else
{
    printf "そんな月はありませんよ!!\n";
}
L:\>perl lesson_03_022.pl
何月ですか:5
春です。

L:\>perl lesson_03_022.pl
何月ですか:8
夏です。

PHP

更新日 : 2010.11.03
<?php
print "何月ですか:";

$fp = fopen("php://stdin", "r");
$month = fgets($fp);
fclose($fp);

if (3 <= $month && $month <= 5)
    echo "春です。\n";
elseif (6 <= $month && $month <= 8)
    echo "夏です。\n";
elseif (9 <= $month && $month <= 11)
    echo "秋です。\n";
elseif($month == 1 || $month == 2 || $month == 12)
    echo "冬です。\n";
else
    echo "そんな月はありませんよ!!\n";
?>
L:\>php lesson_03_022.php
何月ですか:5
春です。

L:\>php lesson_03_022.php
何月ですか:8
夏です。

Python

更新日 : 2010.11.17
# coding: Shift_JIS

month = int(raw_input("何月ですか:"))

if (3 <= month) and (month <= 5):
    print "春です。"
elif (6 <= month) and (month <= 8):
    print "夏です。"
elif (9 <= month) and (month <= 11):
    print "秋です。"
elif (month == 1) or (month == 2) or (month == 12):
    print "冬です。"
else:
    print "そんな月はありませんよ!!"
N:\>python lesson_03_022.py
何月ですか:5
春です。

N:\>python lesson_03_022.py
何月ですか:8
夏です。

Ruby

更新日 : 2010.11.01
print "何月ですか:"
month = STDIN.gets.chomp.to_i

if (3 <= month && month <= 5)
    puts "春です。"
elsif (6 <= month && month <= 8)
    puts "夏です。"
elsif (9 <= month && month <= 11)
    puts "秋です。"
elsif(month == 1 || month == 2 || month == 12)
    puts "冬です。"
else
    puts "そんな月はありませんよ!!"
end

if ((3..5) === month)
    puts "春です。"
elsif ((6..8) === month)
    puts "夏です。"
elsif ((9..11) === month)
    puts "秋です。"
elsif((1..12) === month)
    puts "冬です。"
else
    puts "そんな月はありませんよ!!"
end
L:\>ruby  l:\lesson_03_022.rb
何月ですか:5
春です。
春です。

L:\>ruby  l:\lesson_03_022.rb
何月ですか:8
夏です。
夏です。

PowerShell

Scala

F#

C

更新日 : 2010.10.08
#include <stdio.h>
int main(int argc, char* argv[])
{
    int month;

    printf("何月ですか:");
    scanf("%d", &month);

    if (3 <= month && month <= 5)
        puts("春です。");
    else if (6 <= month && month <= 8)
        puts("夏です。");
    else if (9 <= month && month <= 11)
        puts("秋です。");
    else if (month == 1 || month == 2 || month == 12)
        puts("冬です。");
    else
        puts("そんな月はありませんよ!!");

    return 0;
}
R:\>lesson022\project1.exe
何月ですか:5
春です。

R:\>lesson022\project1.exe
何月ですか:8
夏です。

C++

C++Builder

VC++

C#

Java

更新日 : 2010.11.05
import java.util.Scanner;

class Lesson022 {
    public static void main(String[] args) {
        System.out.print("何月ですか:");
        Scanner stdIn = new Scanner(System.in);
        int month = stdIn.nextInt();

        if (3 <= month && month <= 5)
            System.out.println("春です。");
        else if (6 <= month && month <= 8)
            System.out.println("夏です。");
        else if (9 <= month && month <= 11)
            System.out.println("秋です。");
        else if (month == 1 || month == 2 || month == 12)
            System.out.println("冬です。");
        else
            System.out.println("そんな月はありませんよ!!");
    }
}
L:\>java Lesson022
何月ですか:5
春です。

L:\>java Lesson022
何月ですか:8
夏です。

Objective-C

D

VB

VB.NET

Delphi

更新日 : 2010.09.24
program Project1;

{$APPTYPE CONSOLE}

uses
    SysUtils;
var
    month: Integer;
begin
    write('何月ですか:');
    read(month);

    if (3 <= month) and (month <= 5) then
        writeln('春です。')
    else if (6 <= month) and (month <= 8) then
        writeln('夏です。')
    else if (9 <= month) and (month <= 11) then
        writeln('秋です。')
    else if (month = 1) or (month = 2) or (month = 12) then
        writeln('冬です。')
    else
        writeln('そんな月はありませんよ!!');
end.
S:\>lesson022\project1.exe
何月ですか:5
春です。

S:\>lesson022\project1.exe
何月ですか:8
夏です。

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system