2014-10-02 104 views
-1

即时通讯尝试完成我的if语句,并且我无法将winChance解析为if语句中的变量,如果有人能够向我展示它,将不胜感激。该ereor是'目标之前会破产是:“+永昌);‘我的最后永昌方法没有返回一个双变量’解决if语句的变量

import java.util.Scanner; 

public class StockiColeA1Q3 { 

    public static void main(String[] args) { 

    Scanner keyboard = new Scanner(System.in); //scanner 

    System.out.println("What is your first name? "); 
    String first = keyboard.nextLine(); 

    System.out.println("What is your last name? "); 
    String last = keyboard.nextLine(); 

    System.out.println("What is the amount of chips you are starting with, " + first +"?"); 
    int start = keyboard.nextInt(); 

    System.out.println("What is the amount of chips you wish you have ?"); 
    int end = keyboard. nextInt(); 

    System.out.println("What is the probability of each round? \n" + 
         "(A value between 0.0 and 1.0)"); 
    double p = keyboard.nextDouble(); 

    printResult(start,end,p,first,last); 

    }//main 

    public static void printResult(int start, int end, double p, String first, String last) { 

    System.out.println(first + " " + last +": If you start with " + start + " chips\n" + 
         "And do not quit until you have " + end + " chips,\n" + 
         "with the probability of " + p + " of winning each round, \n" + 
         "the chance of reaching your goal before going broke is: " + winChance); 

    }//print results 

    public static double winChance(double start, double end, double p) { 


    if (p==0.5) { 
    System.out.println(start/end); 

    }  
    else if (p>0&&p<1) { 
    System.out.println(1-(Math.pow((1-p)/p, start))/1-(Math.pow((1-p)/p, end))); 

    }//if p isnt 0.5 
    }//win chance 

}//gamblers ruin 
+0

你实际上并没有声明一个叫做winChance的变量 – SamTebbs33 2014-10-02 17:42:31

回答

0
if (p==0.5) { 
System.out.println(start/end); 
**return (start/end);** 

}  
else if (p>0&&p<1) { 
System.out.println(1-(Math.pow((1-p)/p, start))/1-(Math.pow((1-p)/p, end))); 
**return (1-(Math.pow((1-p)/p, start))/1-(Math.pow((1-p)/p, end)));** 

}//if p isnt 0.5 

确保您返回的东西,现在你不返回任何东西。

+0

试试这个确保你返回double,否则你会得到一个编译错误。 – brso05 2014-10-02 17:44:55

0

您需要添加括号为“永昌”结束的打印语句中,与参数一起。现在,编译器认为你正在使用一个变量,而不是一个方法调用。

对于示例

System.out.println(first + " " + last +": If you start with " + start + " chips\n" + 
        "And do not quit until you have " + end + " chips,\n" + 
        "with the probability of " + p + " of winning each round, \n" + 
        "the chance of reaching your goal before going broke is: " + winChance(start, end, p)); 

您还需要关注brso05的回答,并在winChance方法中返回一个值。

PS我猜你的意思是使用return代替的System.out.println()在你的文创法,如果是这样,请使用以下代替:

if (p==0.5) { 
    return start/end; 
}  
else if (p>0&&p<1) { 
    return 1-(Math.pow((1-p)/p, start))/1-(Math.pow((1-p)/p, end)); 
} 

您可能需要阅读起来更多关于在Java中使用方法,试试这个http://www.tutorialspoint.com/java/java_methods.htm