def triple(num)
result = num * 3
result
end
num = 20
puts triple(10)
puts num
カテゴリー: programming
Java タスク管理
package sample;
import java.util.ArrayList;
import java.util.Scanner;
public class TaskManager {
private static ArrayList<Task> tasks = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
boolean running = true;
while (running) {
System.out.println("1. タスクの追加");
System.out.println("2. タスクの削除");
System.out.println("3. タスクの表示");
System.out.println("4. タスクの状態を変更");
System.out.println("5. 終了");
System.out.print("操作を選択してください: ");
int choice = scanner.nextInt();
scanner.nextLine(); // 改行を読み飛ばす
switch (choice) {
case 1:
addTask();
break;
case 2:
deleteTask();
break;
case 3:
displayTasks();
break;
case 4:
changeTaskStatus();
break;
case 5:
running = false;
break;
default:
System.out.println("無効な選択です。");
}
}
scanner.close();
}
private static void addTask() {
System.out.print("タスクの名前を入力してください: ");
String name = scanner.nextLine();
System.out.print("タスクの期限を入力してください (例: YYYY-MM-DD): ");
String deadline = scanner.nextLine();
System.out.print("タスクの優先度を入力してください (高/中/低): ");
String priority = scanner.nextLine();
tasks.add(new Task(name, deadline, priority));
System.out.println("タスクを追加しました。");
}
private static void deleteTask() {
if (tasks.isEmpty()) {
System.out.println("削除するタスクはありません。");
return;
}
System.out.println("削除するタスクを選択してください:");
displayTasks();
System.out.print("削除するタスクの番号を入力してください: ");
int index = scanner.nextInt();
scanner.nextLine(); // 改行を読み飛ばす
if (index < 1 || index > tasks.size()) {
System.out.println("無効な番号です。");
} else {
Task deletedTask = tasks.remove(index - 1);
System.out.println(deletedTask.getName() + " を削除しました。");
}
}
private static void displayTasks() {
if (tasks.isEmpty()) {
System.out.println("タスクはありません。");
} else {
System.out.println("現在のタスク:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
}
private static void changeTaskStatus() {
if (tasks.isEmpty()) {
System.out.println("タスクはありません。");
return;
}
System.out.println("状態を変更するタスクを選択してください:");
displayTasks();
System.out.print("状態を変更するタスクの番号を入力してください: ");
int index = scanner.nextInt();
scanner.nextLine(); // 改行を読み飛ばす
if (index < 1 || index > tasks.size()) {
System.out.println("無効な番号です。");
} else {
Task task = tasks.get(index - 1);
System.out.print("新しい状態を入力してください (未完了/進行中/完了): ");
String newStatus = scanner.nextLine();
task.setStatus(newStatus);
System.out.println(task.getName() + " の状態を " + newStatus + " に変更しました。");
}
}
}
class Task {
private String name;
private String deadline;
private String priority;
private String status = "未完了"; // デフォルトの状態は未完了
public Task(String name, String deadline, String priority) {
this.name = name;
this.deadline = deadline;
this.priority = priority;
}
public String getName() {
return name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "タスク名: " + name + ", 期限: " + deadline + ", 優先度: " + priority + ", 状態: " + status;
}
}
ruby 論理演算子
def dividable?(a, b)
# if b != 0
# if a % b == 0
# true
# else
# false
# end
# else
# false
# end
if b != 0 && a % b == 0
true
else
false
end
end
puts dividable?(10, 2) # true
puts dividable?(10, 3) # false
puts dividable?(10, 0)
ruby puts 真偽値を返すメソッド
def dividable?(a, b)
if a % b == 0
true
else
false
end
end
puts dividable?(10,2) # true
puts dividable?(10,3) #false
ruby メソッド
def show_ad
puts "---------"
puts "SALE! 50% OFF!"
puts "---------"
end
def show_content
puts "BREAKING NEWS!"
puts "Two baby pandas born at our Zoo!"
end
show_ad
show_content
show_ad
ruby caseで条件分岐
print "Score? "
score = gets.to_i
case score
when 100
puts "Super"
when 90..89
puts "A"
when 70...89
puts "B"
else
puts"C"
end
ruby メモアプリ
require 'date'
class Memo
attr_accessor :title, :content, :category, :created_at
def initialize(title, content, category)
@title = title
@content = content
@category = category
@created_at = DateTime.now
end
def to_s
"#{@title}: #{@content} (Category: #{@category}) [Created at: #{@created_at}]"
end
end
class MemoApp
def initialize
@memos = []
load_memos
end
def run
loop do
show_menu
choice = gets.chomp.to_i
case choice
when 1
create_memo
when 2
display_memos
when 3
search_memos
when 4
break
else
puts "Invalid choice. Please try again."
end
end
end
private
def show_menu
puts "\nMemo App Menu"
puts "1. Create Memo"
puts "2. Display Memos"
puts "3. Search Memos"
puts "4. Exit"
print "Enter your choice: "
end
def create_memo
print "Enter memo title: "
title = gets.chomp
print "Enter memo content: "
content = gets.chomp
print "Enter memo category: "
category = gets.chomp
memo = Memo.new(title, content, category)
@memos << memo
save_memos
puts "Memo created successfully!"
end
def display_memos
if @memos.empty?
puts "No memos found."
else
puts "\nMemos:"
@memos.each_with_index do |memo, index|
puts "#{index + 1}. #{memo}"
end
end
end
def search_memos
print "Enter search term: "
term = gets.chomp.downcase
results = @memos.select { |memo| memo.title.downcase.include?(term) || memo.content.downcase.include?(term) || memo.category.downcase.include?(term) }
if results.empty?
puts "No memos found matching the search term."
else
puts "\nSearch Results:"
results.each { |memo| puts memo }
end
end
def load_memos
if File.exist?("memos.txt")
File.open("memos.txt", "r") do |file|
file.each_line do |line|
title, content, category, created_at = line.chomp.split(": ")
@memos << Memo.new(title, content, category)
end
end
end
end
def save_memos
File.open("memos.txt", "w") do |file|
@memos.each do |memo|
file.puts "#{memo.title}: #{memo.content}: #{memo.category}: #{memo.created_at}"
end
end
end
end
# メモアプリの実行
memo_app = MemoApp.new
memo_app.run
ruby 特定の文字にアクセス
print "Your name? "
name = gets
puts name[0]
puts name[1]
puts name[2]
name[0] = "*"
name[1] = "*"
name[2] = "*"
puts name
Ruby 文字列を操作
print "Your name? "
name = gets
puts "*" * 10
puts "Hello #{name}"
puts "Hello " + name
puts "*" * 10
ruby ユーザーからの入力
print "Your name?"
name = gets
puts name