New Posts  All Forums:Forum Nav:

help on this java

post #1 of 6
Thread Starter 
WAP that will accept a seconds, compute & display the corresponding hours,minute,seconds.

Sample output:
Enter Seconds:120
00:02:00
post #2 of 6
OK COOL
looks like this is old but.... I am teaching myself Java and decided to use this as a Homework assignment;

Please anyone let me know if i can better clean this up..... Only been a week since starting lol thumb.gif
Code:
import java.util.Scanner;

public class seconds {
        public static void main(String[] args){
                
                int sec,min,hours,secr,minr;
                sec=min=hours=secr=minr=0;
                
                Scanner user_input = new Scanner(System.in);
                
                System.out.print("Please input seconds:");
                sec = user_input.nextInt();
                
                if (sec >= 60)
                {
                                min = (sec / 60);
                                secr = (sec % 60);
                                if (min >= 60)
                                {
                                                hours = (min / 60);
                                                minr = (min % 60);
                                }else{
                                                minr = min;
                                                }
                }else{
                        secr=sec;
                }
                
                
                System.out.println(String.format("%02d", hours) + ":" + String.format("%02d", minr) + ":" + String.format("%02d", secr));
        }
}

Edited by FuzzyPants - 8/7/12 at 5:16am
Riggington
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6100 GIGABYTE GA-990XA-UD3 AM3+ BFG Tech GeForce GTX 275 G.SKILL Sniper 8GB (2 x 4GB) DDR3 1600 
Hard DriveOptical DriveCoolingOS
Seagate Raid 0 1TB DVD-RW Cooler Master V8 Windows 7 
KeyboardPowerCase
Razer Black Widow 550 Watt Cooler Master Haf 922 
  hide details  
Reply
Riggington
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6100 GIGABYTE GA-990XA-UD3 AM3+ BFG Tech GeForce GTX 275 G.SKILL Sniper 8GB (2 x 4GB) DDR3 1600 
Hard DriveOptical DriveCoolingOS
Seagate Raid 0 1TB DVD-RW Cooler Master V8 Windows 7 
KeyboardPowerCase
Razer Black Widow 550 Watt Cooler Master Haf 922 
  hide details  
Reply
post #3 of 6
Use System.out.printf for your output, it will eliminate the need to use String.format and dramatically simplify your final output expression, Google it :-)
Edited by dushan24 - 8/10/12 at 4:04am
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II SLI MSI GTX560 Ti TwinFrozr II SLI 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 1600MHz CL9 XMS3 (2 x 4GB) OCZ Vertex 3 SSD Western Digital Caviar Black 1TB 7200RPM 64MB C... Western Digital Caviar Green 1TB ~5900RPM 64MB ... 
Optical DriveCoolingOSMonitor
Lite-On 24x DVD-RW CoolerMaster V8 Windows 7 Professional SP1 3 x Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II SLI MSI GTX560 Ti TwinFrozr II SLI 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 1600MHz CL9 XMS3 (2 x 4GB) OCZ Vertex 3 SSD Western Digital Caviar Black 1TB 7200RPM 64MB C... Western Digital Caviar Green 1TB ~5900RPM 64MB ... 
Optical DriveCoolingOSMonitor
Lite-On 24x DVD-RW CoolerMaster V8 Windows 7 Professional SP1 3 x Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
post #4 of 6
Quote:
Originally Posted by rentaw View Post

WAP that will accept a seconds, compute & display the corresponding hours,minute,seconds.
Sample output:
Enter Seconds:120
00:02:00

Sounds like a homework assignment wink.gif
HAL9000
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k @ 4.5GHz Asus P8Z68-V Pro EVGA GTX680 SC Signature 2 G.SKILL 8GB (2 x 4GB) Ripjaws X Series DDR3 160... 
Hard DriveHard DriveCoolingOS
WD Caviar Black 1Tb Intel 520 120gb ssd Corsair H100i Windows 7 Home Premium (64 bit) 
MonitorKeyboardPowerCase
Samsung SyncMaster 2433 Steelseries 6GV2 Corsair AX860i CoolerMaster Cosmos 2 
Mouse
Steelseries Sensei 
  hide details  
Reply
HAL9000
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k @ 4.5GHz Asus P8Z68-V Pro EVGA GTX680 SC Signature 2 G.SKILL 8GB (2 x 4GB) Ripjaws X Series DDR3 160... 
Hard DriveHard DriveCoolingOS
WD Caviar Black 1Tb Intel 520 120gb ssd Corsair H100i Windows 7 Home Premium (64 bit) 
MonitorKeyboardPowerCase
Samsung SyncMaster 2433 Steelseries 6GV2 Corsair AX860i CoolerMaster Cosmos 2 
Mouse
Steelseries Sensei 
  hide details  
Reply
post #5 of 6
Quote:
Originally Posted by the_duality View Post

Sounds like a homework assignment wink.gif

It does rather...
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II SLI MSI GTX560 Ti TwinFrozr II SLI 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 1600MHz CL9 XMS3 (2 x 4GB) OCZ Vertex 3 SSD Western Digital Caviar Black 1TB 7200RPM 64MB C... Western Digital Caviar Green 1TB ~5900RPM 64MB ... 
Optical DriveCoolingOSMonitor
Lite-On 24x DVD-RW CoolerMaster V8 Windows 7 Professional SP1 3 x Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II SLI MSI GTX560 Ti TwinFrozr II SLI 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 1600MHz CL9 XMS3 (2 x 4GB) OCZ Vertex 3 SSD Western Digital Caviar Black 1TB 7200RPM 64MB C... Western Digital Caviar Green 1TB ~5900RPM 64MB ... 
Optical DriveCoolingOSMonitor
Lite-On 24x DVD-RW CoolerMaster V8 Windows 7 Professional SP1 3 x Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
post #6 of 6
Yes that is why i waited a week before answering him lol

Dushan: Yes i was just reading up on printf
Riggington
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6100 GIGABYTE GA-990XA-UD3 AM3+ BFG Tech GeForce GTX 275 G.SKILL Sniper 8GB (2 x 4GB) DDR3 1600 
Hard DriveOptical DriveCoolingOS
Seagate Raid 0 1TB DVD-RW Cooler Master V8 Windows 7 
KeyboardPowerCase
Razer Black Widow 550 Watt Cooler Master Haf 922 
  hide details  
Reply
Riggington
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6100 GIGABYTE GA-990XA-UD3 AM3+ BFG Tech GeForce GTX 275 G.SKILL Sniper 8GB (2 x 4GB) DDR3 1600 
Hard DriveOptical DriveCoolingOS
Seagate Raid 0 1TB DVD-RW Cooler Master V8 Windows 7 
KeyboardPowerCase
Razer Black Widow 550 Watt Cooler Master Haf 922 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming