using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
int HP = 100, ATK = 100;
int DEF = 50;
//
string name = "";
//少数
float f = 0.1f;
bool isDead = false;
// Start is called before the first frame update
void Start()
{
HP = 1000;
f = 1000;
name = "名前は勇者" + "です";
isDead = true;
HP = 200;
UnityEngine.Debug.Log("HP" + HP);
HP = 100;
UnityEngine.Debug.Log("HP" + HP);
}
private void FixedUpdate()
{
}
// Update is called once per frame
void Update()
{
}
}
カテゴリー: programming
ELDER
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ELDER</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
<style>
body {
padding-top: 20px;
background-color: #f8f9fa;
}
.card {
margin-bottom: 20px;
}
.profile-img {
width: 50px;
height: 50px;
object-fit: cover;
border-radius: 50%;
}
.comment-section {
margin-top: 20px;
}
.navbar {
margin-bottom: 20px;
}
.form-error {
color: red;
font-size: 0.9em;
}
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">SNS</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#" onclick="showLoginForm()">ログイン</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" onclick="showRegisterForm()">登録</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" onclick="showProfile()" style="display: none;" id="navProfile">プロフィール</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" onclick="logout()" style="display: none;" id="navLogout">ログアウト</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<h1 class="text-center mb-4">ソーシャルネットワーキングサービス</h1>
<!-- ログインフォーム -->
<div id="loginForm" class="card w-50 mx-auto">
<div class="card-body">
<h2 class="card-title text-center mb-4">ログイン</h2>
<div class="form-group">
<input type="text" id="loginUsername" class="form-control" placeholder="ユーザー名">
<span id="loginUsernameError" class="form-error" style="display: none;">ユーザー名を入力してください</span>
</div>
<div class="form-group">
<input type="password" id="loginPassword" class="form-control" placeholder="パスワード">
<span id="loginPasswordError" class="form-error" style="display: none;">パスワードを入力してください</span>
</div>
<button onclick="login()" class="btn btn-primary btn-block">ログイン</button>
<button onclick="showRegisterForm()" class="btn btn-secondary btn-block">登録</button>
</div>
</div>
<!-- 登録フォーム -->
<div id="registerForm" class="card w-50 mx-auto" style="display: none;">
<div class="card-body">
<h2 class="card-title text-center mb-4">登録</h2>
<div class="form-group">
<input type="text" id="registerName" class="form-control" placeholder="氏名">
<span id="registerNameError" class="form-error" style="display: none;">氏名を入力してください</span>
</div>
<div class="form-group">
<input type="text" id="registerUsername" class="form-control" placeholder="ユーザー名">
<span id="registerUsernameError" class="form-error" style="display: none;">ユーザー名を入力してください</span>
</div>
<div class="form-group">
<input type="password" id="registerPassword" class="form-control" placeholder="パスワード">
<span id="registerPasswordError" class="form-error" style="display: none;">パスワードを入力してください</span>
</div>
<div class="form-group">
<input type="password" id="registerConfirmPassword" class="form-control" placeholder="パスワードを確認">
<span id="registerConfirmPasswordError" class="form-error" style="display: none;">パスワードが一致しません</span>
</div>
<button onclick="register()" class="btn btn-primary btn-block">登録</button>
<button onclick="showLoginForm()" class="btn btn-secondary btn-block">ログインに戻る</button>
</div>
</div>
<!-- プロフィール -->
<div id="profile" class="card w-50 mx-auto" style="display: none;">
<div class="card-body">
<h2 class="card-title text-center mb-4">プロフィール</h2>
<div class="text-center mb-3">
<img id="profileImg" class="profile-img" src="default_profile_img.jpg" alt="プロフィール画像">
</div>
<p class="text-center mb-2"><strong>氏名:</strong> <span id="profileName"></span></p>
<p class="text-center mb-4"><strong>ユーザー名:</strong> <span id="profileUsername"></span></p>
<div class="form-group">
<input type="file" id="profileImageInput" accept="image/*" class="form-control-file">
</div>
<button onclick="uploadProfileImage()" class="btn btn-primary btn-block"><i class="fas fa-upload"></i> プロフィール画像をアップロード</button>
<button id="followButton" onclick="toggleFollow()" class="btn btn-primary btn-block"></button>
<button id="messageButton" onclick="openDirectMessageModal()" class="btn btn-primary btn-block">ダイレクトメッセージを送信</button>
<button onclick="logout()" class="btn btn-danger btn-block"><i class="fas fa-sign-out-alt"></i> ログアウト</button>
</div>
</div>
<!-- 投稿フォーム -->
<div id="postForm" class="card w-75 mx-auto" style="display: none;">
<div class="card-body">
<h2 class="card-title text-center mb-4">投稿を作成</h2>
<div class="form-group">
<textarea id="postContent" class="form-control" rows="3" placeholder="今何を考えていますか?"></textarea>
</div>
<button onclick="createPost()" class="btn btn-primary btn-block">投稿</button>
</div>
</div>
<!-- FEED登録フォーム -->
<div id="feedForm" class="card w-75 mx-auto" style="display: none;">
<div class="card-body">
<h2 class="card-title text-center mb-4">FEEDを登録</h2>
<div class="form-group">
<input type="text" id="feedUrl" class="form-control" placeholder="RSSフィードのURL">
<span id="feedUrlError" class="form-error" style="display: none;">RSSフィードのURLを入力してください</span>
</div>
<button onclick="registerFeed()" class="btn btn-primary btn-block">登録</button>
</div>
</div>
<!-- タイムライン -->
<div id="timeline" class="w-75 mx-auto mt-4"></div>
</div>
<!-- リプライモーダル -->
<div class="modal fade" id="replyModal" tabindex="-1" role="dialog" aria-labelledby="replyModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="replyModalLabel">投稿に返信</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="閉じる">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<textarea id="replyContent" class="form-control" rows="3" placeholder="返信をここに書いてください..."></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">閉じる</button>
<button type="button" onclick="postReply()" class="btn btn-primary">返信</button>
</div>
</div>
</div>
</div>
<!-- ダイレクトメッセージモーダル -->
<div class="modal fade" id="directMessageModal" tabindex="-1" role="dialog" aria-labelledby="directMessageModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="directMessageModalLabel">ダイレクトメッセージを送信</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="閉じる">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<select id="recipient" class="form-control">
<option value="">送信先を選択</option>
<!-- ユーザーリストがここに追加される -->
</select>
<textarea id="directMessageContent" class="form-control mt-2" rows="3" placeholder="メッセージをここに書いてください..."></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">閉じる</button>
<button type="button" onclick="sendDirectMessage()" class="btn btn-primary">送信</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
let currentUser = null; // 現在のログインユーザー
let users = []; // ユーザーの配列
let posts = []; // 投稿の配列
let feeds = []; // フィードの配列
// ローカルストレージにユーザー情報を保存
function saveUsersToLocalStorage() {
localStorage.setItem('users', JSON.stringify(users));
}
// ローカルストレージに投稿情報を保存
function savePostsToLocalStorage() {
localStorage.setItem('posts', JSON.stringify(posts));
}
// ローカルストレージにフィード情報を保存
function saveFeedsToLocalStorage() {
localStorage.setItem('feeds', JSON.stringify(feeds));
}
// ローカルストレージからユーザー情報を読み込み
function loadUsersFromLocalStorage() {
const storedUsers = localStorage.getItem('users');
if (storedUsers) {
users = JSON.parse(storedUsers);
}
}
// ローカルストレージから投稿情報を読み込み
function loadPostsFromLocalStorage() {
const storedPosts = localStorage.getItem('posts');
if (storedPosts) {
posts = JSON.parse(storedPosts);
}
}
// ローカルストレージからフィード情報を読み込み
function loadFeedsFromLocalStorage() {
const storedFeeds = localStorage.getItem('feeds');
if (storedFeeds) {
feeds = JSON.parse(storedFeeds);
}
}
function showLoginForm() {
hideAll();
document.getElementById('loginForm').style.display = 'block';
}
function showRegisterForm() {
hideAll();
document.getElementById('registerForm').style.display = 'block';
}
function validateLoginForm() {
let valid = true;
const username = document.getElementById('loginUsername').value;
const password = document.getElementById('loginPassword').value;
if (!username) {
document.getElementById('loginUsernameError').style.display = 'block';
valid = false;
} else {
document.getElementById('loginUsernameError').style.display = 'none';
}
if (!password) {
document.getElementById('loginPasswordError').style.display = 'block';
valid = false;
} else {
document.getElementById('loginPasswordError').style.display = 'none';
}
return valid;
}
function login() {
if (!validateLoginForm()) {
return;
}
const username = document.getElementById('loginUsername').value;
const password = document.getElementById('loginPassword').value;
const user = users.find(u => u.username === username && u.password === password);
if (user) {
currentUser = user;
showProfile();
} else {
alert('ユーザー名またはパスワードが間違っています。');
}
}
function logout() {
currentUser = null;
hideAll();
document.getElementById('loginForm').style.display = 'block';
document.getElementById('navProfile').style.display = 'none';
document.getElementById('navLogout').style.display = 'none';
}
function validateRegisterForm() {
let valid = true;
const name = document.getElementById('registerName').value;
const username = document.getElementById('registerUsername').value;
const password = document.getElementById('registerPassword').value;
const confirmPassword = document.getElementById('registerConfirmPassword').value;
if (!name) {
document.getElementById('registerNameError').style.display = 'block';
valid = false;
} else {
document.getElementById('registerNameError').style.display = 'none';
}
if (!username) {
document.getElementById('registerUsernameError').style.display = 'block';
valid = false;
} else {
document.getElementById('registerUsernameError').style.display = 'none';
}
if (!password) {
document.getElementById('registerPasswordError').style.display = 'block';
valid = false;
} else {
document.getElementById('registerPasswordError').style.display = 'none';
}
if (password !== confirmPassword) {
document.getElementById('registerConfirmPasswordError').style.display = 'block';
valid = false;
} else {
document.getElementById('registerConfirmPasswordError').style.display = 'none';
}
return valid;
}
function register() {
if (!validateRegisterForm()) {
return;
}
const name = document.getElementById('registerName').value;
const username = document.getElementById('registerUsername').value;
const password = document.getElementById('registerPassword').value;
users.push({ name, username, password, following: false });
saveUsersToLocalStorage(); // ユーザー情報を保存
alert('登録が完了しました!ログインしてください。');
showLoginForm();
}
function showProfile() {
hideAll();
document.getElementById('profile').style.display = 'block';
document.getElementById('profileName').textContent = currentUser.name;
document.getElementById('profileUsername').textContent = currentUser.username;
document.getElementById('postForm').style.display = 'block';
document.getElementById('feedForm').style.display = 'block';
loadProfileImage(); // プロフィール画像をロード
displayTimeline(); // タイムラインを表示
updateFollowButton(); // フォローボタンの表示を更新
loadDirectMessageRecipients(); // ダイレクトメッセージの送信先をロード
document.getElementById('navProfile').style.display = 'block';
document.getElementById('navLogout').style.display = 'block';
}
// プロフィール画像をロード
function loadProfileImage() {
const profileImg = document.getElementById('profileImg');
// プロフィール画像が設定されている場合はその画像を表示
if (currentUser.profileImage) {
profileImg.src = currentUser.profileImage;
} else {
// デフォルトのプロフィール画像を表示
profileImg.src = 'default_profile_img.jpg';
}
}
// プロフィール画像をアップロード
function uploadProfileImage() {
const input = document.getElementById('profileImageInput');
const file = input.files[0];
if (file) {
// FileReaderを使用して画像を読み込む
const reader = new FileReader();
reader.onload = function(e) {
// 読み込んだ画像をプロフィール画像として設定
currentUser.profileImage = e.target.result;
// プロフィール画像を更新して表示
loadProfileImage();
saveUsersToLocalStorage(); // プロフィール画像を保存
}
reader.readAsDataURL(file);
}
}
// フォローボタンの更新
function updateFollowButton() {
const followButton = document.getElementById('followButton');
if (currentUser.following) {
followButton.textContent = 'フォロー解除';
} else {
followButton.textContent = 'フォロー';
}
saveUsersToLocalStorage(); // フォロー状態を保存
}
// フォローの切り替え
function toggleFollow() {
currentUser.following = !currentUser.following;
updateFollowButton();
}
// ダイレクトメッセージの送信先をロード
function loadDirectMessageRecipients() {
const select = document.getElementById('recipient');
select.innerHTML = '<option value="">送信先を選択</option>';
users.forEach(user => {
if (user !== currentUser) {
const option = document.createElement('option');
option.value = user.username;
option.textContent = user.name;
select.appendChild(option);
}
});
}
// ダイレクトメッセージのモーダルを開く
function openDirectMessageModal() {
$('#directMessageModal').modal('show');
}
// ダイレクトメッセージを送信
function sendDirectMessage() {
const recipientUsername = document.getElementById('recipient').value;
const messageContent = document.getElementById('directMessageContent').value;
if (recipientUsername && messageContent.trim() !== '') {
const recipient = users.find(user => user.username === recipientUsername);
if (recipient) {
alert(`メッセージを${recipient.name}に送信しました: ${messageContent}`);
$('#directMessageModal').modal('hide');
} else {
alert('送信先が見つかりません。');
}
} else {
alert('送信先を選択し、メッセージを入力してください。');
}
}
function createPost() {
const postContent = document.getElementById('postContent').value;
if (postContent.trim() !== '') {
const post = {
id: Date.now(),
content: postContent,
author: currentUser.name,
authorUsername: currentUser.username,
authorProfileImage: currentUser.profileImage, // プロフィール画像を追加
likes: 0,
comments: []
};
posts.unshift(post); // 最新の投稿を先頭に追加
savePostsToLocalStorage(); // 投稿を保存
displayTimeline();
document.getElementById('postContent').value = ''; // 投稿後、入力欄を空にする
}
}
function registerFeed() {
const feedUrl = document.getElementById('feedUrl').value;
if (feedUrl.trim() !== '') {
feeds.push(feedUrl);
saveFeedsToLocalStorage(); // フィード情報を保存
alert('RSSフィードが登録されました!');
document.getElementById('feedUrl').value = ''; // 登録後、入力欄を空にする
} else {
document.getElementById('feedUrlError').style.display = 'block';
}
}
function displayTimeline() {
const timeline = document.getElementById('timeline');
timeline.innerHTML = '';
posts.forEach(post => {
const postElement = document.createElement('div');
postElement.classList.add('card', 'mb-3');
postElement.innerHTML = `
<div class="card-body">
<div class="d-flex align-items-center mb-3">
<img src="${post.authorProfileImage || 'default_profile_img.jpg'}" class="profile-img mr-2" alt="プロフィール画像">
<div>
<strong>${post.author}</strong> (@${post.authorUsername})
</div>
</div>
<p class="card-text">${post.content}</p>
${post.link ? `<a href="${post.link}" target="_blank" class="btn btn-link">続きを読む</a>` : ''}
<div class="d-flex justify-content-between">
<div>
<button onclick="likePost(${post.id})" class="btn btn-primary btn-sm"><i class="fas fa-thumbs-up"></i> いいね (${post.likes})</button>
<button onclick="toggleComments(${post.id})" class="btn btn-secondary btn-sm"><i class="fas fa-comments"></i> コメント (${post.comments.length})</button>
<button onclick="replyToPost(${post.id})" class="btn btn-info btn-sm"><i class="fas fa-reply"></i> 返信</button>
</div>
<small class="text-muted">${new Date(post.id).toLocaleString()}</small>
</div>
<div id="comments-${post.id}" class="comment-section mt-3" style="display: none;">
${post.comments.map(comment => `
<div class="card mb-2">
<div class="card-body">
<p><strong>${comment.author}</strong>: ${comment.content}</p>
</div>
</div>
`).join('')}
</div>
</div>
`;
timeline.appendChild(postElement);
});
}
function likePost(postId) {
const post = posts.find(p => p.id === postId);
post.likes++;
savePostsToLocalStorage(); // いいねを保存
displayTimeline();
}
function toggleComments(postId) {
const commentSection = document.getElementById(`comments-${postId}`);
commentSection.style.display = commentSection.style.display === 'none' ? 'block' : 'none';
}
function replyToPost(postId) {
const modal = document.getElementById('replyModal');
modal.dataset.postId = postId;
$('#replyModal').modal('show');
}
function postReply() {
const postId = parseInt(document.getElementById('replyModal').dataset.postId);
const post = posts.find(p => p.id === postId);
const replyContent = document.getElementById('replyContent').value;
if (replyContent.trim() !== '') {
post.comments.push({ author: currentUser.name, content: replyContent });
$('#replyModal').modal('hide');
savePostsToLocalStorage(); // コメントを保存
displayTimeline();
} else {
alert('返信を入力してください。');
}
}
function fetchFeeds() {
feeds.forEach(feedUrl => {
fetch(`https://api.rss2json.com/v1/api.json?rss_url=${encodeURIComponent(feedUrl)}`)
.then(response => response.json())
.then(data => {
data.items.forEach(item => {
const post = {
id: Date.now() + Math.random(), // 重複しないIDを生成
content: item.title,
link: item.link, // リンクを追加
author: 'RSSフィード',
authorUsername: 'rssfeed',
authorProfileImage: 'default_profile_img.jpg', // プロフィール画像を設定
likes: 0,
comments: []
};
posts.unshift(post); // 最新の投稿を先頭に追加
savePostsToLocalStorage(); // 投稿を保存
});
displayTimeline(); // タイムラインを表示
})
.catch(error => console.error('Error fetching RSS feed:', error));
});
}
function hideAll() {
document.getElementById('loginForm').style.display = 'none';
document.getElementById('registerForm').style.display = 'none';
document.getElementById('profile').style.display = 'none';
document.getElementById('postForm').style.display = 'none';
document.getElementById('feedForm').style.display = 'none';
}
// 初期データをローカルストレージから読み込む
loadUsersFromLocalStorage();
loadPostsFromLocalStorage();
loadFeedsFromLocalStorage();
// 初期ログイン画面表示
hideAll();
document.getElementById('loginForm').style.display = 'block';
// 1時間ごとにRSSフィードをフェッチして自動投稿
setInterval(fetchFeeds, 5); // 3600000ミリ秒 = 1時間
</script>
</body>
</html>
RSSの機能を使えるようにしました
Q&Aサイト
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Q&A Site</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
h1, h2, h3 {
color: #333;
}
#question-list, #add-question, #question-detail {
margin-bottom: 20px;
}
#questions {
list-style-type: none;
padding: 0;
}
.question, .answer {
background-color: #fff;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
.answer {
margin-left: 20px;
}
form {
display: flex;
flex-direction: column;
}
form input, form textarea {
margin-bottom: 10px;
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
}
form button {
padding: 10px;
border: none;
border-radius: 5px;
background-color: #333;
color: #fff;
cursor: pointer;
}
form button:hover {
background-color: #555;
}
.hidden {
display: none;
}
.action-buttons {
display: flex;
gap: 10px;
}
</style>
</head>
<body>
<h1>Q&A Site</h1>
<div id="question-list">
<h2>Questions</h2>
<ul id="questions"></ul>
</div>
<div id="add-question">
<h2>Add a Question</h2>
<form id="question-form">
<input type="text" id="question-title" placeholder="Title" required>
<textarea id="question-body" placeholder="Question" required></textarea>
<button type="submit">Add Question</button>
</form>
</div>
<div id="question-detail" class="hidden">
<h2 id="detail-title"></h2>
<p id="detail-body"></p>
<div class="action-buttons">
<button id="edit-question-button">Edit</button>
<button id="delete-question-button">Delete</button>
</div>
<div id="edit-question" class="hidden">
<h3>Edit Question</h3>
<form id="edit-question-form">
<input type="text" id="edit-question-title" required>
<textarea id="edit-question-body" required></textarea>
<button type="submit">Save Changes</button>
</form>
</div>
<h3>Answers</h3>
<ul id="answers"></ul>
<h3>Add an Answer</h3>
<form id="answer-form">
<input type="text" id="answer-name" placeholder="Your name" required>
<textarea id="answer-body" placeholder="Your answer" required></textarea>
<button type="submit">Add Answer</button>
</form>
<button id="back-button">Back to Questions</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
let questions = [];
let currentQuestionIndex = null;
const questionForm = document.getElementById('question-form');
const questionTitle = document.getElementById('question-title');
const questionBody = document.getElementById('question-body');
const questionsList = document.getElementById('questions');
const questionDetail = document.getElementById('question-detail');
const detailTitle = document.getElementById('detail-title');
const detailBody = document.getElementById('detail-body');
const answersList = document.getElementById('answers');
const answerForm = document.getElementById('answer-form');
const answerName = document.getElementById('answer-name');
const answerBody = document.getElementById('answer-body');
const backButton = document.getElementById('back-button');
const editQuestionButton = document.getElementById('edit-question-button');
const deleteQuestionButton = document.getElementById('delete-question-button');
const editQuestionForm = document.getElementById('edit-question-form');
const editQuestionTitle = document.getElementById('edit-question-title');
const editQuestionBody = document.getElementById('edit-question-body');
questionForm.addEventListener('submit', (e) => {
e.preventDefault();
const newQuestion = {
title: questionTitle.value,
body: questionBody.value,
answers: []
};
questions.push(newQuestion);
displayQuestions();
questionTitle.value = '';
questionBody.value = '';
});
answerForm.addEventListener('submit', (e) => {
e.preventDefault();
const newAnswer = {
name: answerName.value,
body: answerBody.value
};
questions[currentQuestionIndex].answers.push(newAnswer);
displayQuestionDetail(currentQuestionIndex);
answerName.value = '';
answerBody.value = '';
});
editQuestionButton.addEventListener('click', () => {
editQuestionForm.classList.remove('hidden');
editQuestionTitle.value = questions[currentQuestionIndex].title;
editQuestionBody.value = questions[currentQuestionIndex].body;
});
editQuestionForm.addEventListener('submit', (e) => {
e.preventDefault();
questions[currentQuestionIndex].title = editQuestionTitle.value;
questions[currentQuestionIndex].body = editQuestionBody.value;
displayQuestionDetail(currentQuestionIndex);
displayQuestions();
editQuestionForm.classList.add('hidden');
});
deleteQuestionButton.addEventListener('click', () => {
questions.splice(currentQuestionIndex, 1);
questionDetail.classList.add('hidden');
document.getElementById('question-list').classList.remove('hidden');
document.getElementById('add-question').classList.remove('hidden');
displayQuestions();
});
backButton.addEventListener('click', () => {
questionDetail.classList.add('hidden');
document.getElementById('question-list').classList.remove('hidden');
document.getElementById('add-question').classList.remove('hidden');
});
function displayQuestions() {
questionsList.innerHTML = '';
questions.forEach((question, index) => {
const li = document.createElement('li');
li.className = 'question';
li.innerHTML = `
<h3>${question.title}</h3>
<p>${question.body}</p>
<button onclick="viewQuestion(${index})">View Details</button>
`;
questionsList.appendChild(li);
});
}
window.viewQuestion = (index) => {
currentQuestionIndex = index;
displayQuestionDetail(index);
document.getElementById('question-list').classList.add('hidden');
document.getElementById('add-question').classList.add('hidden');
questionDetail.classList.remove('hidden');
};
function displayQuestionDetail(index) {
const question = questions[index];
detailTitle.textContent = question.title;
detailBody.textContent = question.body;
displayAnswers(index);
}
function displayAnswers(index) {
const question = questions[index];
answersList.innerHTML = '';
question.answers.forEach((answer, answerIndex) => {
const li = document.createElement('li');
li.className = 'answer';
li.innerHTML = `
<p><strong>${answer.name}:</strong> ${answer.body}</p>
<button onclick="editAnswer(${index}, ${answerIndex})">Edit</button>
<button onclick="deleteAnswer(${index}, ${answerIndex})">Delete</button>
`;
answersList.appendChild(li);
});
}
window.editAnswer = (questionIndex, answerIndex) => {
const answer = questions[questionIndex].answers[answerIndex];
const newBody = prompt("Edit your answer:", answer.body);
if (newBody) {
answer.body = newBody;
displayAnswers(questionIndex);
}
};
window.deleteAnswer = (questionIndex, answerIndex) => {
questions[questionIndex].answers.splice(answerIndex, 1);
displayAnswers(questionIndex);
};
displayQuestions();
});
</script>
</body>
</html>
C++ 純粋仮想関数
#include <iostream>
#include <vector>
using namespace std;
struct Character {
virtual void action() = 0;
};
struct Warrior : public Character {
void action() { cout << "Warrior attacks with a sword!" << endl; }
};
struct Mage : public Character {
void action() { cout << "Mage casts a fireball!" << endl; }
};
int main() {
Warrior w;
w.action();
Mage m;
m.action();
vector<Character*> characters = { &w, &m };
for (auto c : characters) {
c->action();
}
}
C++ デストラクタ
#include <iostream>
#include <string>
#include <memory>
using namespace std;
struct Person {
string name;
Person(const string& newName) : name(newName) {}
~Person() {
cout << name << "は解体された\n";
}
};
int main() {
Person a1("Taro");
Person* pA2 = new Person("Jiro");
Person* pA3 = new Person("Saburo");
auto pA4 = make_shared<Person>("Shiro");
cout << a1.name << endl;
cout << pA2->name << endl;
cout << pA3->name << endl;
cout << pA4->name << endl;
delete pA2;
}
React アプリケーション完成版
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My React App</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container"></div>
<script type="text/babel">
'use strict';
{
const Menu = (props) => {
const decrement = () => {
props.onDecrement(props.menuId);
};
const increment = () => {
props.onIncrement(props.menuId);
};
return (
<li>
<button onClick={decrement}>-</button>
<button onClick={increment}>+</button>
{props.name} ({props.price}G ☓ {props.count}個)
</li>
);
};
const App = () => {
const [counts, setCounts] = React.useState([0, 0, 0]);
const menus = [
{ id: 0, name: 'ポーション', price: 400 },
{ id: 1, name: 'ハイポーション', price: 500 },
{ id: 2, name: 'エリクサー', price: 300 },
];
const total =
(menus[0].price * counts[0]) +
(menus[1].price * counts[1]) +
(menus[2].price * counts[2]);
const decrementMenu = (menuId) => {
if(counts[menuId] > 0){
const newCounts = [...counts];
newCounts[menuId]--;
setCounts(newCounts);
}
};
const incrementMenu = (menuId) => {
const newCounts = [...counts];
newCounts[menuId]++;
setCounts(newCounts);
};
const menuItems = menus.map((menu) => {
return (
<Menu
key={menu.id}
menuId={menu.id}
count={counts[menu.id]}
name={menu.name}
price={menu.price}
onDecrement={decrementMenu}
onIncrement={incrementMenu}
/>
);
});
return (
<>
<h1>メニュー</h1>
<ul className="menus">
{menuItems}
</ul>
<p>合計: {total}G</p>
</>
);
};
const container = document.querySelector('#container');
const root = ReactDOM.createRoot(container);
root.render(<App />);
}
</script>
</body>
</html>
style.css
@charset "utf-8";
body{
margin: 0;
}
#container{
width: 400px;
margin: auto;
}
h1{
margin: 16px 0 0 0;
font-size: 20px;
text-align: center;
}
.menus{
margin: 0;
padding: 0;
list-style-type: none;
}
.menus > li{
border: 1px solid #ccc;
padding: 8px;
border-radius: 8px;
margin-top: 16px;
}
.menus button{
margin-right: 8px;
width: 24px;
}
p{
margin: 0;
text-align: right;
}
C++ search
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v{ 2,3,5,1,4 };
auto begin = v.cbegin();
auto end = v.cend();
int target = 3;
auto pos = find(begin, end, target);
if (pos == end)
cout << "見つからない\n";
else
cout << "見つかった: " << *pos << endl; // 出力値: 見つかった: 3
target = 6; // Correcting typo from 'traget' to 'target'
pos = find(begin, end, target);
if (pos == end)
cout << "見つからない\n"; // 出力値: 見つからない
else
cout << "見つかった: " << *pos << endl;
return 0;
}
React 合計金額を表示してみよう!
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My React App</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container"></div>
<script type="text/babel">
'use strict';
{
const Menu = (props) => {
const decrement = () => {
};
const increment = () => {
};
return (
<li>
<button onClick={decrement}>-</button>
<button onClick={increment}>+</button>
{props.name} ({props.price}G ☓ {props.count}個)
</li>
);
};
const App = () => {
const [counts, setCounts] = React.useState([1, 1, 1]);
const menus = [
{ id: 0, name: 'ポーション', price: 400 },
{ id: 1, name: 'ハイポーション', price: 500 },
{ id: 2, name: 'エリクサー', price: 300 },
];
const total =
(menus[0].price * counts[0]) +
(menus[1].price * counts[1]) +
(menus[2].price * counts[2]);
const menuItems = menus.map((menu) => {
return (
<Menu
key={menu.id}
count={counts[menu.id]}
name={menu.name}
price={menu.price}
/>
);
});
return (
<>
<h1>メニュー</h1>
<ul className="menus">
{menuItems}
</ul>
<p>合計: {total}G</p>
</>
);
};
const container = document.querySelector('#container');
const root = ReactDOM.createRoot(container);
root.render(<App />);
}
</script>
</body>
</html>
style.css
@charset "utf-8";
body{
margin: 0;
}
#container{
width: 400px;
margin: auto;
}
h1{
margin: 16px 0 0 0;
font-size: 20px;
text-align: center;
}
.menus{
margin: 0;
padding: 0;
list-style-type: none;
}
.menus > li{
border: 1px solid #ccc;
padding: 8px;
border-radius: 8px;
margin-top: 16px;
}
.menus button{
margin-right: 8px;
width: 24px;
}
p{
margin: 0;
text-align: right;
}
C++ sort
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v{ 2 ,3 , 5, 1, 4 };
sort(v.begin(), v.end());
for (auto i : v) cout << i << ", ";
cout << endl;
int a[] = { 2,3,5,1,4 };
sort(begin(a), end(a));
for (auto i : a) cout << i << ",";
cout << endl;
}
React リスト項目にkey属性を設定する
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My React App</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container"></div>
<script type="text/babel">
'use strict';
{
const Menu = (props) => {
return (
<li>
<button>-</button>
<button>+</button>
{props.name}({props.price}G X 0個)
</li>
);
};
const App = () => {
const menus = [
{id: 0,name: '聖剣', price: 400},
{id: 1,name: '魔装銃', price: 500},
{id: 2,name: '魔剣', price: 300},
];
const menuItems = menus.map((menu) =>{
return(
<Menu
key={menu.id}
name={menu.name}
price={menu.price}
/>
);
});
return (
<>
<h1>メニュー</h1>
<ul className="menus">
{menuItems}
</ul>
<p>合計: 0円</p>
</>
);
};
const container = document.querySelector('#container');
const root = ReactDOM.createRoot(container);
root.render(<App />);
}
</script>
</body>
</html>
style.css
@charset "utf-8";
body{
margin: 0;
}
#container{
width: 400px;
margin: auto;
}
h1{
margin: 16px 0 0 0;
font-size: 20px;
text-align: center;
}
.menus{
margin: 0;
padding: 0;
list-style-type: none;
}
.menus > li{
border: 1px solid #ccc;
padding: 8px;
border-radius: 8px;
margin-top: 16px;
}
.menus button{
margin-right: 8px;
width: 24px;
}
p{
margin: 0;
text-align: right;
}