Java 试图让用户输入正常工作
我有一个河内之塔益智项目正在接近完成。我现在的问题是试图从用户那里获得正确的输入 如果他们键入“v”或“v”,则将显示解决此难题的步骤(因此输出为“将光盘从S移动到D”,依此类推)。否则,如果用户未键入“v”或“v”,则程序将继续解决难题,显示全部移动,但不显示步骤 我面临的问题是,这些选项并不像它们应该的那样有效 现在唯一的错误是当用户输入的'v'或'v'时,移动显示不正确。 输出:Java 试图让用户输入正常工作,java,recursion,user-input,towers-of-hanoi,Java,Recursion,User Input,Towers Of Hanoi,我有一个河内之塔益智项目正在接近完成。我现在的问题是试图从用户那里获得正确的输入 如果他们键入“v”或“v”,则将显示解决此难题的步骤(因此输出为“将光盘从S移动到D”,依此类推)。否则,如果用户未键入“v”或“v”,则程序将继续解决难题,显示全部移动,但不显示步骤 我面临的问题是,这些选项并不像它们应该的那样有效 现在唯一的错误是当用户输入的'v'或'v'时,移动显示不正确。 输出: Enter the min number of discs : 2 Press 'v' or 'V' for
Enter the min number of discs :
2
Press 'v' or 'V' for a list of moves
v
Move disc from needle S to A
Total Moves : 3
如果用户键入“v”或“v”,并且如果用户键入了除此之外的其他一些移动,则输出仅显示“总移动”,我如何实现显示移动?
这是我的密码:
import java.util.*;
import java.util.Scanner;
public class TowerOfHanoi4 {
static int moves=0;
public static void main(String[] args) {
System.out.println("Enter the min number of discs : ");
Scanner scanner = new Scanner(System.in);
int iHtMn = scanner.nextInt(); //iHeightMin
char source='S', auxiliary='D', destination='A'; //name poles or 'Needles'
System.out.println("Press 'v' or 'V' for a list of moves");
Scanner show = new Scanner(System.in);
String c = show.next();
// char lstep='v', lsteps='V'; // grab option v or V
if (c.equalsIgnoreCase("v")){ //if option is not v or V, execute code and only display total moves
hanoi(iHtMn, source, destination, auxiliary); //else, user typed v or V and moves are displayed
System.out.println(" Move disc from needle "+source+" to "+destination);
System.out.println(" Total Moves : "+moves);
} else {
hanoi(iHtMn, source, destination, auxiliary);
System.out.println(" Total Moves : "+moves);
}
}
static void hanoi(int htmn,char source,char destination,char auxiliary)
{
if (htmn >=1)
{
hanoi(htmn-1, source, auxiliary, destination); // move n-1 disks from source to auxilary
// System.out.println(" Move disc from needle "+source+" to "+destination); // move nth disk to destination
moves++;
hanoi(htmn-1, auxiliary, destination, source);//move n-1 disks from auxiliary to Destination
}
// else (
}
}
检查这行
if (c != lstep || c != lsteps){////}
即使c是v或v,这也会起作用
如果c='v',那么c!='所以这个“如果测试”适用于V或V。
换成
if (c != lstep && c != lsteps){
检查这行
if (c != lstep || c != lsteps){////}
即使c是v或v,这也会起作用
如果c='v',那么c!='所以这个“如果测试”适用于V或V。
换成
if (c != lstep && c != lsteps){
代码的问题在于
else if
条件,当您键入v
或v
时,应用程序将执行第一个if和条件,因此永远不会执行else if
一个如果
条件足够,就这样试试
//if option is not v or V, execute code and only display total moves
//else, user typed v or V and moves are displayed
if (c == lstep || c == lsteps){
hanoi(iHeight, source, destination, auxiliary);
System.out.println(" Move disc from needle "+source+" to "+destination);
System.out.println(" Total Moves : "+moves);
} else {
hanoi(iHeight, source, destination, auxiliary);
System.out.println(" Total Moves : "+moves);
}
顺便说一下,代码中不需要多个扫描程序,只有一个就足够了
也不需要定义两个字符来比较它们,您可以很容易地这样做:
String c = show.next();
//if option is not v or V, execute code and only display total moves
//else, user typed v or V and moves are displayed
if (c.equalsIgnoreCase("v")){
hanoi(iHeight, source, destination, auxiliary);
System.out.println(" Move disc from needle "+source+" to "+destination);
System.out.println(" Total Moves : "+moves);
} else {
hanoi(iHeight, source, destination, auxiliary);
System.out.println(" Total Moves : "+moves);
}
更新根据上次编辑,您可以定义一个静态布尔
字段,并将if条件移动到hanoi
方法
检查粘贴箱代码的问题在于
else if
条件,当您键入v
或v
时,应用程序将执行第一个if and条件,因此您的else if
永远不会执行
一个如果
条件足够,就这样试试
//if option is not v or V, execute code and only display total moves
//else, user typed v or V and moves are displayed
if (c == lstep || c == lsteps){
hanoi(iHeight, source, destination, auxiliary);
System.out.println(" Move disc from needle "+source+" to "+destination);
System.out.println(" Total Moves : "+moves);
} else {
hanoi(iHeight, source, destination, auxiliary);
System.out.println(" Total Moves : "+moves);
}
顺便说一下,代码中不需要多个扫描程序,只有一个就足够了
也不需要定义两个字符来比较它们,您可以很容易地这样做:
String c = show.next();
//if option is not v or V, execute code and only display total moves
//else, user typed v or V and moves are displayed
if (c.equalsIgnoreCase("v")){
hanoi(iHeight, source, destination, auxiliary);
System.out.println(" Move disc from needle "+source+" to "+destination);
System.out.println(" Total Moves : "+moves);
} else {
hanoi(iHeight, source, destination, auxiliary);
System.out.println(" Total Moves : "+moves);
}
更新根据上次编辑,您可以定义一个静态布尔
字段,并将if条件移动到hanoi
方法
检查粘贴箱为什么有多个扫描仪?为什么有多个扫描仪?非常感谢。它几乎可以工作,但现在当我输入v或v时,输出不正确。我编辑了上面的代码以向您展示。如何使其正确显示移动列表?只需移动此打印语句
System.out.println(“将光盘从指针“+源+”移动到“+目标”)
在hanoi
方法中,并仅将此语句的if条件移动到该方法中,您可以在方法中添加一个boolean
参数,或在我将if条件移动到语句中时定义一个静态布尔字段,我不需要在hanoi方法中移动整个if/else语句以及字符串c的声明吗?否则,用于比较的“c”在该方法中无法识别。哇,这是一个多么简单和方便的答案。非常感谢,很抱歉我是java的新手,才做了大约一个月。非常感谢。它几乎可以工作,但现在当我输入v或v时,输出不正确。我编辑了上面的代码以向您展示。如何使其正确显示移动列表?只需移动此打印语句System.out.println(“将光盘从指针“+源+”移动到“+目标”)
在hanoi
方法中,并仅将此语句的if条件移动到该方法中,您可以在方法中添加一个boolean
参数,或在我将if条件移动到语句中时定义一个静态布尔字段,我不需要在hanoi方法中移动整个if/else语句以及字符串c的声明吗?否则,用于比较的“c”在该方法中无法识别。哇,这是一个多么简单和方便的答案。非常感谢,很抱歉我是java的新手,我才做了一个月。谢谢。它几乎可以工作,但现在当我输入v或v时,输出不正确。我编辑了上面的代码以向您展示。如何让它正确显示移动列表?谢谢。它几乎可以工作,但现在当我输入v或v时,输出不正确。我编辑了上面的代码以向您展示。如何使其正确显示移动列表?