gpt4 book ai didi

java - 显示剩余部分而不是全部

转载 作者:行者123 更新时间:2023-12-01 20:20:47 24 4
gpt4 key购买 nike

所以我需要编写一个程序来告诉特定大小的文件将以特定速率下载多长时间。

// get user input 
System.out.print("Enter file size (MB): ");
double fileSize = sc.nextDouble();

//get user input
System.out.print("Enter download speed (MB/sec): ");
double downloadSpeed = sc.nextDouble();
.
.
code body is here
.
.
String message =
"This download will take approximately\n" + number.format(totalHours)
+ " hours\n" + number.format(totalMinutes) + " minutes\n"
+ number.format(totalSeconds) + " seconds\n" ;
System.out.println(message);

这就是基本思想。您输入所需的文件大小和所需的下载速率,它会告诉您以给定速率下载需要多长时间。所以我想知道如何做到这一点,但我没有得到如下结果:

Welcome to the Download Time Estimator

Enter file size (MB): 20000
Enter download speed (MB/sec): 5.0
This download will take approximately
1 hours
67 minutes
4,000 seconds

Continue? (y/n):

如何才能在结果中给出余数? (一小时 60 分钟,一分钟 60 秒)这有道理吗?这是我现在的代码:

import java.util.Scanner;
import java.text.NumberFormat;

public class DownloadTimeApp
{

public static void main(String[] args)
{
// welcome the user to the program
System.out.println("Welcome to the Download Time Estimator");
System.out.println(); // print a blank line

// create Scanner object and start a while loop
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get user input
System.out.print("Enter file size (MB): ");
double fileSize = sc.nextDouble();

//get user input
System.out.print("Enter download speed (MB/sec): ");
double downloadSpeed = sc.nextDouble();

// calculate the hour, minutes and seconds
double totalSeconds;
double totalMinutes;
double totalHours;
int hour;
int minutes = 60;
int seconds = 60;


totalSeconds = fileSize / downloadSpeed;
totalMinutes = (totalSeconds)/60;
totalHours = (totalSeconds)/3600;

NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(0);

String message =
"This download will take approximately\n" +
number.format(totalHours)
+ " hours\n" + number.format(totalMinutes) + " minutes\n"
+ number.format(totalSeconds) + " seconds\n" ;
System.out.println(message);

// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}

最佳答案

在Java中,有使用%运算符的模函数。您可以像除法一样使用它,您将得到余数而不是除法结果。例如,您的分钟数可以是 minutesDisplayed = 分钟 % 60,以便获得您想要的内容。

关于java - 显示剩余部分而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44767641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com