Implémentez la programmation pour générer les "résultats que vous souhaitez afficher" suivants dans chaque langage de Swift / Ruby / Python / Java en fonction de l'orientation de l'objet
・ Se souvenir quand tu oublies ・ Comparaison de chaque langue
① Calculez l'IMC à partir de la taille et du poids (2) Distinguer mince / normal / obèse de la valeur de l'IMC ③ Afficher ①② ci-dessus
---1ère personne---
Hauteur: 178.0cm
Poids: 61.8kg
BMI: 19.505112990784
d'habitude
---Deuxième personne---
Hauteur: 153.0cm
Poids: 74.0kg
BMI: 31.6117732496049
obésité
---3ème personne---
Hauteur: 185.0cm
Poids: 59.0kg
BMI: 17.2388604821037
Maigre
Swift
sample.swift
import Foundation
class Person{
var height:Double
var weight:Double
init(_ height:Double,_ weight:Double){
self.height = height
self.weight = weight
}
func getHeight() -> Double{
return self.height
}
func getWeight() -> Double{
return self.weight
}
func bmi() -> Double{
return self.weight / self.height / self.height * 10000
}
func isHealthy() -> String{
switch self.bmi(){
case ..<18.5:
return "Maigre"
break
case 18.5..<25:
return "d'habitude"
break
default:
return "obésité"
break
}
}
func info() {
print("la taille:\(self.getHeight())cm")
print("poids:\(self.getWeight())kg")
print("BMI: \(self.bmi())")
print("\(self.isHealthy())")
}
}
var person1:Person = Person(178,61.8)
var person2:Person = Person(153,74)
var person3:Person = Person(185,59)
var persons:[Person] = [person1,person2,person3]
var index:Int = 1
for person in persons{
print("---\(index)Œil---")
person.info()
index+=1
}
Ruby
sample.rb
class Person
attr_accessor :height,:weight
def initialize(height,weight)
@height = height
@weight = weight
end
def getHeight()
return self.height
end
def getWeight()
return self.weight
end
def bmi()
return self.weight / self.height / self.height * 10000
end
def isHealthy()
case self.bmi()
when 0..18.49
return "Maigre"
when 18.5..24.99
return "d'habitude"
else
return "obésité"
end
end
def info()
puts "#{self.getHeight()} cm"
puts "#{self.getWeight()} kg"
puts "BMI: #{self.bmi()}"
puts "#{self.isHealthy()}"
end
end
person1 = Person.new(178.0,61.8)
person2 = Person.new(153.0,74.0)
person3 = Person.new(185.0,59.0)
persons = [person1,person2,person3]
index = 1
persons.each do |person|
puts "---#{index}Œil---"
person.info()
index += 1
end
Python
sample.py
class Person:
def __init__(self,height,weight):
self.height = height
self.weight = weight
def getHeight(self):
return self.height
def getWeight(self):
return self.weight
def bmi(self):
return self.weight / self.height / self.height * 10000
def isHealthy(self):
if self.bmi() < 18.5:
return "Maigre"
elif 18.5 <= self.bmi() < 25.0:
return "d'habitude"
else:
return "obésité"
def info(self):
print("la taille:" + str(self.height) + "cm")
print("poids:" + str(self.weight) + "kg")
print("BMI:" + str(self.bmi()))
print(self.isHealthy())
person1 = Person(178.0,61.8)
person2 = Person(153.0,74.0)
person3 = Person(185.0,59.0)
persons = [person1,person2,person3]
index = 1
for person in persons:
print("---" + str(index) + "Œil---")
person.info()
index += 1
Java
sample.java
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Person person1 = new Person(178.0,61.8);
Person person2 = new Person(153.0,74.0);
Person person3 = new Person(185.0,59.0);
Person[] persons = {person1,person2,person3};
int index = 1;
for(int i=0;i<persons.length;i++){
System.out.println("---" + index + "Œil---");
persons[i].info();
index += 1;
}
}
public static class Person{
private double height;
private double weight;
Person(double height,double weight){
this.height = height;
this.weight = weight;
}
public double getHeight() {
return this.height;
}
public double getWeight(){
return this.weight;
}
public double bmi(){
return this.weight / this.height / this.height * 10000;
}
public String isHealthy(){
if(this.bmi()< 18.5){
return "Maigre";
} else if (this.bmi()>=18.5 && this.bmi()<25){
return "d'habitude";
} else {
return "obésité";
}
}
public void info(){
System.out.println("la taille:" + this.getHeight() + "cm");
System.out.println("poids:" + this.getWeight() + "kg");
System.out.println("BMI:" + this.bmi());
System.out.println(this.isHealthy());
}
}
}
① Swift est le plus simple à écrire ② De manière inattendue, Ruby a beaucoup de code ③ Swift est facile à écrire si vous apprenez Java ④ Pour ceux qui apprennent la programmation pour la première fois, Python peut être plus facile à comprendre que Ruby
Recommended Posts