我无法让我的程序将用户输入作为字符串,然后查看它是等于“是”还是“否”,如果都不等于,则显示“输入错误”。无论我输入“是”还是“否”,它总是显示“输入错误”。我尝试了几种不同类型的 if 和 do/while,但我似乎无法理解:
类文件:
import java.util.Scanner;
public class PhysicsProblem
{
private double vI; // initial velocity
private double vF; // final velocity
private double t; // time
private double deltaX; // change in the x value
//Make sure to add acceleration
public PhysicsProblem (double vI, double vF, double t, double deltaX)
{
this.vI = vI;
this.vF = vF;
this.t = t;
this.deltaX = deltaX;
}
public void setVi(String strVi)
{
while (!(strVi.equals("no") || strVi.equals("yes")));
{
System.out.println("incorrect entry");
}
if (strVi.equals("yes"))
{
System.out.println("Enter the initial velocity: ");
vI = new Scanner(System.in).nextDouble();
}
if (strVi.equals("no"))
{
System.out.println("The program is assuming you want to solve" +
"for intial velocity");
}
}
程序:
导入 java.util.Scanner;
public class PhysicsProblemSolver
{
public static void main (String[] args)
{
double vI = 0;
double vF = 0;
double t = 0;
double deltaX = 0;
Scanner scan = new Scanner(System.in);
PhysicsProblem problem1 = new PhysicsProblem (vI, vF, t, deltaX);
// Checks to see if initial velocity is given
System.out.println("Do you know the initial velocity? (Type 'yes' or 'no')");
String strVi = scan.next();
problem1.setVi(strVi);
我知道它看起来像一个不完整的程序,而且确实如此,但我只需要这一部分的帮助,所以我尽量不包括不必要的部分。对不起,如果它令人困惑!