Java 显示剩余物而不是整体
所以我需要编写一个程序,告诉你一个特定大小的文件以特定速率下载的时间Java 显示剩余物而不是整体,java,Java,所以我需要编写一个程序,告诉你一个特定大小的文件以特定速率下载的时间 // 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(); .
// 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=minutes%60
,以获取所需内容。在Java中,有一个使用%运算符的模函数。你可以像除法一样使用它,得到的是余数而不是除法结果。例如,您的分钟数可以是minutesDisplayed=minutes%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)%60;
totalHours = (totalSeconds)/3600;
totalSeconds - totalSeconds % 60;
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中有一个模运算符%
,
这是完美的这类工作,如提取秒再到完整的分钟,或分钟再到完整的小时。
基本上,%运算符计算结果如下:
1%60=1;
2%60=2;
3%60=3;
...
61%60=1;
62%60=2;
63%60=3;
...
121%60=1;
122%60=2;
123%60=3;
...
and so on...and so on...
这就是你要找的吗?:
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)%60;
totalHours = (totalSeconds)/3600;
totalSeconds - totalSeconds % 60;
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中有一个模运算符%
,
这是完美的这类工作,如提取秒再到完整的分钟,或分钟再到完整的小时。
基本上,%运算符计算结果如下:
1%60=1;
2%60=2;
3%60=3;
...
61%60=1;
62%60=2;
63%60=3;
...
121%60=1;
122%60=2;
123%60=3;
...
and so on...and so on...
使用一些
while
语句将每个totalSeconds、totalMinutes和totalHours分解为反映整个下载时间的数字
在代码中使用一些
import java.util.Scanner;
导入java.text.NumberFormat;
公共类下载时间应用程序
{
公共静态void main(字符串[]args)
{
//欢迎用户使用该程序
System.out.println(“欢迎使用下载时间估计器”);
System.out.println();//打印一个空行
//创建扫描仪对象并启动while循环
扫描仪sc=新的扫描仪(System.in);
字符串选择=“y”;
while(choice.equalsIgnoreCase(“y”))
{
//获取用户输入
System.out.print(“输入文件大小(MB):”;
double fileSize=sc.nextDouble();
//获取用户输入
系统输出打印(“输入下载速度(MB/秒):”;
双下载速度=sc.nextDouble();
//计算小时、分钟和秒
整数秒=0;
int totalMinutes=0;
整数总小时=0;
/*整小时;
整数分钟=60;
整数秒=60*/
//^^无用信息
totalSeconds=文件大小/下载速度;
同时(总秒数>=60){
总分钟数=总分钟数+1;
totalSeconds=totalSeconds-60;
}
而(总分钟数>=60){
总小时数=总小时数+1;
totalMinutes=totalMinutes-60;
}
//如果你愿意的话,在几天内添加相同的内容
/*NumberFormat number=NumberFormat.getNumberInstance();
数字。setMaximumFractionDigits(0)*/
//^^无用的格式
字符串消息=
“此下载大约需要\n”+
totalHours+“小时数\n”+
totalMinutes+“分钟数\n”+
totalSeconds+“秒\n”;
System.out.println(消息);
//查看用户是否要继续
系统输出打印(“是否继续?”);
choice=sc.next();
System.out.println();
}
}
}
使用一些while
语句将每个totalSeconds、totalMinutes和totalHours分解为反映整个下载时间的数字
在代码中使用一些
import java.util.Scanner;
导入java.text.NumberFormat;
公共类下载时间应用程序
{
公共静态void main(字符串[]args)
{
//欢迎用户使用该程序
System.out.println(“欢迎使用下载时间估计器”);
System.out.println();//打印一个空行
//创建扫描仪对象并启动while循环
扫描仪sc=新的扫描仪(System.in);
字符串选择=“y”;
while(choice.equalsIgnoreCase(“y”))
{
//获取用户输入
System.out.print(“输入文件大小(MB):”;
double fileSize=sc.nextDouble();
//获取用户输入
系统输出打印(“输入下载速度(MB/秒):”;
双下载速度=sc.nextDouble();
//计算小时、分钟和秒
整数秒=0;
int totalMinutes=0;
整数总小时=0;
/*整小时;
整数分钟=60;
整数秒=60*/
//^^无用信息
totalSeconds=文件大小/下载速度;
同时(总秒数>=60){
总分钟数=总分钟数+1;
totalSeconds=totalSeconds-60;
}
而(总分钟数>=60){
总小时数=总小时数+1;
totalMinutes=totalMinutes-60;
}
//如果你愿意的话,在几天内添加相同的内容
/*NumberFormat number=NumberFormat.getNumberInstance();
数字。setMaximumFractionDigits(0)*/
//^^无用的格式
字符串消息=
“此下载大约需要\n”+
totalHours+“小时数\n”+