显示剩余而不是整数

时间:2017-06-26 19:52:02

标签: 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(); 
.
.
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();
       }
   }
}

3 个答案:

答案 0 :(得分:1)

在Java中,有一个使用%运算符的模数函数。您可以使用此功能,就像分割时一样,您将得到余数而不是除法结果。例如,您的分钟可以是minutesDisplayed = minutes % 60,以便获得您要查找的内容。

答案 1 :(得分:1)

这就是您要找的内容?:

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...

答案 2 :(得分:1)

使用一些https://www.mywebsitename.com/.well-known/assetlinks.json语句将每个totalSeconds,totalMinutes和totalHours分解为反映整个下载时间的数字。

在代码中使用某些内容看起来像



while




如果你将fileSize放入1000MB并将速度下载为10MB /秒,那么你就可以

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 
           int totalSeconds = 0; 
           int totalMinutes = 0; 
           int totalHours = 0; 
           /*int hour; 
           int minutes = 60; 
           int seconds = 60;*/
           //^^ Useless information


           totalSeconds = fileSize / downloadSpeed; 
           while (totalSeconds >= 60) {
              totalMinutes = totalMinutes + 1;
              totalSeconds = totalSeconds - 60;
           }
           while (totalMinutes >= 60) {
              totalHours = totalHours + 1;
              totalMinutes = totalMinutes - 60;
           }
           //Add same thing for days if you'd like

           /*NumberFormat number = NumberFormat.getNumberInstance(); 
           number.setMaximumFractionDigits(0);*/
           //^^ Useless formatting

           String message = 
           "This download will take approximately\n" + 
           totalHours + " hours\n" +
           totalMinutes + " minutes\n" +
           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();
       }
   }
}