0

仍在研究这个愚蠢的时间计划....我的新问题在于计算,分钟似乎并没有增加总数,我不确定为什么...任何输入输出将一如既往地受到高度赞赏: )

public  class Time {

    public int Minutes, Hours, TotalMin;
    private String  correctedMinutes ;
    public String valid, TimeofDay;
    public String error, noerror;


    //-----------------------------------------------------------------
    //  Sets up the time by defining its minutes and hours 
    //-----------------------------------------------------------------

public Time() {
    this.Hours = 0;
    this.Minutes = 0;}

public Time(int hour, int minute, String timeofday) {
    this.Hours = hour;
    this.Minutes = minute;
    this.TimeofDay = timeofday;}


public String testTimeofDay ( String timeofday)

{ 
    if  (TimeofDay.equals ("AM")) {TimeofDay="AM";
         return "";}

     else if (TimeofDay.equals("PM")) {TimeofDay="PM";
         return "";}

        else {TimeofDay="AM";
        return "Invalid Entry: AM/PM";}
    }

public String testHoursMinutes(int hours, int minutes)

{
    if (Minutes>59|| Minutes<0||Hours>23 || Hours<0) 
        {Minutes=0;
        Hours=0;
        TimeofDay="AM";
        return "Invalid Entry: Incorrect Value";}

            else {Hours=hours;
            Minutes=minutes;
            return "";}



}


public int addMinutes (int hours, int minutes, int elapsedMin)
    { 
        if (elapsedMin<0)
            TotalMin=minutes;

            else {
            TotalMin=elapsedMin+Minutes;}

            return TotalMin;}

public String ConvertMintoHour (int hours, int minutes){        
        while (TotalMin>60)
            {TotalMin=TotalMin-60;
            Hours=Hours+1;   
            if (Hours==13)
             { Hours=0;

             Minutes=TotalMin;
             }}
return(Hours+ " " +Minutes);




    }   
public String toString ()
        {
            String correctedMinutes = String.format("%02d", Minutes);
            return (Hours+ ":" + correctedMinutes + TimeofDay);     }





}

这是测试代码:

import java.util.Scanner;


public class TimeTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


int minutes, hours, elapsedMin; 
String timeofday, startTime;

Scanner scan=new Scanner (System.in);

System.out.println("Please enter starting hour (1-12):");
hours=scan.nextInt();
System.out.println("Please enter starting minute (0-59):");
minutes=scan.nextInt();
System.out.println("Please enter AM or PM:");
timeofday=scan.next();


Time classtime = new Time (hours, minutes, timeofday);

System.out.println(classtime.testTimeofDay(timeofday));
System.out.println(classtime.testHoursMinutes (hours, minutes));
 System.out.println(classtime.toString());


System.out.print("The class begins at ");
System.out.println (classtime.toString());

startTime=classtime.toString();

System.out.println("Enter a time in minutes representing the class duration:");
elapsedMin=scan.nextInt();

classtime.addMinutes (hours, minutes, elapsedMin);
System.out.println ("Class End Time:" + classtime.toString());


System.out.print(elapsedMin + " minutes after "+ startTime + " is ");
System.out.println (classtime.toString());





System.out.print("The class ends at ");
System.out.println (classtime.toString());


System.out.print("The class ends at ");

System.out.print("MILITARY");
}}
4

3 回答 3

1

在方法String ConvertMintoHour (int hours, int minutes)中。改变你的条件

while (TotalMin>60)

while (TotalMin>59)
于 2013-10-23T05:59:19.703 回答
1

你有很多问题,但让我们从addMinutes方法开始......

public int addMinutes(int hours, int minutes, int elapsedMin) {
    if (elapsedMin < 0) {
        TotalMin = minutes;
    } else {
        TotalMin = elapsedMin + Minutes;
    }

    return TotalMin;
}

基本上,hours永远minutes不会,没有意义,应该被丢弃。

您添加elapsedMinTotalMin但它从不用于产生输出。

相反,您应该将值应用回HoursandMinutes变量,例如...

public int addMinutes(int elapsedMin) {

    int time = (Hours * 60) + Minutes;
    time += elapsedMin;

    Hours = time / 60;
    time = time % 60;
    Minutes = time;

    return time;

}

这个概念也可以应用于您的ConvertMintoHour方法;)

一个更简单的解决方案是将时间的内部概念管理为自午夜以来的分钟数。

这将允许您从“时间”课程中简单地增加或减少分钟。准备好后,您可以使用上述算法生成生成输出所需的小时和分钟...恕我直言

我确定其他人已经向您指出了这一点,但是您应该花时间阅读Java 编程语言的代码约定,这将有助于使您的代码更易于其他开发人员阅读;)

于 2013-10-23T06:10:24.603 回答
0

该方法ConvertMintoHour可以简单地写成如下:

public String ConvertMintoHour() {
        int noOfHrs = TotalMin / 60;
        Hours = (Hours + noOfHrs) % 13;
        Minutes = TotalMin % 60;
        return Hours + " " + Minutes;
}
于 2013-10-23T06:23:12.097 回答