- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
以下是一个 Java 作业问题,我已经完成了大部分,但是我在按降序显示输出时遇到了问题(从最高工作小时数开始到最低工作小时数,同时还将这些时间与工作的员工相匹配)。我用粗体 突出显示了我需要帮助的问题部分,并包含了示例输出。
问题:
计算员工的每周工时假设所有员工的每周工时都存储在一个二维数组中。每行记录一个员工的 n 天工作时间,n 列,其中 n ≥ 1 且 n ≤ 7 表示这些员工一周工作的天数。例如,下表表示一个数组,其中存储了八名员工一周 7 天的工作时间。编写一个程序,将员 worker 数和一周的工作天数作为输入。然后它包含所有员工信息(姓名和每日工作时数)。 此程序应按总时数的降序显示员工及其在一周内的总工作时数。
示例输出:
Employee 7 worked 39 hours
Employee 6 worked 37 hours
Employee 0 worked 34 hours
Employee 4 worked 32 hours
Employee 3 worked 31 hours
Employee 1 worked 28 hours
Employee 5 worked 28 hours
Employee 2 worked 24 hours
这是我到目前为止所得到的,我相信我在最后一个 for
循环中遗漏了一些东西,但这就是我发布这个的原因。非常感谢任何帮助:
import java.util.Scanner;
import java.util.Arrays;
public class EmployeeWorkHours {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("How many Employee's do you have?: ");
int NUM_OF_EMPLOYEES = scan.nextInt();
scan.nextLine();
int [][]hours;
int []totalHours= new int[NUM_OF_EMPLOYEES];
hours = new int[NUM_OF_EMPLOYEES][7];
String[] employee = new String[NUM_OF_EMPLOYEES];
// input Names
for (int x = 0; x < (employee.length); x++)
{
System.out.println("Name of Employee " + (x + 1) + ": ");
String name = scan.nextLine();
employee[x] = name;
}
// input Hours
for (int z = 0; z < employee.length; z++)
{
System.out.println("Beginning on Monday, enter the hours Employee "+ (z + 1)+ " has worked each day (Separate each day by spaces): ");
for (int a = 0; a < 7; a++)
{
hours[z][a] = scan.nextInt();
}
scan.nextLine();
}
// Print everything in for loop
for (int i = 0; i < employee.length; i++)
{
totalHours[i]=0;
for(int j=0; j<7; j ++)
{
totalHours[i] = totalHours[i]+hours[i][j];
}
// I THINK I NEED TO ADD SOMETHING HERE BUT I'M NOT SURE WHAT
// Arrays.sort(totalHours); gives me the wrong output, I'm stuck
System.out.println("Employee " + (i + 1) +" worked " + totalHours[i] + " hours");
}
}
}
最佳答案
对您的代码进行快速但性能不佳的补丁:
// Print everything in for loop
for (int i = 0; i < employee.length; i++) {
totalHours[i] = 0;
for (int j = 0; j < 7; j++) {
totalHours[i] = totalHours[i] + hours[i][j];
}
// I THINK I NEED TO ADD SOMETHING HERE BUT I'M NOT SURE WHAT
// Arrays.sort(totalHours); gives me the wrong output, I'm stuck
}
//patch starts here
int[] totalHoursSortedAsc = Arrays.copyOf(totalHours, totalHours.length);
Arrays.sort(totalHoursSortedAsc);
for (int i = totalHoursSortedAsc.length - 1;i>=0;i--) {
for (int j = 0;j < totalHours.length;j++) {
if (totalHoursSortedAsc[i] == totalHours[j]) {
System.out.println("Employee " + (j + 1) + " worked " + totalHours[j] + " hours");
totalHours[j] = -1; //Employees may work the same time
}
}
}
更好的方法是按照 Andreas 的建议,通过 Collections.sort()
对您的 EmployeeWorkHours
进行排序。看here示例代码。
关于Java Eclipse 排序然后排序 1 个数组,也许更多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36253343/
我做了一些研究,但没有找到任何东西。我不明白这样的函数是如何工作的: func :: Maybe (Int) -> Maybe (Int) 我应该如何进行模式匹配?我已经尝试过,但没有成功: func
我需要从屏幕上删除一个元素,然后重新生成一个具有相同名称的元素。 代码中有一个deleteObject函数和一个appendChild调用。 在deleteObject函数中,它使用removeChi
我读了一些关于 monad 的帖子和博客,也许,只是,什么都没有..但并没有真正明白:/在给定的代码中,我必须实现“latestActivity”函数。在我看来,它应该有效,但我不知道如何正确使用“J
在功能光学中,一个性能良好的棱镜(我相信在 scala 中称为部分透镜)应该具有 'subpart -> 'parent -> 类型的 set 函数'parent,如果棱镜“成功”并且在结构上与给定的
有输入文件;未排序的长类型数字。(约100万)我想对输入文件中的数字进行排序。为了分配数组的内存,我使用了fseek和ftell。但出现段错误。如何修复代码? int main( int argc,
我仍在尝试从单元格收集信息,使用该信息执行函数,然后将结果返回到不同的单元格。我知道这是可能的,但我正在努力解决这个问题。我在这里收到的许多提示引导我找到了以我理解的方式表达的大量信息。我希望这种事能
所以我正在制作一个小型命令行彩票游戏(代码中的注释解释了一切),但是当我生成随机数时,我的代码并没有像我希望的那样将其缩短为三位数,除了游戏可以运行并且可以玩之外,除了偶尔有一个随机生成的数字超过一千
这是另一种情况,在 C++ 中空格很重要,还是编译器错误?以下代码在语法上是否正确? #include template using EnableIf = typename std::enable
我参加了一个 PHP 工作面试,我被要求实现一段代码来检测访问者是否是爬行网站并窃取内容的机器人。 因此,我实现了几行代码,通过使用 session 变量存储上次访问时间戳来检测网站是否刷新/访问过快
我有一个List (Maybe a),我想过滤出Nothing的实例。我大概已经做到了,但是对所需的代码量却不满意: removeNothingFromList : List (Maybe a) ->
有谁知道任何指定场所开放时间的本体?例如,我有一个博物馆,有 2 个季节。淡季(指定季节起止),平日10-18:00,周六10-16(周日休息),旺季平日10-20,周末10-18。 如果没有本体,也
我有this代码(接受的解决方案)。此代码从 js 文件中截取加载。当我在此函数处放置断点时,我看到该函数在加载页面(包含它)时被调用。 初始页面加载后,当我在此页面中选择一个选项时,该 anchor
step :: [Int] -> String -> [Int] step (x:y:ys) "*" = (x*y):ys step (x:y:ys) "+" = (x + y):ys step (x
我正在尝试找到将以下有状态命令式代码转换为纯函数表示的最优雅的方法(最好在 Haskell 中使用其 Monad 实现提供的抽象)。然而,我还不擅长使用变压器等组合不同的单子(monad)。在我看来,
我希望它更方便地使用库定义的partialfunc,或者使用部分模式匹配编写回调。 像这样, partialMaybe :: forall a b. (Partial => a -> b) -> a
一周前,我在代码中编写了一个名为 getline 的函数,但该函数不起作用。从那时起,每当我将函数命名为 getline 并尝试编译它时,它都不起作用。如果我将函数名称更改为其他名称,它会再次起作用。
下面的代码有问题 package com.example.ch13_searchflickrr; import android.os.Bundle; import android.support.v7
我有这个需求,并且我使用的是MySQl数据库 评论表 id user id ariticle_id comment comment date ================
是否有 IDE 或软件可以为我的 C++ 程序计时?我目前正在使用 Visual Studio 2010,所以如果有功能可以帮助解决这个问题,我将不胜感激。 最佳答案 您将需要使用 Profiler。
我是 C# 的新手,正在研究它的可能性。 现在我对使用泛型的方式有点困惑...列出泛型的种类。我想在单个父类中创建一个基本的列表功能,只需命名我的子类应包含的类类型。 比如说,我创建了一个类 clas
我是一名优秀的程序员,十分优秀!