順次処理
#print(100)
#print(100)
#print(100)
#print(100)
条件分岐
#if
score = int(input(“Score? “))
if score > 80:
print(“OK!”)
print(“Good Job”)
print(“End of program …”)
score = int(input(“Score? “))
if score > 80:
print(“OK!”)
print(“Good Job”)
print(“End of program …”)
class Pokemon {
public:
// コンストラクタ
Pokemon(const std::string& nickname) {
this->nickname_ = nickname;
// nickname_ = nickname; と省略できる
}
// デストラクタ
virtual ~Pokemon() {
}
virtual void attack() = 0;
protected:
std::string nickname_;
};
class Pikachu : public Pokemon {
public:
Pikachu(const std::string& nickname) : Pokemon(nickname) {
}
virtual ~Pikachu() {
}
void attack() override {
std::cout << nickname_ << “の十万ボルト!” << std::endl;
}
};
class Zenigame : public Pokemon {
public:
Zenigame(const std::string& nickname) : Pokemon(nickname) {
}
virtual ~Zenigame() {
}
void attack() override {
std::cout << nickname_ << “のハイドロポンプ!” << std::endl;
}
};
int main() {
int i;
Pokemon* monsters[2];
monsters[0] = new Pikachu(“サトシのピカチュウ”);
monsters[1] = new Zenigame(“サトシのゼニガメ”);
std::cin >> i;
if (i >= 0 && i < 2) { monsters[i]->attack();
}
delete monsters[0];
delete monsters[1];
}
initial_balance = int(input(“Initial Balance? “))
RATE = 1.1
print(f”Year 0: {initial_balance:,.2f}”)
print(f”Year 1: {initial_balance * RATE:,.2f}”)
print(f”Year 2: {initial_balance * RATE * RATE:,.2f}”)
class TodoList:
def init(self):
self.tasks = []
def add_task(self, task):
self.tasks.append(task)
def remove_task(self, task):
if task in self.tasks:
self.tasks.remove(task)
else:
print("タスクが見つかりません。")
def list_tasks(self):
if not self.tasks:
print("ToDoリストは空です。")
else:
print("ToDoリスト:")
for idx, task in enumerate(self.tasks, start=1):
print(f"{idx}. {task}")
def main():
todo_list = TodoList()
while True:
print(“\n操作を選択してください:”)
print(“1. タスクを追加”)
print(“2. タスクを削除”)
print(“3. タスク一覧を表示”)
print(“4. 終了”)
choice = input(“選択: “)
if choice == '1':
task = input("タスクを入力してください: ")
todo_list.add_task(task)
print("タスクが追加されました。")
elif choice == '2':
task_idx = int(input("削除するタスクの番号を入力してください: "))
if 1 <= task_idx <= len(todo_list.tasks):
task_to_remove = todo_list.tasks[task_idx - 1]
todo_list.remove_task(task_to_remove)
print("タスクが削除されました。")
else:
print("無効な番号です。")
elif choice == '3':
todo_list.list_tasks()
elif choice == '4':
print("プログラムを終了します。")
break
else:
print("無効な選択です。")
if name == “main“:
main()
int main()
{
std::cout << “Hello, World!\n”;
}
fname = “Taro”
lname = “Yamada”
age = 32
separator = “=-“
print(separator * 10)
print(“I am {}{},{} years old!”.format(fname, lname,age))
print(f” {fname}{lname},{age} years old!”)
print(separator * 10)
‘use strict’;
{
function printNumberTwice(num: number): void {
console.log(num);
console.log(num);
}
function printStringTwice(str: string): void {
console.log(str);
console.log(str);
}
function printTwice(value: T): void{
console.log(value);
console.log(value);
}
printTwice(10);
printTwice(‘OK’);
}
‘use strict’;
{
let keyword: string | number | boolean;
keyword = ‘milk’;
keyword = 50;
keyword = true;
}
using System;
struct Simple
{
public int Number;
public string Name;
public Simple(int n,string s)
{
Number = n;
Name = s;
}
}
class MainClass
{
static void Main()
{
Simple s1 = new Simple();
Console.WriteLine(s1.Number);
Console.WriteLine(s1.Name);
Simple s2 = new Simple(1, "testname");
Console.WriteLine(s2.Number);
Console.WriteLine(s2.Name);
Simple ss;
}
}
using System;
namespace array3
{
internal class Program
{
static void Main(string[] args)
{
string[] weekDays =
{ “sun”, “Mon”, “Tue” , “Wed”, “Thu”, “Fri” , “Sat”};
for(int i = 0; i < weekDays.Length; i++)
{
Console.WriteLine(weekDays[i]);
}
foreach(string s in weekDays)
{
Console.WriteLine(s);
}
int[] a = { 1, 2, 3 };
int sum = 0;
for(int i = 0; i < a.Length; i++)
{
sum += a[i];
}
Console.WriteLine(sum);
}
}
}