Try & Catch in double Array 没有像我预期的那样工作
Try & Catch in double Array not working as I expected
我正在尝试对用户输入执行 try catch,如果通过检查则将用户输入放入数组中,但如果我输入无效输入,它会将索引替换为 0 并移至下一个索引。我试图弄清楚如何让重新提示在 for 循环内工作,以便该特定索引中的无效值被有效的用户输入替换。数组中值的顺序无关紧要。我正在尝试在不导入其他 Java 库的情况下执行此操作!
非常感谢任何帮助!我是编程新手 Java。感谢您的时间 !
public static double[] getAmount()
{
int MAX_NUM = 10;
double[] numArray = new double[MAX_NUM];
for (int i = 0; i < numArray.length;i++)
{
double numInput;
do
{
try
{
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amounts in $: "));
}
catch (NumberFormatException e)
{
numInput = MAX_NUM - 11;
}
if (numInput < 0 || numInput > 999999)
{
JOptionPane.showMessageDialog(null, "Error. Please enter valid amount in dollars");
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amount in $: "));
}
else
{
numArray[i] = numInput;
}
}
while (numInput < 0 && numInput > 999999);
}
return numArray;
}
public static double[] getAmount()
{
int MAX_NUM = 10;
double[] numArray = new double[MAX_NUM];
int i = 0;
while (i < numArray.length)
{
double numInput;
try
{
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amounts in $: "));
}
catch (NumberFormatException e)
{
numInput = MAX_NUM - 11;
}
if (numInput < 0 || numInput > 999999)
{
JOptionPane.showMessageDialog(null, "Error. Please enter valid amount in dollars");
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amount in $: "));
}
else
{
numArray[i] = numInput;
i++;
}
}
return numArray;
}
这应该有效:
public static double[] getAmount()
{
int MAX_NUM = 10;
double[] numArray = new double[MAX_NUM];
for (int i = 0; i < numArray.length;i++)
{
double numInput;
do
{
try
{
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amounts in $: "));
}
catch (NumberFormatException e)
{
numInput = MAX_NUM - 11;
}
if (numInput < 0 || numInput > 999999)
{
JOptionPane.showMessageDialog(null, "Error. Please enter valid amount in dollars");
}
}
while (numInput < 0 || numInput > 999999);
numArray[i] = numInput;
}
return numArray;
}
我正在尝试对用户输入执行 try catch,如果通过检查则将用户输入放入数组中,但如果我输入无效输入,它会将索引替换为 0 并移至下一个索引。我试图弄清楚如何让重新提示在 for 循环内工作,以便该特定索引中的无效值被有效的用户输入替换。数组中值的顺序无关紧要。我正在尝试在不导入其他 Java 库的情况下执行此操作!
非常感谢任何帮助!我是编程新手 Java。感谢您的时间 !
public static double[] getAmount()
{
int MAX_NUM = 10;
double[] numArray = new double[MAX_NUM];
for (int i = 0; i < numArray.length;i++)
{
double numInput;
do
{
try
{
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amounts in $: "));
}
catch (NumberFormatException e)
{
numInput = MAX_NUM - 11;
}
if (numInput < 0 || numInput > 999999)
{
JOptionPane.showMessageDialog(null, "Error. Please enter valid amount in dollars");
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amount in $: "));
}
else
{
numArray[i] = numInput;
}
}
while (numInput < 0 && numInput > 999999);
}
return numArray;
}
public static double[] getAmount()
{
int MAX_NUM = 10;
double[] numArray = new double[MAX_NUM];
int i = 0;
while (i < numArray.length)
{
double numInput;
try
{
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amounts in $: "));
}
catch (NumberFormatException e)
{
numInput = MAX_NUM - 11;
}
if (numInput < 0 || numInput > 999999)
{
JOptionPane.showMessageDialog(null, "Error. Please enter valid amount in dollars");
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amount in $: "));
}
else
{
numArray[i] = numInput;
i++;
}
}
return numArray;
}
这应该有效:
public static double[] getAmount()
{
int MAX_NUM = 10;
double[] numArray = new double[MAX_NUM];
for (int i = 0; i < numArray.length;i++)
{
double numInput;
do
{
try
{
numInput = Double.parseDouble(JOptionPane.showInputDialog("Enter amounts in $: "));
}
catch (NumberFormatException e)
{
numInput = MAX_NUM - 11;
}
if (numInput < 0 || numInput > 999999)
{
JOptionPane.showMessageDialog(null, "Error. Please enter valid amount in dollars");
}
}
while (numInput < 0 || numInput > 999999);
numArray[i] = numInput;
}
return numArray;
}