Java 循环中的一些问题
你好,Java 循环中的一些问题,java,loops,if-statement,Java,Loops,If Statement,你好, 我是java新手,我对这段代码有问题。我在想这是循环中的问题还是什么 public static void main(String[] args) { try { Scanner scn = new Scanner(System.in); int z = 0; String input; int[] ABArray = new int[2]; while (z == 0) {
我是java新手,我对这段代码有问题。我在想这是循环中的问题还是什么
public static void main(String[] args) {
try {
Scanner scn = new Scanner(System.in);
int z = 0;
String input;
int[] ABArray = new int[2];
while (z == 0) {
System.out.println("Input X to terminate.");
System.out.print("Input: ");
input = scn.nextLine().toLowerCase();
for (int i = 0; i < input.length(); i++) {
char AB = input.charAt(i);
ABArray[AB - 'a']++;
}
if (ABArray[0] == 0 || ABArray[1] == 0) {
System.out.println("Not Equal");
System.out.println("");
} else if (ABArray[0] == ABArray[1]) {
System.out.println("Equal");
System.out.println("");
} else if (ABArray[0] != ABArray[1]) {
System.out.println("Not Equal");
if (ABArray[0] > ABArray[1]) {
System.out.println("The number of A is greater than B.");
} else if (ABArray[0] < ABArray[1]) {
System.out.println("The number of B is greater than A.");
}
System.out.println("");
}
}
} catch (ArrayIndexOutOfBoundsException X) { } //Terminates the program
}
如你所见,问题是当我第一次输入相等的A和B时,它表示相等,当我输入不相等的A和B时,但在第三次输入相等的A和B时,它表示不相等
问题已解决。
感谢您的帮助。每次开始在
while
循环内工作时,您必须将阵列中的所有值设置为零。现在,在第三次启动时,
循环(使用AABB
输入)仍保留上次运行循环时留下的值-5
在数组的0索引元素中,而6
在数组的1索引元素中,因此,程序会给您错误的输出。您只需将输入读入一个字符串,然后在其中循环,计算您感兴趣的每个字符(此处为“a”和“b”)的出现次数,检查它们的计数是否相等,怎么样?例如,这是可行的:
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
String input;
while (true) {
System.out.println("Input X to terminate.");
System.out.print("Input: ");
input = scanner.nextLine().toLowerCase();
if (input.equals("X")) {
break;
} else {
int countA = 0;
int countB = 0;
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'a') {
countA++;
} else if (input.charAt(i) == 'b') {
countB++;
}
}
if (countA == countB) {
System.out.println("Equal!");
} else {
System.out.println("Not equal!");
}
}
}
} catch (ArrayIndexOutOfBoundsException e) // Terminates the program
{
}
}
publicstaticvoidmain(字符串[]args){
试一试{
扫描仪=新的扫描仪(System.in);
字符串输入;
while(true){
System.out.println(“输入X终止”);
系统输出打印(“输入:”);
输入=scanner.nextLine().toLowerCase();
if(输入等于(“X”)){
打破
}否则{
int countA=0;
int countB=0;
对于(int i=0;i
您可以使用以下代码:
public static void main( String[] args )
{
try
{
Scanner scn = new Scanner(System.in);
int z=0;
String input;
int[] ABArray = null;
while(z==0)
{
ABArray = new int[2];
System.out.println("Input X to terminate.");
System.out.print("Input: ");
input=scn.nextLine().toLowerCase();
for ( int i = 0; i < input.length(); i++ )
{
char AB=input.charAt(i);
ABArray[AB-'a']++;
}
if(ABArray[0]==0||ABArray[1]==0)
{
System.out.println("Not Equal");
System.out.println("");
}
else if(ABArray[0]==ABArray[1])
{
System.out.println("Equal");
System.out.println("");
}
else if(ABArray[0]!=ABArray[1])
{
System.out.println("Not Equal");
if(ABArray[0]>ABArray[1])
{
System.out.println("The number of A is greater than B.");
}
else if(ABArray[0]<ABArray[1])
{
System.out.println("The number of B is greater than A.");
}
System.out.println("");
}
}
}
catch(ArrayIndexOutOfBoundsException X) //Terminates the program
{
X.printStackTrace();
}
}
publicstaticvoidmain(字符串[]args)
{
尝试
{
扫描仪scn=新扫描仪(System.in);
int z=0;
字符串输入;
int[]ABArray=null;
而(z==0)
{
ABArray=新整数[2];
System.out.println(“输入X终止”);
系统输出打印(“输入:”);
输入=scn.nextLine().toLowerCase();
对于(int i=0;iABArray[1])
{
System.out.println(“A的数量大于B”);
}
else if(ABArray[0]您的程序的目的是什么?@ADTC检查A和B的数量是否相等。您如何做到这一点?(算法)请使用此信息扩展您的问题如果问题已解决,请接受给定的答案之一或通过编辑问题来描述您是如何解决问题的。
public static void main( String[] args )
{
try
{
Scanner scn = new Scanner(System.in);
int z=0;
String input;
int[] ABArray = null;
while(z==0)
{
ABArray = new int[2];
System.out.println("Input X to terminate.");
System.out.print("Input: ");
input=scn.nextLine().toLowerCase();
for ( int i = 0; i < input.length(); i++ )
{
char AB=input.charAt(i);
ABArray[AB-'a']++;
}
if(ABArray[0]==0||ABArray[1]==0)
{
System.out.println("Not Equal");
System.out.println("");
}
else if(ABArray[0]==ABArray[1])
{
System.out.println("Equal");
System.out.println("");
}
else if(ABArray[0]!=ABArray[1])
{
System.out.println("Not Equal");
if(ABArray[0]>ABArray[1])
{
System.out.println("The number of A is greater than B.");
}
else if(ABArray[0]<ABArray[1])
{
System.out.println("The number of B is greater than A.");
}
System.out.println("");
}
}
}
catch(ArrayIndexOutOfBoundsException X) //Terminates the program
{
X.printStackTrace();
}
}