我是Scala的新手,因为它是我在学校为入门CS课程所教授的。我不确定为什么我会收到此错误:
/home/ubuntu/Documents/Scala/Self/timeConvertIf.scala:12: error: type mismatch;
found : String
required: Int
val finalString = displayHours+":"("0"*(2-min.length))+min+":"+("0"*(2-sec.length))+sec
^
one error found
代码:
import io.StdIn._
print("Enter the number of seconds. ")
val totalSeconds = readInt()
if (totalSeconds > 0) {
val displaySeconds = totalSeconds%60
val totalMinutes = totalSeconds/60
val displayMinutes = totalMinutes%60
val displayHours = totalMinutes/60
val sec = displaySeconds.toString
val min = displayMinutes.toString
val finalString = displayHours+":"("0"*(2-min.length))+min+":"+("0"*(2-sec.length))+sec
println(finalString)
}
else {
println("This only works for a positive number of seconds.")
}
我了解类型不匹配错误是什么,但我不知道为什么我会收到错误显示错误的原因。非常感谢任何帮助!
答案 0 :(得分:0)
您在第一个+
和":"
之间缺少一个("0" * (2 - min.length))
:
val finalString = displayHours + ":" + ("0" * (2 - min.length)) + min + ":" + ("0" * (2 - sec.length)) + sec
答案 1 :(得分:0)
您错过+
前面的("0"*(2-min.length))
:
val finalString = displayHours + ":" + ("0"*(2-min.length)) + min +
":" + ("0"*(2-sec.length)) + sec
您还可以使用f"$num%02d"
数字格式,如下所示:
val finalString = f"${displayHours}:${displayMinutes}%02d:${displaySeconds}%02d"
答案 2 :(得分:0)
我想解释一下为什么你会得到这个"怪异的"错误信息。
如果你缩进一点,留一些空间 在令牌之间,你可以看得更清楚:
import io.StdIn._
print("Enter the number of seconds. ")
val totalSeconds = 3600 // readInt()
if (totalSeconds > 0) {
val displaySeconds = totalSeconds % 60
val totalMinutes = totalSeconds / 60
val displayMinutes = totalMinutes % 60
val displayHours = totalMinutes / 60
val sec = displaySeconds.toString
val min = displayMinutes.toString
val finalString =
displayHours + ":" + // <---- you missed this '+'
("0" * (2 - min.length)) + min + ":" +
("0" * (2 - sec.length)) + sec
println(finalString)
} else {
println("This only works for a positive number of seconds.")
}
发生的事情是:您忘记了+
,而是写下了":" (foo)
,其中foo
的类型为String。
现在,编译器将它置于方法应用程序":".apply(foo)
中(如果你写下来,它总是这样做)
类似于f(x)
,其中f
不是方法。方法apply
在String
上不存在,但
存在于&#34; pimped&#34; RichString
,字符串":"
自动转换为apply(i: Int)
。方法RichString
on i
期望一个整数i
,并返回该字符串的library(cowplot)
A <- vare.dist.long %>%
ggplot(aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_viridis(direction = 1) +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
plot.margin = unit(c(1,1,-1.5,1), "cm") ## Note the -1.5, it tells the grob to print itself 1.5 cm below its normal position.
) #/theme
B <- vare.data %>% dplyr::select(-c(Var2, value)) %>% unique %>%
arrange(Var1_Annotated) %>%
mutate(highN = medcode_chr(N, 0, 1),
highP = medcode_chr(P, 0, 1),
highK = medcode_chr(K, 0, 1)) %>%
dplyr::select(-c(N,P,K, Var1, Var1_Annotated)) %>%
gather(key, value, -Var1_chr) %>%
filter(value == 1) %>%
ggplot(aes(x = Var1_chr, y = key, color = key)) +
geom_point() +
theme(axis.text.x = element_text(angle = 270, hjust = 0, vjust = 0.5),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
plot.margin = unit(c(0,1,1,1), "cm")) +
coord_fixed(ratio = 0.3)
cowplot::plot_grid(A,B, nrow = 2, align = "v")
个字符。这就是您收到错误消息的原因
&#34;需要Int但找到String&#34; 。