Java 如何写天和公里
一个旅行者每天做几公里的旅行。任务是创建一个方法,显示他从旅程开始的路径,如下所示:第1天:10km;第2天:7公里;第3天:13公里;。该方法适用于任何旅行天数 我编写了以下代码,但它只能工作5天: ` ` 你能帮我写几天的代码吗。 例如,在第17天,他跑了8公里。 如何编写这个程序没有开关和案例。Java 如何写天和公里,java,Java,一个旅行者每天做几公里的旅行。任务是创建一个方法,显示他从旅程开始的路径,如下所示:第1天:10km;第2天:7公里;第3天:13公里;。该方法适用于任何旅行天数 我编写了以下代码,但它只能工作5天: ` ` 你能帮我写几天的代码吗。 例如,在第17天,他跑了8公里。 如何编写这个程序没有开关和案例。 事先非常感谢如果我们不知道您是如何获得这些数字的,我们真的无法提供帮助,但您可以使用地图 假设你的人一天可以走50公里。您可以使用: public static void main(String[
事先非常感谢如果我们不知道您是如何获得这些数字的,我们真的无法提供帮助,但您可以使用地图 假设你的人一天可以走50公里。您可以使用:
public static void main(String[] args) {
Map<Integer, Integer> walks = new HashMap<Integer, Integer>();
int nbDays = 20; //number of days he'll walk
int maxKmPerDay = 50;
// This fills up your map with distances and days
Random r = new Random();
for (int i = 0; i < nbDays; i++) {
walks.put(i, r.nextInt(maxKmPerDay ));
}
for (int i = 0; i < nbDays; i++) {
System.out.println("Distance = " + walks.get(i) + " km.");
}
}
如果你想让他一遍又一遍地重复相同的距离,请原谅我在这里使用C,因为我不是坐在JavaIDE前面;不过,Java应该非常相似:
private static void Distances()
{
Console.WriteLine("Enter the number of the day:");
int day;
// Keep prompting the user until they enter a valid integer
while (!int.TryParse(Console.ReadLine(), out day))
{
Console.WriteLine("Please enter a valid integer");
}
String distance = null;
// Use modular arithmetic to determine
int multipleOf5 = day % 5;
switch (multipleOf5)
{
// Even multiple of 5
case 0:
distance = "9 km";
break;
// E.g.: day 6 should be 10 km again
case 1:
distance = "10 km";
break;
case 2:
distance = "7 km";
break;
case 3:
distance = "13 km";
break;
case 4:
distance = "11 km";
break;
}
Console.WriteLine(distance);
}
在这种情况下,例如,第6天与第1天的距离与第11天的距离相同。同样,第7天的距离与第2天的距离相同
如果它真的是完全随机的,并且你不关心它在后续调用中是否保持一致,那么每次你只需要生成一些随机数——你甚至不关心用户输入是什么。这只是一个随机数
Random r = new Random();
// He walks some distance from, for example, 5 km to 30 km
int distance = r.Next(5, 30);
Console.WriteLine(distance + " km");
如果您希望这样做,它会变得稍微复杂一些。在这种情况下,您希望将结果存储在哈希表中:
// Build a hash table
private static Dictionary<int, int> dict = new Dictionary<int, int>();
// In C#, you must use the same instance of Random across all callsdue to the fact that
// the pseudorandom number generator is seeded by time
private static void DistanceCached(Random r)
{
Console.WriteLine("Enter the number of the day:");
int day;
// Keep prompting the user until they enter a valid integer
while (!int.TryParse(Console.ReadLine(), out day))
{
Console.WriteLine("Please enter a valid integer");
}
// Generate a value for this day if we haven't yet
if (!dict.ContainsKey(day))
{
dict[day] = r.Next(5, 30);
}
Console.WriteLine(dict[day] + " km");
}
如果你想变得非常复杂,你可以序列化它并反序列化它,这样它在程序的多个调用中是一致的。有什么公式可以计算距离吗?看起来像是随机距离。所以你可能想用一个生成器来获得随机距离并将它们相加?我想当你掌握了距离是如何计算的时候,你会自己回答这个问题的。在我看来,这并不是一个编程问题,但您显然不了解您手上的功能问题!这应该如何工作?你如何计算出超过5天的距离?或者你只是重复相同的距离?第6天的结果应该是什么?又跑了10公里?非常感谢你的帮助much@Vit没问题,这就是网站的目的:如果这回答了你的问题,请随意将其标记为已接受的答案,这样问题就结束了。
Random r = new Random();
// He walks some distance from, for example, 5 km to 30 km
int distance = r.Next(5, 30);
Console.WriteLine(distance + " km");
// Build a hash table
private static Dictionary<int, int> dict = new Dictionary<int, int>();
// In C#, you must use the same instance of Random across all callsdue to the fact that
// the pseudorandom number generator is seeded by time
private static void DistanceCached(Random r)
{
Console.WriteLine("Enter the number of the day:");
int day;
// Keep prompting the user until they enter a valid integer
while (!int.TryParse(Console.ReadLine(), out day))
{
Console.WriteLine("Please enter a valid integer");
}
// Generate a value for this day if we haven't yet
if (!dict.ContainsKey(day))
{
dict[day] = r.Next(5, 30);
}
Console.WriteLine(dict[day] + " km");
}