切换大小写算法 Java 问题(需要一个新的 while 循环?



我正在尝试根据我的数组值运行开关语句。

我需要做一个while循环,运行我的整个数组, 如果数组位value = 0我需要它运行case 0如果位value = 1我需要它运行case 1

我需要运行 while 循环,直到它完成通过数组值和相应的大小写切换函数运行

我正在处理的代码:

Scanner scan = new Scanner(System.in);
System.out.println("What position do you want the top card to be moved to?");
System.out.println("Enter "1", "2", "3", "4" or "5"");
int i = scan.nextInt();
int in = i -1;
//System.out.println(i);
String binaryString = Integer.toBinaryString(in);
System.out.println(binaryString);
int foo = Integer.parseInt(binaryString, 2);
System.out.println(foo);
String[] array = binaryString.split("\|", -1);
System.out.println(Arrays.toString(array));

switch (foo) {
case 0:
cardForceOutShuffle();
index = 51;
break;
case 1:
cardForceInShuffle();
index = 51;
break;
}

请帮忙。

假设很多事情,这就是我想出的。请更准确地回答您的问题..

我将我的意见作为注释添加到一些代码中。

Scanner scan = new Scanner(System.in);
System.out.println("What position do you want the top card to be moved to?");
System.out.println("Enter "1", "2", "3", "4" or "5"");
int i = scan.nextInt(); // This can lead to an InputMismatchException. Try to check if your input is an integer before you proceed.
int in = i - 1; // Unnecessary
String binaryString = Integer.toBinaryString(in); // Use i-1 directly
char[] charArray = binaryString.toCharArray(); // Since you need each bit you dont need String.split. Just convert to char[]
for (int j = 0; j < charArray.length; j++) {
switch (charArray[j]) { // This switch is actually useless, since both cases do the same thing.
case '0':
cardForceOutShuffle();
index = 51; // What is index?
break;
case '1':
cardForceOutShuffle();
index = 51;
break;
}
}
scan.close(); // Close your resources if they are not needed anymore.

相关内容

最新更新