alphasil Posted October 7, 2012 at 02:06 PM Report #478123 Posted October 7, 2012 at 02:06 PM 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
brunoais Posted October 7, 2012 at 04:52 PM Report #478144 Posted October 7, 2012 at 04:52 PM 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í. 1 Report "[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%.
alphasil Posted October 8, 2012 at 12:41 PM Author Report #478247 Posted October 8, 2012 at 12:41 PM 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
alphasil Posted October 8, 2012 at 01:02 PM Author Report #478249 Posted October 8, 2012 at 01:02 PM 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
HappyHippyHippo Posted October 8, 2012 at 01:42 PM Report #478257 Posted October 8, 2012 at 01:42 PM que tipo de dados é : hours.getDisplayValue() ?? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
alphasil Posted October 8, 2012 at 01:57 PM Author Report #478258 Posted October 8, 2012 at 01:57 PM 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
HappyHippyHippo Posted October 8, 2012 at 02:06 PM Report #478259 Posted October 8, 2012 at 02:06 PM Nunca tinha visto esse tipo de dado (NumberDisplay) nem eu nem o google ... é por isso que copy-paste de código que se vê na net nunca funciona IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
alphasil Posted October 8, 2012 at 02:25 PM Author Report #478260 Posted October 8, 2012 at 02:25 PM 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
HappyHippyHippo Posted October 8, 2012 at 02:40 PM Report #478262 Posted October 8, 2012 at 02:40 PM Então se é código que não faz parte da API do Java como querias que fosse possivel saber ?? Pelos incríveis poderes de adivinhação dos participantes do fórum ? 1º - diz exactamente qual o erro apresentado (toda a mensagem) 2º - diz em que linha IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
alphasil Posted October 8, 2012 at 04:57 PM Author Report #478279 Posted October 8, 2012 at 04:57 PM 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
HappyHippyHippo Posted October 8, 2012 at 04:59 PM Report #478281 Posted October 8, 2012 at 04:59 PM se é isso, que tal em vez de usares as funções getDisplayValue usares somente getValue ?? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
alphasil Posted October 8, 2012 at 05:25 PM Author Report #478289 Posted October 8, 2012 at 05:25 PM 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
HappyHippyHippo Posted October 8, 2012 at 06:07 PM Report #478299 Posted October 8, 2012 at 06:07 PM (edited) o valha-me ... int horas = hours.getValue(); ou melhor : não uses essa variável !!! usa a função directamente !!! Edited October 8, 2012 at 06:07 PM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
alphasil Posted October 8, 2012 at 06:48 PM Author Report #478306 Posted October 8, 2012 at 06:48 PM Oi HHH É isto mesmo, obrigado. cumps gmc11
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now