You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.4 KiB
2.4 KiB
- 华氏度和摄氏度互相转化
use std::io;
fn main() {
println!("请输入您的体温: ");
let mut temp = String::new();
io::stdin()
.read_line(&mut temp)
.expect("输入数据有误");
let temp: f64 = temp.trim().parse().expect("类型转换失败");
println!("请输入温度单位(F或C):(若无特殊设置,默认大于50度为华氏度):");
let mut unit = String::new();
io::stdin()
.read_line(&mut unit)
.expect("输入数据有误");
// 也可以通过去字符串,我直接true false了。
let unit2:bool = match unit.trim(){
"F"=>true,
"C"=>false,
_=>{
if temp > 50.0 {
println!("使用华氏温度单位(F)");
true
}else {
println!("使用摄氏温度单位(C)");
false
}
}
};
println!("FFF is: {}", temp);
println!("type is: {}", unit2);
let res = trans(temp, unit2);
println!("res is: {}", res);
}
fn trans(temp:f64, is_fahrenheit:bool)->f64{
if is_fahrenheit{
(temp-32.0)*5.0/9.0
}else {
temp*9.0/5.0 +32.0
}
}
- 生成第n个斐波那契数
use std::io;
fn main() {
println!("请输入你要查的队列下标");
let mut n = String::new();
io::stdin()
.read_line(&mut n)
.expect("数据输入有误");
let n:i32 = n.trim().parse().expect("类型转换失败");
let mut a = 0;
let mut b = 1;
for i in 0..n{
let c = a+b;
a = b;
b = c;
}
println!("res is: {}", a);
}
- 圣诞节的十二天
fn main() {
let days = ["first", "second", "third", "fourth", "fifth", "sixth",
"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"];
let gifts = ["A partridge in a pear tree.", "Two turtle doves,",
"Three French hens,", "Four calling birds,",
"Five golden rings,", "Six geese a-laying,",
"Seven swans a-swimming,", "Eight maids a-milking,",
"Nine ladies dancing,", "Ten lords a-leaping,",
"Eleven pipers piping,", "Twelve drummers drumming,"];
for i in 0..12{
println!("On the {} day of Christmas, \nmy true love sent to me", days[i]);
for j in (0..=i).rev(){
println!("{}",gifts[j]);
}
println!();
}
println!("Hello, world!");
}