我试图要求用户输入是或否,如果它是是,那么它将重新启动,并且将添加特征,因为他们进行了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 );
}
}
您的代码可以进行一些压缩,但主要问题是yesno
变量从未初始化,因此条件if(yesno.equalsIgnoreCase(“y”))
的计算结果为false,而下面的块从未计算,因此行
System.out.println("Would you like to re-select?(Y/N)");
yesno = scan.nextLine();
将永远不会被评估并且用户永远不会被询问。如果代码保持原样,则应在创建代码时设置yesno=“y”
。但是,正如我所说,您的代码可以简化。
要打印: 这不是一个家庭作业问题,我只是不能理解这个概念。
这段代码是为一个基本的杂货计算器的按钮。当我按下按钮时,一个输入对话框显示您在哪里输入您的商品价格。我遇到的问题是,我不知道如何获得循环,使输入对话框在输入后弹出。 我希望它总是回来,除非用户选择ok与nothing或cancel,在这种情况下,循环应该中断并填充剩余的框。使用当前的代码,我必须每次手动按下按钮来恢复对话框。我一直在玩不同的while条件和if语句,但我似乎无法让它发挥作用。我是一
我对编码完全陌生,正在使用Python作为一个学校项目开发一个基于文本的游戏。用户必须通过键入“北”、“东”、“南”或“西”进入房间。如果方向无效,则应弹出错误消息,提示用户输入新方向。如果用户键入退出,游戏应该结束。 这个项目有一百万个问题,因为我发现我在编码方面很糟糕,但我想弄清楚的是,如果出现提示,如何让我的程序退出游戏。这是我的代码(它不是完整的代码,只是我到目前为止所拥有的代码。我试图一
我用For创建了一个嵌套循环,这是程序代码和输出,然后我尝试了同时循环,得到了不同的结果 对于 虽然 请引导我。。谢谢
只要给定条件为真,Perl编程语言中的while循环语句就会重复执行目标语句。 语法 (Syntax) Perl编程语言中while循环的语法是 - while(condition) { statement(s); } 这里的statement(s)可以是单个陈述或一个陈述块。 condition可以是任何表达。 当条件为真时,循环迭代。 当条件变为假时,程序控制将立即传递到循环之后的行。
编写程序时,您可能会遇到需要反复执行操作的情况。 在这种情况下,您需要编写循环语句以减少行数。 JavaScript支持所有必要的循环,以减轻编程压力。 while循环 JavaScript中最基本的循环是while循环,将在本章中讨论。 while循环的目的是只要expression为真,就重复执行语句或代码块。 表达式变为false,循环终止。 流程图 while loop流程图如下 - 语法