Jump to content

Recommended Posts

Posted

Olá pessoal

Estou a tentar resolver um problema que me diz :

Utilizar um formato 12h, ou seja, em que o tempo seja representado nos intervalos 01:00 AM até 12:59 AM, e 01:00 PM até 12:59 PM.

Ou seja

Estou a pensar usar uma condição mas está a dar-me um erro de "bad operand"


public ClockDisplay()
{
	hours = new NumberDisplay(24);
	minutes = new NumberDisplay(60);
	updateDisplay();

}
gmc11

 

Posted

Ou seja, não dás contexto ao erro que tens.

Tens um erro e não indicas mais para além da exceção que te aparece. Tens aí um código aparentemente aleatório, escolhido a dedo do teu programa.

O problema não está aí.

  • Vote 1

"[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31

Life is a genetically transmitted disease, induced by sex, with death rate of 100%.

Posted

Pois, não está.

O problema tem de ser resolvido aqui.


private void updateDisplay()
   {

       displayString = (hours.getDisplayValue()+ ":" + minutes.getDisplayValue());
   }

Que é onde aparecem as horas e os minutos.

O problema é que quero usar o if/else dizendo:

se horas<12, o tempo aparece AM

senão tempo aparece em PM

Não me dá a comparação visto que as horas estão com tipos de dados "NumberDisplay" e o meu 12 está como int.

Alguma dica??

gmc11

 

Posted

Pus assim mas aparece-me um warning.

private void updateDisplay()
   {
    int horas, min;
    horas = Integer.parseInt(hours.getDisplayValue());
    min = Integer.parseInt(minutes.getDisplayValue());
    if(horas>=01.00 && horas<12.59)
    {
    displayString = (hours.getDisplayValue()+ ":" + minutes.getDisplayValue()) + "AM";
   }
   else
   {
    displayString = (hours.getDisplayValue()+ ":" + minutes.getDisplayValue()) + "PM";
   }
   }

"This class cannot be parsed"...

Alguma dica?'

gmc11

 

Posted

Oi HHH

A classe está definida assim:

public class ClockDisplay
{
   private NumberDisplay hours;
   private NumberDisplay minutes;
   private String displayString;    // simulates the actual display

   /**
 * Constructor for ClockDisplay objects. This constructor
 * creates a new clock set at 00:00.
 */
   public ClockDisplay()
   {
    hours = new NumberDisplay(12);
    minutes = new NumberDisplay(60);
    updateDisplay();

   }

Nunca tinha visto esse tipo de dado (NumberDisplay)

gmc11

 

Posted

lol

Olha que o prof deu isso assim.

Deu este código e usa o bluej

Tem duas classes,

1ª chamada NumberDisplay.

/**
* The NumberDisplay class represents a digital number display that can hold
* values from zero to a given limit. The limit can be specified when creating
* the display. The values range from zero (inclusive) to limit-1. If used,
* for example, for the seconds on a digital clock, the limit would be 60,
* resulting in display values from 0 to 59. When incremented, the display
* automatically rolls over to zero when reaching the limit.
*
* @author Michael Kolling and David J. Barnes
* @version 2008.03.30
*/
public class NumberDisplay
{
private int limit;
private int value;
/**
 * Constructor for objects of class NumberDisplay.
 * Set the limit at which the display rolls over.
 */
public NumberDisplay(int rollOverLimit)
{
	limit = rollOverLimit;
	value = 0;
}
/**
 * Return the current value.
 */
public int getValue()
{
	return value;
}
/**
 * Return the display value (that is, the current value as a two-digit
 * String. If the value is less than ten, it will be padded with a leading
 * zero).
 */
public String getDisplayValue()
{
	if(value < 10) {
		return "0" + value;
	}
	else {
		return "" + value;
	}
}
/**
 * Set the value of the display to the new specified value. If the new
 * value is less than zero or over the limit, do nothing.
 */
public void setValue(int replacementValue)
{
	if((replacementValue >= 0) && (replacementValue < limit)) {
		value = replacementValue;
	}
}
/**
 * Increment the display value by one, rolling over to zero if the
 * limit is reached.
 */
public void increment()
{
	value = (value + 1) % limit;
}
}

e a 2ª classe

/**
* The ClockDisplay class implements a digital clock display for a
* European-style 24 hour clock. The clock shows hours and minutes. The
* range of the clock is 00:00 (midnight) to 23:59 (one minute before
* midnight).
*
* The clock display receives "ticks" (via the timeTick method) every minute
* and reacts by incrementing the display. This is done in the usual clock
* fashion: the hour increments when the minutes roll over to zero.
*
* @author Michael Kolling and David J. Barnes
* @version 2008.03.30
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;	// simulates the actual display

/**
 * Constructor for ClockDisplay objects. This constructor
 * creates a new clock set at 00:00.
 */
public ClockDisplay()
{
	hours = new NumberDisplay(12);
	minutes = new NumberDisplay(60);
	updateDisplay();

}
/**
 * Constructor for ClockDisplay objects. This constructor
 * creates a new clock set at the time specified by the
 * parameters.
 */
public ClockDisplay(int hour, int minute)
{
	hours = new NumberDisplay(12);
	minutes = new NumberDisplay(60);
	setTime(hour, minute);
}

/**
 * This method should get called once every minute - it makes
 * the clock display go one minute forward.
 */
public void timeTick()
{
	minutes.increment();
	if(minutes.getValue() == 0) {  // it just rolled over!
		hours.increment();
	}
	updateDisplay();
}
/**
 * Set the time of the display to the specified hour and
 * minute.
 */
public void setTime(int hour, int minute)
{
	hours.setValue(hour);
	minutes.setValue(minute);
	updateDisplay();
}
/**
 * Return the current time of this display in the format HH:MM.
 */
public String getTime()
{
	return displayString;
}

/**
 * Update the internal string that represents the display.
 */
private void updateDisplay()

{

	displayString = (hours.getDisplayValue()+ ":" + minutes.getDisplayValue());
}


}

Já vi este exemplo também na net...acho que vem de um livro.

A questão é essa, se fosse int, comparava o valor da hora e punha uma string AM ou PM conforme o caso...assim, não estou a ver.

gmc11

 

Posted

Oi

É aqui neste método que quero implementar a condição if

private void updateDisplay()
   {
	    int horas, min;
	    horas = Integer.parseInt(hours.getDisplayValue());
	    min = Integer.parseInt(minutes.getDisplayValue());
	    if(horas>=01 && horas<13)
	    {
	    displayString = (hours.getDisplayValue()+ ":" + minutes.getDisplayValue()) + "AM";
   }
   else
   {
	    displayString = (hours.getDisplayValue()+ ":" + minutes.getDisplayValue()) + "PM";
   }
   }

Diz -me que "can´t be parsed"

São estas linhas

horas = Integer.parseInt(hours.getDisplayValue());
min = Integer.parseInt(minutes.getDisplayValue());

Quero converter a variável hours em inteiro para fazer a comparação com um inteiro.

gmc11

 

Posted

Oi

assim:

int horas = Integer.parseInt(hours.getDisplayValue());

como disseste

[code=java
]int horas = Integer.parseInt(hours.getValue());
[/code]

method java.lang.integer.parseInt (java.lang.String) is not applicable.

actual argument int cannot be converted to java.lang.String by method invocation conversion

gmc11

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.