【问题标题】:Combine for and while loop (Java)结合 for 和 while 循环 (Java)
【发布时间】:2012-12-03 00:05:57
【问题描述】:

我是 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);
    }
    }
}

}

【问题讨论】:

  • 那么for 循环可能不合适?要么使用for 循环并在内部有一个if,要么你有一个while 循环并有一个变量作为计数器。
  • “for 循环”是“while”循环的特化(或者,就此而言,“do/while”循环)。你可以在一个while循环中做所有你可以在for循环中做的事情。你也可以嵌套循环,一个在另一个里面。
  • For 源自 while,它源自布尔递归。

标签: java loops for-loop while-loop


【解决方案1】:

您只需要一个 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 + "");

【讨论】:

  • 谢谢!但是我如何摆脱零点?任务是让它打印“数字是:1、3、7”
  • @user1905426 如果你想去掉所有的零,即增量添加到数组中,你需要使用像 arrayList 这样的动态结构。
【解决方案2】:

这是方法的主体,它将按照您的要求执行。我没有使用过while循环(但实际上,for循环在内部是一种while循环)。

int size = 100; // Set the number of numbers to input.
int[] list = new int[size]; // Create an array with 'size' elements.
int min = Integer.MAX_VALUE; // Set the highest possible integer as start value.
int max = 0; // Set the minimum to zero, assuming that the user won't input negative numbers.
int sum = 0; // Initialize the sum of the numbers in the list.

Scanner scan = new Scanner(System.in);

for (int i = 0; i < size; i++) { // Run 'size' times the process of inputting a number.
    System.out.print("Enter number (0 to exit) " + (i + 1) + ": ");
    int number = scan.nextInt();
    if (number == 0) { // Quit program if input equals '0'
        System.out.println("Exiting...");
        break;
    }
    list[i] = number; // Add the current number to the list
    sum += number; // Add the number to the total
    if (number < min) { // If the number is smaller than the previous one, set this number as the smallest
        min = number;
    }
    if (number > max) { // If the number is greater than the previous smallest number, set this number as the greatest
        max = number;
    }
}

// Output all numbers in the list
for (int i = 0; i < list.length; i++) {
    if (list[i] != 0) {
        System.out.print((i == 0 ? "" : ", ") + list[i]);
    }
}

// You see the snippet (i == 0 ? "" : ", ")
// That is a shorthand if-else statement:
// If i equals 0, then "" (empty string), else ", " (comma and space).
// The statement
//     System.out.print((i == 0 ? "" : ", ") + list[i])
// is the same as
//     if (i == 0) {
//         System.out.println("") + list[i];
//     }
//     else {
//         System.out.println(", ") + list[i];
//     }

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

【讨论】:

  • 哦,谢谢。很抱歉,我将任务制定得如此糟糕,但它不仅仅是假设退出,它还必须打印数字、最小最大等
  • 已更新。只需使用break 而不是System.exit(0)。 Break 将退出当前循环并继续执行代码。
  • 操作,没有正确阅读。非常感谢!您现在没有碰巧退出时如何摆脱零?
  • @user1905426:如果是正确答案,请投票并将问题标记为已回答。
【解决方案3】:

您的代码混乱。最好使用这样的模式:

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

【讨论】:

  • 他还需要跟踪到目前为止的条目数,并在 100 处停止。
  • 但是我想怎么用while循环打印出所有的数字呢?
猜你喜欢
  • 1970-01-01
  • 2020-09-18
  • 2013-06-01
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 2014-02-01
相关资源
最近更新 更多