Java 解析为if语句的变量
我试图完成我的if语句,但我无法将winChance解析为if语句中的变量,如果有人能告诉我它是什么,我将不胜感激。ereor在“破产前的目标是:“+winChance”);“我的上一个winChance方法没有返回双变量”Java 解析为if语句的变量,java,variables,if-statement,Java,Variables,If Statement,我试图完成我的if语句,但我无法将winChance解析为if语句中的变量,如果有人能告诉我它是什么,我将不胜感激。ereor在“破产前的目标是:“+winChance”);“我的上一个winChance方法没有返回双变量” import java.util.Scanner; public class StockiColeA1Q3 { public static void main(String[] args) { Scanner keyboard = new Scanner
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
import java.util.Scanner;
公营证券交易所1Q3{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);//扫描仪
System.out.println(“您的名字是什么?”);
String first=keyboard.nextLine();
System.out.println(“你姓什么?”);
字符串last=keyboard.nextLine();
System.out.println(“您开始使用的芯片数量,“+first+”?”);
int start=keyboard.nextInt();
System.out.println(“您希望有多少芯片?”);
int end=keyboard.nextInt();
System.out.println(“每轮的概率是多少?\n”+
“(介于0.0和1.0之间的值)”;
double p=键盘.nextDouble();
打印结果(开始、结束、p、第一、最后);
}//主要
公共静态void打印结果(int开始、int结束、双p、字符串第一、字符串最后){
System.out.println(first+“”+last+”:如果以“+start+”芯片开头\n+
在拥有“+end+”芯片之前不要退出,\n+
每轮获胜的概率为“+p+”,\n+
“破产前达到目标的机会是:”+winChance);
}//打印结果
公共静态双绞车(双启动、双端、双p){
如果(p==0.5){
系统输出打印项次(开始/结束);
}
else如果(p>0&&pif(p==0.5){
系统输出打印项次(开始/结束);
**返回(开始/结束)**
}
else if(p>0&&p您需要在print语句中的“winChance”结尾添加括号以及参数。现在编译器认为您使用的是变量,而不是方法调用
比如说
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方法中返回一个值
我猜您的意思是在winChance方法中使用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));
}
如果(p==0.5){
返回开始/结束;
}
否则,如果(p>0&&p)您实际上没有声明一个名为winChanceTry的变量,请确保返回double,否则将出现编译错误。
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));
}