我正在编写一个简单的项目,但我在这里面临一个非常小的问题。说实话,我不知道我的问题的正确逻辑
我有一段时间的条件,所以它不会停止接收输入。我想通过键入'compute'来停止输入,但它给了我错误,因为我真的不知道如何在System.out.println(bla bla)的输出之后插入'compute'东西
int discreteNum;
double prob;
boolean condition = true;
String forComput;
Scanner input = new Scanner(System.in);
Scanner str = new Scanner(System.in);
//These are just outputs, they're harmless
System.out.println("Mean of Discrete Random Variable \n");
System.out.print("Instructions:\n");
System.out.print("Please enter a discrete random variable with its
probability\n type 'compute' for the expected value\n");
while(condition = true ){
discreteNum = input.nextInt();
prob = input.nextDouble();
System.out.println("( " + discreteNum + "," + prob + " )");
}
//This code below, I just put it here.
//It's just my insight. I tried it but it did not work.
//It's worthy to be deleted here but I just want to show you
forComput = str.nextLine();
if(forComput == "compute"){
condition = false;
System.out.println("End!");
}
这是我的代码的工作方式
现在,我试图通过键入compute来结束我的输入,这是我不知道如何。赦免
我真的不知道它的逻辑。我刚试过