Java,一个循环,打印从最小到最大的所有数字?

时间:2018-01-27 00:19:24

标签: java loops

所有代码都可以使用我想要解决的循环底部。它应该打印用户从最小到最大输入的所有三个数字:

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 );  

    }  
}

2 个答案:

答案 0 :(得分:0)

您可以使用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] );

}

答案 1 :(得分:0)

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

也就是说,要创建一个包含min和max之间所有值的单个对话框,您应该创建一个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中显示所有值。