我写的代码是一个随机的coinflipper,将有一个人输入他们的猜测。我无法触及主体中的任何东西,因为这是作业的方向。 coin=flip();
但我不能改变主任何东西:虽然当我尝试运行它硬币翻转需要错误帮助
Error:(15, 34) java: incompatible types: void cannot be converted to java.lang.String
错误是发生在主在这条线我得到这个错误。请帮助这是今晚!这里是我的代码:
public class CoinFlip {
static String side = "";
static String user = "";
static String winner = "";
public static void main(String args[]) {
String coin;
greeting();
do
{
user=IO.getString("Would you like: (H)- Heads or (T)- Tails");
coin=flip();
winner=compare(user,coin);
output(user,coin,winner);
}while(repeat());
}
public static void greeting() {
System.out.println("In a second, choose heads or tails. A coin flip will show and a winner will be shown.");
}
public static void flip() {
int random = (int) Math.floor(Math.random() * 2);
if (random = 0)
side = "H";
else if (random = 1)
side = "T";
}
public static void compare(String args[]) {
if (user = side)
winner = "Winner";
else
winner = "Loser";
}
public static void output() {
System.out.println(side);
System.out.println(winner);
}
public static void repeat() {
String again;
if (again = "yes")
main();
else if (again = "no")
System.out.println("Thanks for playing!");
}
}
'void flip()'不返回任何东西,但是当你说'coin = flip();'时,你试图把结果赋给'coin'。你需要使'flip()'返回一个'String',或者说'flip();硬币=边;'。 –
你应该把''“H”或''T“'''返回,但这对于void类型的方法是不可能的。改变''void flip()''''String flip()''。 – f1sh