if else
Jeg får da godt nok livet noget op i denne java tråd ;-)Den næste opgave hedder:
Modify your printDetails method to include printing the reference
number. However, the method should only print the reference number if it has been set - that is, the refNumber string has a non-zero length. If it has not been set, then print the string, "ZZZ" instead. Hint: Use a conditional statement whose test calls the length method on the refNumber string.
__________________________________________________________
Koden ser sådan ud:
/**
* A class that maintains information on a book.
* This might form part of a larger application such
* as a library system, for instance.
*
* @author (Insert your name here.)
* @version (Insert today's date here.)
*/
class Book
{
// The fields.
private String author, title, refNumber;
private int pages;
/**
* Set the author and title fields when this object
* is constructed.
*/
public Book(String bookAuthor, String bookTitle, String refNumber, int pages)
{
author = bookAuthor;
title = bookTitle;
this.pages = pages;
refNumber = ("");
}
// Add the methods here ...
public String getAuthor()
{
return author;
}
public String gettitle()
{
return title;
}
public void printAuthor()
{
System.out.println(author);
}
public void printTitle()
{
System.out.println(title);
}
public int getpages()
{
return pages;
}
public void printDetails()
{
System.out.println("Title:" + title);
System.out.println("Author:" + author);
System.out.println("pages:" + pages);
}
public void setRefNumber(String ref)
{
refNumber = ref;
}
public String getRefNumber()
{
return refNumber;
}
}
______________________________________________________
Jeg har prøvet at skrive dette:
public void printDetails()
{
System.out.println("Title:" + title);
System.out.println("Author:" + author);
System.out.println("pages:" + pages);
if (refNumber.length() = 0)
{
System.out.println("Indtast min et tegn");
}
else
{
System.out.println("zzz");
}
}
Men får en fejl ved refNumber.length. Jeg er lidt i tvivl, fordi sådan som jeg har forstået det, skal det hentes fra constructoren, hvor der nu står:
refnumber = "()";
Men der får jeg en fejl?
Mvh