これはヒントを計算するためのプログラムです。私はhoursArray []を使って各セルを普通に丸める必要があります(Math.round()を使って私の知る限りの最善の方法です)。問題は、私はまだtotalHours []に格納するためにUNrounded hoursArray []が必要であるということです。所望の出力の配列のセルを丸めて残したまま、セルを丸めて表示する問題
例:ボブは33.49時間の作品とサラは33.5時間動作しますが、一週間のためのヒントだからボブの時間は33に切り捨てなければならない$ 200.00
あり、そしてサラのは34 にあなたを切り上げますでしょうボブ以外のサラとの差を知ることができます。しかし、週の合計時間はまだ66.99として表示されます。
は今、このプログラムは、を行うことになっています他のすべてを行い、私はちょうど[] hoursArrayの各セルを丸め、まだ無傷で[] totalHoursを維持する方法を見つけ出すことはできません。
hoursArray []で各セルを丸めて、totalHours []に格納されているUNrounded値を保持する方法はありますか? hoursArrayのクローン()を使用しますか? arrayCopy()?私はこだわっています...
と行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();
}
}
}
floor()は、常に入力よりも小さい次の数値に切り捨てられます。 33.5のような値を丸めて34にするには、round()を使用する必要があります。問題は計算中です。私はあなたのようなものを意味すると仮定します:Math.round(hoursArray [x])/ totalHours * totalTips。 totalHoursの配列は、指定された配列 "セル"に丸められた値を格納しない限り、影響を受けません – Supahupe
Math.floor()はヒントを下に丸めなければならないので、私はそれを編集しました。将来の視聴者。あなたのご意見ありがとうございます。 –
次に、次のようにします。Math.round(hoursArray [x])/ totalHours * Math.floor(totalTips)。これは指定された配列インデックスの時間を切り上げまたは上げますが、正しい値にとどまるtotalHoursには影響せず、totalTipを切り捨てます。 – Supahupe