我正在尝试要求用户输入是或否如果是,那么它将重新启动并且将添加特征,因为它们进行了2次选择。但是,它不让我问Y / N,我显然已经搞砸了。如果可能的话,我怎样才能最小化代码,而不是使用那些眼睛很差的优化if语句。
public static void main(String[]args)
{
//Number 1
Scanner scan = new Scanner(System.in);
int num = 0;
int num2 = 0;
int num3 = 0;
int count = 2;
String yesno;
do
{
System.out.println("Which aircraft would you like to simulate?");
System.out.println("1. Blimp");
System.out.println("2. Helicopter");
System.out.println("3. Fighter Jet");
System.out.println("4. Space Shuttle");
num = scan.nextInt();
if(num<1 || num>4) {System.out.println("Invalid");}
}
while(num<1 || num>4);
do
{
System.out.println("What characteristics would you like? (Input one, then the other)");
System.out.println("1. Position Trim ");
System.out.println("2. Force Breakout");
System.out.println("3. Force Gradient");
System.out.println("4. Force Friction");
System.out.println("5. Damping");
System.out.println("6. Hard Stop");
num2 = scan.nextInt();
num3 = scan.nextInt();
if(num2 == 1 || num3 == 1)
{
System.out.println("The position to which a flight control returns");
}
if(num2 == 2 ||num3 == 2)
{
System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity).");
}
if(num2 == 3 || num3 == 3)
{
System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim.");
}
if (num2 == 4 || num3 == 4)
{
System.out.println("A constant force that is opposite to the direction of movement");
}
if(num2 == 5 || num3 == 5)
{
System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force.");
}
if (num2 == 6 || num3 == 6)
{
System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted");
}
if(num2 < 1 || num2 > 6)
{
System.out.println("Invalid input");
}
if (yesno.equalsIgnoreCase("y")) {
do
{
System.out.println("What characteristics would you like? (Input one, then the other)");
System.out.println("1. Position Trim ");
System.out.println("2. Force Breakout");
System.out.println("3. Force Gradient");
System.out.println("4. Force Friction");
System.out.println("5. Damping");
System.out.println("6. Hard Stop");
num2 = scan.nextInt();
num3 = scan.nextInt();
if(num2 == 1 || num3 == 1)
{
System.out.println("The position to which a flight control returns");
}
if(num2 == 2 ||num3 == 2)
{
System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity).");
}
if(num2 == 3 || num3 == 3)
{
System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim.");
}
if (num2 == 4 || num3 == 4)
{
System.out.println("A constant force that is opposite to the direction of movement");
}
if(num2 == 5 || num3 == 5)
{
System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force.");
}
if (num2 == 6 || num3 == 6)
{
System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted");
}
if(num2 < 1 || num2 > 6)
{
System.out.println("Invalid input");
}
System.out.println("Would you like to re-select?(Y/N)");
yesno = scan.nextLine();
count += 2;
}
while(num2 < 1 || num2 > 6 || num3 < 1 ||num3 > 6 );
}
else if (yesno.equalsIgnoreCase("n"))
{
System.out.println("You've used " + count + " characteristics");
}
}
while(num2 < 1 || num2 > 6 || num3 < 1 ||num3 > 6 );
}
}
答案 0 :(得分:1)
您的代码可能会被压缩一些,但主要问题是yesno
变量永远不会被初始化,因此条件if (yesno.equalsIgnoreCase("y"))
的计算结果为false,而后面的块永远不会计算,所以行
System.out.println("Would you like to re-select?(Y/N)");
yesno = scan.nextLine();
永远不会被评估,用户永远不会被问到。如果保留代码,则应在创建时设置yesno="y"
。但是,就像我说的那样,你的代码可以简化。