有人可以帮我解释一下为什么我的程序在输入“0”或10个整数后不会终止?它应该在之后,但即使在满足退出条件之后也不会继续进行。它正确编译并且没有给我任何错误,所以我认为它与我的循环有关但我不知道什么是错的...
import java.util.Scanner;
public class Lab11
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int intArray[] = new int[10];
int size = 0;
int userInt;
do
{
do
{
System.out.print("Enter an integer in the array, or enter '0' to quit:" + " ");
userInt = scan.nextInt();
if (userInt != 0)
{
intArray[size] = userInt;
size++;
}
}
while (size <= intArray.length);
System.out.println(sum(intArray, size));
System.out.println(max(intArray, size));
System.out.println(min(intArray, size));
System.out.println(average(intArray, size));
System.out.println(stringToArray(intArray, size));
}
while (userInt !=0);
}
public static int sum(int intArray[], int size)
{
int sum = 0;
for (int a = 0; a <= size; a++)
{
sum = sum + intArray[size];
}
return sum;
}
public static int max(int intArray[], int size)
{
int max = intArray[0];
for (int b = 1; b <= size; b++)
{
if (intArray[size] > max)
{
max = intArray[size];
}
}
return max;
}
public static int min(int intArray[], int size)
{
int min = intArray[0];
for (int c = 1; c <= size; c++)
{
if (intArray[size] < min)
{
min = intArray[size];
}
}
return min;
}
public static double average(int intArray[], int size)
{
double total = 0;
double average = 0;
for (double element : intArray)
{
total = total + element;
}
if (intArray.length > 0)
{
average = total / intArray.length;
}
return average;
}
public static String stringToArray(int intArray[], int size)
{
String stringArray = "";
for (int d = 1; d <= size; d++)
{
stringArray = intArray.toString();
}
return stringArray;
}
}
答案 0 :(得分:0)
测试0的第一个if语句夹在do / while中,do / while将循环你的checker,直到你的if语句为真。把它放在你的循环外面。
编辑:主要的方法位是C,而不是java:)