Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
组合for和while循环(Java)_Java_Loops_For Loop_While Loop - Fatal编程技术网

组合for和while循环(Java)

组合for和while循环(Java),java,loops,for-loop,while-loop,Java,Loops,For Loop,While Loop,我是Java新手,我正在尝试制作一个程序,允许用户输入100个数字,如果用户写“0”,那么该程序将打印最小、最大、总和和所有数字。我要做的是工作,但不是退出打印。我的老师说了一些关于使用while循环的事情,但是当你使用for循环时,这怎么可能呢 问候 public static void main(String[] args) { int[] list = new int[100]; int min = 0; int max = 0; int sum = 0;

我是Java新手,我正在尝试制作一个程序,允许用户输入100个数字,如果用户写“0”,那么该程序将打印最小、最大、总和和所有数字。我要做的是工作,但不是退出打印。我的老师说了一些关于使用while循环的事情,但是当你使用for循环时,这怎么可能呢

问候

public static void main(String[] args) {
    int[] list = new int[100];
    int min = 0;
    int max = 0;
    int sum = 0;
    boolean first = true;

    Scanner scan = new Scanner(System.in);
    while(list[i] != 0) {
        for (int i = 0; i < list.length; i++) {

            System.out.print("Enter number (0 to exit) " + (1 + i) + ":");
            list[i] = scan.nextInt();
        }

        for (int i = 0; i < list.length; i++) {

            if (first == true) {

            min = list[i];
            first = false;
        }

        if (list[i] < min) {

            min = list[i];
        }

        else if (list[i] > max) {

            max = list[i];
        }

        sum = list[i] + sum;

    }

    if (list[i] == 0) {

    System.out.print("Numbers are: " + list[0] + ", ");

    for (int i = 1; i < list.length; i++)

    System.out.print(list[i] + ", ");
    System.out.println();

    System.out.println("Smallest number is: " + min);
    System.out.println("Largest numeber is: " + min);
    System.out.println("Sum is: " + sum);
    }
    }
}

}
publicstaticvoidmain(字符串[]args){
int[]列表=新int[100];
int min=0;
int max=0;
整数和=0;
布尔值优先=真;
扫描仪扫描=新扫描仪(System.in);
while(列表[i]!=0){
for(int i=0;i最大值){
max=列表[i];
}
总和=列表[i]+总和;
}
如果(列表[i]==0){
系统输出打印(“编号为:“+列表[0]+”,”);
for(int i=1;i
您的代码很混乱。最好使用这样的模式:

while (true) {
    // read next
    if (input == 0) 
        break;
}

下面是方法的主体,它将执行您所要求的操作。我没有使用while循环(但实际上,for循环在内部是一种while循环)

int size=100;//设置要输入的数字数量。
int[]list=新的int[size];//创建具有“大小”元素的数组。
int min=Integer.MAX_值;//将可能的最大整数设置为起始值。
int max=0;//假设用户不会输入负数,将最小值设置为零。
整数和=0;//初始化列表中数字的总和。
扫描仪扫描=新扫描仪(System.in);
对于(inti=0;i最大值){//如果数字大于上一个最小的数字,则将此数字设置为最大值
最大值=数量;
}
}
//输出列表中的所有数字
for(int i=0;i
执行此操作只需要一个while循环,如果需要,还需要一个for循环来打印阵列:

Scanner scan = new Scanner(System.in);
int i = 0;
int sum = 0;
int maxValue = Integer.MIN_VALUE;
int[] history = new int[100];
System.out.println("INPUT:");
int option = scan.nextInt();
while (option != 0 && i <= 100)
{
    if (option > maxValue)
        maxValue=option;
    sum += option;
    history[i] = option;
    option = scan.nextInt();
    i++;
}
System.out.println("OUTPUT: \n" + "SUM: " + sum + "\n MAX VALUE: " + maxValue);
for (int x : history)
    System.out.print(x + "");
Scanner scan=新的扫描仪(System.in);
int i=0;
整数和=0;
int maxValue=整数.MIN_值;
int[]历史=新int[100];
System.out.println(“输入:”);
int option=scan.nextInt();
while(选项!=0&&i最大值)
最大值=选项;
总和+=期权;
历史[i]=期权;
option=scan.nextInt();
i++;
}
System.out.println(“输出:\n“+”和:“+SUM+”\n最大值:“+maxValue”);
for(int x:历史)
系统输出打印(x+“”);

那么的
循环可能不合适?您可以使用
for
循环并在其内部有一个
if
,或者您有一个
while
循环并有一个变量作为计数器。“for循环”是“while”循环(或者,就此而言,“do/while”循环)的特化。你可以在while循环中做任何你可以在for循环中做的事情。你也可以嵌套循环,一个循环嵌套在另一个循环中。For从while派生,它从布尔递归派生。他还需要跟踪到目前为止的条目数,并在100处停止。但是我想如何用while循环打印出所有的数字呢?哦,谢谢。我很抱歉,我制定的任务如此糟糕,但它不只是假设退出,它必须打印的数字,最小,最大等以及更新。只需使用
break
而不是
System.exit(0)
。Break将退出当前循环并继续执行代码。Ops,未正确读取它。非常感谢你!你现在不知道退出时如何去掉零?@user1905426:请投票,如果答案正确,请将问题标记为已回答。谢谢!但是我如何去掉零呢?任务是打印“数字是:1,3,7”@user1905426如果你想去掉所有的零,即递增地添加到数组中,你需要使用一个动态结构,比如arrayList。
Scanner scan = new Scanner(System.in);
int i = 0;
int sum = 0;
int maxValue = Integer.MIN_VALUE;
int[] history = new int[100];
System.out.println("INPUT:");
int option = scan.nextInt();
while (option != 0 && i <= 100)
{
    if (option > maxValue)
        maxValue=option;
    sum += option;
    history[i] = option;
    option = scan.nextInt();
    i++;
}
System.out.println("OUTPUT: \n" + "SUM: " + sum + "\n MAX VALUE: " + maxValue);
for (int x : history)
    System.out.print(x + "");