如何使用双打用户输入关闭 while 循环
How to close a while loop using user input for doubles
所以,免责声明,这是为了class,但我已经提交了作业并联系了教授寻求帮助,所以我没有作弊或要求别人做我的作业。在这个过程中,我也花了几个小时浏览这个网站。
我正在尝试获取最多 52 次用户输入,将它们全部加在一起,然后求和的平均值。
我制作此方法是为了允许用户根据代码中进一步输入的 Sales 变量输入他们的每周利润。我认为 52 的限制会很好,因为它允许一个人平均 2 周、一个月、6 个月等,就像他们正在尝试预算或寻找估计的年收入一样。
//new method
public static void computeAverageSalesProfit() {
//new scanner object in method
Scanner input = new Scanner(System.in);
//new object
Sales AvgProfit = new Sales();
//prompt for input
System.out.println("Please enter weekly profits, up to 52 weeks. When finished, enter '-1.'");
//value to store input, and test against
double value = 0;
//new double array to sum together and average out
double[] sum2 = new double[52];
//while loop of doom
while(value >= 0) {
for (int i=0; i<sum2.length; i++) {
value = input.nextDouble();
sum2[i] = value + 0;
}
}
double WeeklyProfits = 0;
for (int i=0; i<sum2.length; i++) {
WeeklyProfits = sum2[i] / sum2.length;
}
AvgProfit.setWeeklyProfits(WeeklyProfits);
System.out.printf("Your average profits are %.2f", AvgProfit.getWeeklyProfits());
}
这是我晚上用的代码,但我不知道如何结束 while 循环。我最初没有 value 变量,但认为在 sum2 迭代之前检查它可以让我关闭它。当我 运行 程序时,它接受用户输入,但直到我达到 52 次迭代才结束。我也试过在布尔表达式中使用“0000”,但这也没有用。
我也试过不设置value变量,能想到的和网上能找到的方法我都试过了。我尝试使用和不使用值变量,只有一个普通的双精度值(不是数组)。我什至尝试使用字符串,使用布尔表达式 (good = true/false, while (!/good) 等,但我无法将其转换为 double 并取平均值。
我完全莫名其妙,头疼。如果您能分享任何帮助或见解,我将不胜感激。谢谢。
哦,我的代码的不同部分中有一个 while 循环确实有效,但我认为这个循环的工作方式不同。我想我要么想多了,要么只是漏掉了一些非常明显的东西。
//Check Year length separate, keeping original code mostly intact
public static boolean isValid(String input)
{
//Does it have 4 digits?
if(input.length() != 4)
return false;
// Is it a number ?
try
{
Integer i = Integer.parseInt(input);
}
catch(NumberFormatException e)
{
return false;
}
// Passed all checks and is valid
return true;
}
您可以做的一件事是不使用 while 循环,而是像这样使用 for 循环:
//value to store input, and test against
double value = 0;
//new double array to sum together and average out
double[] sum2 = new double[52];
// repeats 52 times max
for (int i=0; i<sum2.length; i++) {
value = input.nextDouble();
// Checking if the user entered a negative number
if (value < 0) {
// The break keyword exits a loop
break;
}
sum2[i] = value + 0;
}
所以,免责声明,这是为了class,但我已经提交了作业并联系了教授寻求帮助,所以我没有作弊或要求别人做我的作业。在这个过程中,我也花了几个小时浏览这个网站。
我正在尝试获取最多 52 次用户输入,将它们全部加在一起,然后求和的平均值。
我制作此方法是为了允许用户根据代码中进一步输入的 Sales 变量输入他们的每周利润。我认为 52 的限制会很好,因为它允许一个人平均 2 周、一个月、6 个月等,就像他们正在尝试预算或寻找估计的年收入一样。
//new method
public static void computeAverageSalesProfit() {
//new scanner object in method
Scanner input = new Scanner(System.in);
//new object
Sales AvgProfit = new Sales();
//prompt for input
System.out.println("Please enter weekly profits, up to 52 weeks. When finished, enter '-1.'");
//value to store input, and test against
double value = 0;
//new double array to sum together and average out
double[] sum2 = new double[52];
//while loop of doom
while(value >= 0) {
for (int i=0; i<sum2.length; i++) {
value = input.nextDouble();
sum2[i] = value + 0;
}
}
double WeeklyProfits = 0;
for (int i=0; i<sum2.length; i++) {
WeeklyProfits = sum2[i] / sum2.length;
}
AvgProfit.setWeeklyProfits(WeeklyProfits);
System.out.printf("Your average profits are %.2f", AvgProfit.getWeeklyProfits());
}
这是我晚上用的代码,但我不知道如何结束 while 循环。我最初没有 value 变量,但认为在 sum2 迭代之前检查它可以让我关闭它。当我 运行 程序时,它接受用户输入,但直到我达到 52 次迭代才结束。我也试过在布尔表达式中使用“0000”,但这也没有用。
我也试过不设置value变量,能想到的和网上能找到的方法我都试过了。我尝试使用和不使用值变量,只有一个普通的双精度值(不是数组)。我什至尝试使用字符串,使用布尔表达式 (good = true/false, while (!/good) 等,但我无法将其转换为 double 并取平均值。
我完全莫名其妙,头疼。如果您能分享任何帮助或见解,我将不胜感激。谢谢。
哦,我的代码的不同部分中有一个 while 循环确实有效,但我认为这个循环的工作方式不同。我想我要么想多了,要么只是漏掉了一些非常明显的东西。
//Check Year length separate, keeping original code mostly intact
public static boolean isValid(String input)
{
//Does it have 4 digits?
if(input.length() != 4)
return false;
// Is it a number ?
try
{
Integer i = Integer.parseInt(input);
}
catch(NumberFormatException e)
{
return false;
}
// Passed all checks and is valid
return true;
}
您可以做的一件事是不使用 while 循环,而是像这样使用 for 循环:
//value to store input, and test against
double value = 0;
//new double array to sum together and average out
double[] sum2 = new double[52];
// repeats 52 times max
for (int i=0; i<sum2.length; i++) {
value = input.nextDouble();
// Checking if the user entered a negative number
if (value < 0) {
// The break keyword exits a loop
break;
}
sum2[i] = value + 0;
}