【发布时间】: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 循环,但我不确定我是否完全理解您的问题...... -
单个对话框最好。