【问题标题】:Java, a loop that prints all of the numbers from the min to the max?Java,一个打印从最小值到最大值的所有数字的循环?
【发布时间】:2018-01-27 00:19:24
【问题描述】:

所有代码都能正常工作,期待我试图弄清楚的底部 for 循环。它应该打印用户输入的所有三个数字,从最小到最大:

import java.util.Random;
import javax.swing.JOptionPane;

public class CalculateValue {

    public static void main(String[] args) {

        // user input
        int first = Integer.parseInt (JOptionPane.showInputDialog("Enter first number"));
        int second = Integer.parseInt (JOptionPane.showInputDialog("Enter second number"));
        int third = Integer.parseInt (JOptionPane.showInputDialog("Enter third number"));

        // calculate max and min
        int maxValue=first;
        int minValue=first;

        if (second > maxValue) {
            maxValue= second;
        }
        if (second < minValue) {
            minValue = second;  
        }

        if (third > maxValue) {
            maxValue= third;
        }
        if (third < minValue) {
            minValue = third;  
        }
        // display output for largest and smallest numbers
        JOptionPane.showMessageDialog(null, " Largest number: " + maxValue +  " Smallest number: " + minValue);

        //  generate random number between 1 and 100
        int highest = 100;
        int lowest = 1;
        Random randomNum = new Random();
        int display = lowest + randomNum.nextInt(highest);

        // display output for random number
        JOptionPane.showMessageDialog(null, "Random number between 1 and 100:   " + display);

        // for loop that runs through all numbers

        for (int x = minValue; x < maxValue; x++)

            //  output for all numbers
            JOptionPane.showMessageDialog(null,"All numbers from smallest to largest: " + x );  

    }  
}

【问题讨论】:

  • 假设min = 1 和max = 5 应该显示在单个对话框中All numbers from smalles to largest: 1 2 3 4 5 还是为每个数字显示一个对话框?这是最后一个 for 循环,但我不确定我是否完全理解您的问题......
  • 单个对话框最好。

标签: java loops


【解决方案1】:

您可以使用Arrays.sort(),它将对您的三个数字进行快速排序。

public static void main(String[] args) {

    // user input
    int first = Integer.parseInt (JOptionPane.showInputDialog("Enter first number"));
    int second = Integer.parseInt (JOptionPane.showInputDialog("Enter second number"));
    int third = Integer.parseInt (JOptionPane.showInputDialog("Enter third number"));

    int[] numbers = new int[] { first, second, third};
    Arrays.sort(numbers);

    // display output for largest and smallest numbers
    JOptionPane.showMessageDialog(null, " Largest number: " + numbers[2] +  " Smallest number: " + numbers[0]);

    //  generate random number between 1 and 100
    int highest = 100;
    int lowest = 1;
    Random randomNum = new Random();
    int display = lowest + randomNum.nextInt(highest);

    // display output for random number
    JOptionPane.showMessageDialog(null, "Random number between 1 and 100:   " + display);

    //  output for all numbers
    JOptionPane.showMessageDialog(null,"All numbers from smallest to largest: " + numbers[0] + " ," + numbers[1] + ", " + numbers[2] );

}

【讨论】:

    【解决方案2】:

    首先我建议您在所有循环中添加花括号,否则您可能会感到困惑。

    也就是说,要创建一个包含最小值和最大值之间所有值的单个对话框,您应该创建一个 StringBuilder 来存储所有值并将其连接到您的文本:

    StringBuilder sb = new StringBuilder();
    for (int x = minValue; x < maxValue; x++) {
        sb.append(x);
        sb.append(", "); //Add a comma and space or just a space or the char you want to separate each entry
    }
    
    JOptionPane.showMessageDialog(null,"All numbers from smallest to largest: " + sb.toString());  //This will concatenate
    

    这应该在一个对话框中为您提供所有值。

    或者,如果您想要一个显示所有 3 个值的单个对话框,您可以将上述内容与以下内容一起使用:

    JPanel pane = new JPanel();
    pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
    
    pane.add(new JLabel(" Largest number: " + maxValue));
    pane.add(new JLabel(" Smallest number: " + minValue));
    pane.add(new JLabel("Random number between 1 and 100:   " + display));
    pane.add(new JLabel("All numbers from smallest to largest: " + sb.toString());
    
    JOptionPane.showMessageDialog(null, pane);
    

    它会将所有值显示在一个JDialog 中,每个值都在一个新行中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2019-05-26
      • 2020-07-02
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多