数组中的舍入单元格同时仍显示其未舍入副本的问题
Issues with rounding cells in an Array while still displaying an UNrounded copy of it
这是一个计算小费的程序。我需要让 hoursArray[] 正常舍入每个单元格(据我所知,使用 Math.round() 是最好的方法)。问题是我仍然需要将 UNrounded hoursArray[] 存储到 totalHours[] 中。
期望输出示例:Bob 工作 33.49 小时,Sara 工作 33.5 小时,一周的小费是 $200.00
因此 Bob 的工作时间应向下舍入为 33,而 Sara 的工作时间应向上舍入为 34。您将能够看到 Sara 的小费收入与 Bob 的收入有所不同。但是一周的总小时数仍将显示为 66.99。
现在这个程序做了它应该做的所有其他事情,我只是不知道如何舍入每个单元格hoursArray[] 并且仍然保持 totalHours[] 不变。
如何舍入 hoursArray[] 中的每个单元格并仍然将未舍入的值存储到 totalHours[] 中?我是否使用 hoursArray 的 clone()?数组复制()?我卡住了...
问题区域已标记,并且在 第 33 和 43 行之间。 Math.floor() 是必需的,因为我需要程序向下舍入提示。
import java.util.Arrays;
import java.util.Scanner;
class Tips_Calculation2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
float totalTips = 0;
float totalHours = 0;
System.out.print("How many employees worked for the week?: ");
int numberOfEmps = scan.nextInt();
System.out.println("\nEnter the names of all " + numberOfEmps + " employees");
String[] namesArray = new String[numberOfEmps];
for(int i = 0; i < numberOfEmps; i++)
{
namesArray[i] = scan.next();
}
System.out.println("\nEnter the amount of hours each person worked for the week: ");
//////////////////////////////////////////////////////**THIS IS WHERE IM STUCK**///////////////
int counter = 0;
float[] hoursArray = new float[namesArray.length];
for(int n = 0; n < namesArray.length; n++)
{
System.out.print(namesArray[n] + ": ");
hoursArray[n] = scan.nextFloat();
totalHours = totalHours + hoursArray[n];
counter++;
}
//////////////////////////////////////////////////////////////////////////////////
System.out.println("\nTotal hours: " + totalHours);
System.out.print("\nEnter the amount of money to be distributed as tips: $");
totalTips = scan.nextFloat();
System.out.println("---- OUTPUT ----");
System.out.println();
int x = 0;
for(int a = 0; a < namesArray.length; a++)
{
System.out.println(namesArray[a] + ": " + "$" + Math.floor(((hoursArray[x]/totalHours) * totalTips)));
x++;
System.out.println();
}
}
}
您标记的部分看起来没问题:hoursArray 确实包含每个人工作的(未四舍五入的)小时数。
您的问题出现在稍后进行四舍五入时。根据您的描述,我了解到您想要四舍五入小时数,而不是由此产生的小费。因此,您的计算需要类似于:
Math.round(hoursArray[x]) * totalTips / totalHours
此外,当您计算总小时数时,您要添加四舍五入的小时数,否则您的小费加起来将达不到所需总数:
totalHours = totalHours + Math.round(hoursArray[n]);
正如您的问题的评论中所述,您可以使用以下提示计算每个人的小费并将其四舍五入 "down":
Math.floor(Math.round(hoursArray[x])/totalHours * totalTips);
Math.round(hoursArray[x])
将小时向上或向下舍入(33.3 到 33,33.7 到 34)。 Math.floor()
然后会向下舍入计算出的个人小费(333.3 到 333,333.7 到 333)。
这是一个计算小费的程序。我需要让 hoursArray[] 正常舍入每个单元格(据我所知,使用 Math.round() 是最好的方法)。问题是我仍然需要将 UNrounded hoursArray[] 存储到 totalHours[] 中。
期望输出示例:Bob 工作 33.49 小时,Sara 工作 33.5 小时,一周的小费是 $200.00
因此 Bob 的工作时间应向下舍入为 33,而 Sara 的工作时间应向上舍入为 34。您将能够看到 Sara 的小费收入与 Bob 的收入有所不同。但是一周的总小时数仍将显示为 66.99。
现在这个程序做了它应该做的所有其他事情,我只是不知道如何舍入每个单元格hoursArray[] 并且仍然保持 totalHours[] 不变。
如何舍入 hoursArray[] 中的每个单元格并仍然将未舍入的值存储到 totalHours[] 中?我是否使用 hoursArray 的 clone()?数组复制()?我卡住了...
问题区域已标记,并且在 第 33 和 43 行之间。 Math.floor() 是必需的,因为我需要程序向下舍入提示。
import java.util.Arrays;
import java.util.Scanner;
class Tips_Calculation2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
float totalTips = 0;
float totalHours = 0;
System.out.print("How many employees worked for the week?: ");
int numberOfEmps = scan.nextInt();
System.out.println("\nEnter the names of all " + numberOfEmps + " employees");
String[] namesArray = new String[numberOfEmps];
for(int i = 0; i < numberOfEmps; i++)
{
namesArray[i] = scan.next();
}
System.out.println("\nEnter the amount of hours each person worked for the week: ");
//////////////////////////////////////////////////////**THIS IS WHERE IM STUCK**///////////////
int counter = 0;
float[] hoursArray = new float[namesArray.length];
for(int n = 0; n < namesArray.length; n++)
{
System.out.print(namesArray[n] + ": ");
hoursArray[n] = scan.nextFloat();
totalHours = totalHours + hoursArray[n];
counter++;
}
//////////////////////////////////////////////////////////////////////////////////
System.out.println("\nTotal hours: " + totalHours);
System.out.print("\nEnter the amount of money to be distributed as tips: $");
totalTips = scan.nextFloat();
System.out.println("---- OUTPUT ----");
System.out.println();
int x = 0;
for(int a = 0; a < namesArray.length; a++)
{
System.out.println(namesArray[a] + ": " + "$" + Math.floor(((hoursArray[x]/totalHours) * totalTips)));
x++;
System.out.println();
}
}
}
您标记的部分看起来没问题:hoursArray 确实包含每个人工作的(未四舍五入的)小时数。
您的问题出现在稍后进行四舍五入时。根据您的描述,我了解到您想要四舍五入小时数,而不是由此产生的小费。因此,您的计算需要类似于:
Math.round(hoursArray[x]) * totalTips / totalHours
此外,当您计算总小时数时,您要添加四舍五入的小时数,否则您的小费加起来将达不到所需总数:
totalHours = totalHours + Math.round(hoursArray[n]);
正如您的问题的评论中所述,您可以使用以下提示计算每个人的小费并将其四舍五入 "down":
Math.floor(Math.round(hoursArray[x])/totalHours * totalTips);
Math.round(hoursArray[x])
将小时向上或向下舍入(33.3 到 33,33.7 到 34)。 Math.floor()
然后会向下舍入计算出的个人小费(333.3 到 333,333.7 到 333)。