Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
import java.util.Scanner;
public class TimeConversion{
public static void main(String[] args) {
int min= 0;
int hours = min / 60;
int minutes = min % 60;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the time in minutes:");
minutes = keyboard.nextInt();
System.out.print("The time is: ");
System.out.printf("%d:%02d", hours, minutes);
}
}
How do i finish the code?
try this
int hours = min / 60;
int minutes = min % 60;
System.out.print("The hours is "+hours+" and minutes is "+minutes);
EDIT
import java.util.Scanner;
public class TimeConversion{
public static void main(String[] args) {
int minutes = min % 60;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the time in minutes:");
minutes = keyboard.nextInt();
int hours = minutes / 60;
int minutes1 = minutes % 60;
System.out.print("The hours is "+hours+" and minutes is "+minutes1);
}
}
min = keyboard.nextInt();
int hours = Integer.parseInt(Math.floor(min/60));
int minutes = min % 60;
System.out.print("The time is " + hours + ":" + minutes);
Related
I have an exercise in which I need to input hours and minutes in 24 hour format and after that the program will show me the time after 15 minutes. It doesn't work when minutes overflow after 59 minutes. When I have for example the input:
15
59
My output is:
15:74
I know that after 59 minutes at a normal clock will be 16:14. How can I handle this case correctly?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int hours = in.nextInt();
int minutes = in.nextInt();
if ( minutes == 59 ){
hours = hours + 1;
minutes = 00;
minutes = minutes + 14; //because I passed 1 minute
}
else{
minutes = minutes + 15;
}
System.out.printf("%d:%02d" , hours , minutes);
}
}
In Java, you don't need to program such basic things "by hand" like in C. Java provides a lot of APIs. For example the time API. See for example LocalTime with its method plusMinutes.
It's easy you should use Modulo operation like this :
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int hours = in.nextInt();
int minutes = in.nextInt();
minutes = minutes + 15;
hours += minutes / 60;
minutes = minutes % 60;
System.out.printf("%d:%02d", hours, minutes);
}
Do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int hours = in.nextInt();
int minutes = in.nextInt();
minutes=minutes+15;
if (minutes > 59) {
hours++;
}
minutes = minutes % 60;
hours = hours % 24;
System.out.printf("%d:%02d", hours, minutes);
}
}
A sample run:
15
59
16:14
Another sample run:
23
59
0:14
I'm just new in java.I am trying to convert money to time which is 1 dollar == 3 minutes but minutes-- wont let me display the exact minutes of time in the second loop
package javaapplication8;
import java.util.Scanner;
public class JavaApplication8 {
public static void main(String[] args) {
/*
1 Dollar = 3 minutes
*/
//I am trying to convert money to time which is 1 dollar == 3 minutes but minutes--
//wont let me display the exact minutes of time in the second loop
int minutes;
int amount;
int hour=0;
int time;
Scanner in = new Scanner(System.in);
System.out.println("Please Enter Amount");
amount = in.nextInt();
time = amount * 3;
for (minutes = time; minutes>=59; minutes--)
{
minutes = minutes- 59;
hour = hour+1;
System.out.println(minutes+"minutes");
System.out.println(hour+"hour");
}
}
}
So if money == 40 here is the problem:
Please enter amount:
40
61 minutes
1 hour
here lies the problem it should be 2 minutes but because of minutes-- its not,
1 minutes
2 hour
Your second loop is a weird way of implementing it you should replace minute-- by minute-=60 like so:
for (minutes = time; minutes>=60; minutes-=60) {
hour = hour+1;
}
but you could just implement it by using arithmetic:
hour = minute/60;
minute = minute % 60;
I am writing a program to convert minutes into the time of day. How can I write this to keep the hours in standard time, the hour should never exceed 12.
import java.util.*;
public class MinutesConverter {
public static void main(String[] args) {
int minutes;
int hours;
Scanner input = new Scanner(System.in);
System.out.print("Enter minutes: ");
minutes = input.nextInt();
hours = minutes / 60;
minutes %= 60;
System.out.print(hours + ":" + minutes);
}
}
You can make a little change in code, where you are assigning value to hour
here is what i can suggest:
hour = ( minutes / 60 ) % 24;
In the textbook it explains how to convert seconds into minutes with how many seconds remain but the question I have is as follows.
Write a program that prompts the user to enter the minutes (e.g., 1
billion), and displays the number of years and days for the minutes.
For simplicity, assume a year has 365 days. Here is an example.
Enter the number of minutes: 100000000
100000000 minutes is approximately 1902 years and 214 days.
What I currently have is as follows:
import java.util.Scanner;
public class Ch2_Exercise2_7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt user for number of minutes
System.out.println("Enter the number of minutes:");
int minutes = input.nextInt();
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
System.out.println(minutes + " minutes is " + year + " years and " + remainingMinutes + " days ");
}
}
With what I have it isn't giving me the remaining minutes into days. For example, if I put in 525600 minutes it gives me 1 year and 365 days when it should just be 1 year.
I'm using Java Eclipse. Any help would be greatly appreciated! My apologies in advance if I post the code incorrectly.
You screwed up a bit over here:
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
You took the total number of minutes and divided by 1440, so the number of days you got was wrong. You should have taken the remainder and then divided by 1440.
Another thing was in your print statement. You wrote the number of minutes left after one year as the number of days.
This should work:
// Number of minutes in a year
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = remainingMinutes / 1440;
System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days.");
My main method is separate:
public class MinutesToYearsDaysCalculator {
public static void printYearsAndDays(long minutes) {
double days = minutes / 60 / 24;
double years = days / 365;
double remainingDays = days % 365;
System.out.println((minutes < 0 ? "Invalid Value" : minutes + " min = " + (int) years + " y and " + (int) remainingDays + " d"));
}
}
int mins,hours,remainingDays,days,years;
cout << "Enter minutes:";
cin >> mins;
hours = mins/60;
days = hours/24;
years = days/365;
remainingDays = days % 365;
cout << years << " years " << remainingDays << " days " << endl;
return 0;
public class MinutesToYearsDaysCalculator {
public static void printYearsAndDays(long minutes)
{
if(minutes<0) System.out.println("Invalid Value");
else
{
long hours = minutes/60;
long day = hours/24;
long years = day/365;
long remainingDays = day % 365;
System.out.println(minutes +" min = "+ years+" y and "+remainingDays +" d");
}
}
import java.util.Scanner;
public class Prog13 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter The Minute");
int min=sc.nextInt();
int year= min/(365*24*60);
int remaining_min=(min%24*60*60);
int day=remaining_min/24*60;
System.out.println( min + " " + " Minutes is approximately:"+ year + " " +"year"+ ' '+ day + "Day" );
}
}
hello you can use this result
import java.util.Scanner;
public class MinToYearsAndDays {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("number of minutes:");
int minutes = input.nextInt();
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = (remainingMinutes / 1440);
System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days ");
}
}
public class Mints_to_days_years {
// Given some numbers of minutes calculate the number of days and years
// from the given minutes.
// 1hr = 60 minutes
// 1 day = 24 hours
// 1 year = 365 days
public static void find_days_years(long minutes) {
if(minutes < 0) {
System.out.println("Invalid number of minutes.");
}
// to find the number of years, divide the nminutes by total numbers of minutes in a year.
long years = minutes / (60 * 24 * 365);
// then find the remainder minutes that does not goes exactly into year
long years_remainder = minutes % (60 * 24 * 365);
// divide the remainder by number of minutes in a day to find the numbers of days
long days = years_remainder / (60 * 24);
System.out.println(minutes + " mints = " + years + " yrs and " + days + " days.");
}
public static void main(String[] args) {
find_days_years(525600);
find_days_years(1051200);
find_days_years(561600);
}
}
Here is my code:
public class numberOfYears {
public static void main(String args[]){
String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
double mins = Double.parseDouble(minsString);
//calcuate mins in a year
double minsOfaYear = 365*24*60;
double total = mins / minsOfaYear;
double year = total / 10;
double day = total / 10;
System.out.println(year+ " years and " + day + " days");
//result isnt right
}
}
The question is basically asking to enter minutes and the program supposedly to calculate it to years and days.. for instance, if I enter 1000000000 minutes it should give a result of 1902 years and 214 days.. but I am not getting the correct result. Can someone point out the problem for me please? Thank you!
You should convert the remainders of time to the smaller units of time. In example remainder of years should be converted to days, and remainder of days should be converted to hours etc.
Could you please try this:
public class numberOfYears {
public static void main(String args[]){
String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
int mins = Int.parseInt(minsString, 16);
String time = ConvertTime(mins);
System.out.println(time);
}
}
// Convert minutes to years, days, hours an minutes
public String ConvertTime(int time){
String result = "";
int years = time/(365*24*60);
int days = (time%(365*24*60))/(24*60);
int hours= (time%(365*24*60)) / 60;
int minutes = (time%(365*24*60)) % 60;
result = years+" years " + days + " days "+ hours + " hours " + minutes + " minutes";
return result;
}
public class numberOfYears {
public static void main(String args[]){
String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
double mins = Double.parseDouble(minsString);
//calcuate mins in a year
double minsOfaYear = 365*24*60;
double year = mins / minsOfaYear;
double day = (mins % minsOfaYear)/(24*60);
System.out.println((int)year+ " years and " + (int)day + " days");
}
}
For this kind of math problem, we need to make use of integer division. If the number of minutes works out to be 45.8 years, you want this to be calculated as 45 years and then convert the remainder of 0.8 years into days. Its not clear to me why your code has a / 10
Try this:
public class numberOfYears {
static double minsOfaYear = 365*24*60;
public static void main(String args[]) {
String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
double mins = Double.parseDouble(minsString);
//calcuate mins in a year
int year = (int) (mins / minsOfaYear);
double day = mins - year*minsOfaYear;
System.out.println(year+ " years and " + day + " days");
}
}
I would do it this way
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c2.add(Calendar.MINUTE, 1000000000);
int year1 = c1.get(Calendar.YEAR);
int day1 = c1.get(Calendar.DAY_OF_YEAR);
int year2 = c2.get(Calendar.YEAR);
int day2 = c2.get(Calendar.DAY_OF_YEAR);
int years;
int days;
if (day2 > day1) {
years = year2 - year1;
days = day2 - day1;
} else {
c2.add(Calendar.YEAR, -1);
years = year2 - year1 - 1;
days = c2.getActualMaximum(Calendar.DAY_OF_YEAR) - day1 + day2;
}
System.out.println(years + " years " + days + " days from now");
output
1901 years 119 days from now