English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Write the definition of a method dashedLine , with one parameter, an int .

If the parameter is negative or zero, the method does nothing. Otherwise it prints a complete line terminated by a newline to standard output consisting of dashes (hyphens) with the parameter's value determining the number of dashes. The method returns nothing.

Here is what I wrote and is not working:

void dashedLine(int a){};
{
if (a>=0)
{
a=0;
}
if (a > '-' )
{
a= '-';
}
else
{

System.out.println( "a: " ='-');
}
return

Thanks for any help ...

2007-12-15 04:04:05 · 5 answers · asked by cheryl b 1 in Computers & Internet Programming & Design

5 answers

What they said, plus you don't need to specifically check if a > 0, the for loop just won't execute if it isn't.

2007-12-15 04:46:43 · answer #1 · answered by Whatever 3 · 0 0

The syntax and the logic is wrong.
I'm going to give you some pseudo code it wont compile but it will be road map:

public void dashedLine(int a)
{
int i
if a <=0 then return //don't do anything
else
{
use for loop with condition i {
print "-"
}
}
}

2007-12-15 04:37:19 · answer #2 · answered by cpomp 2 · 1 0

a is an integer controlling how many dashes to print, so it doesn't make sense to compare that integer to the '-' character, or assign something to it. Your logic here is pretty simple: If a is greater than 0, print out a dash character a times.

if (a > 0) {
int i;
for (i = 1; i <= a; i = i + 1) {
System.out.print("-");
}
System.out.println(" ");
System.out.println("Printed "+a+" dashes");
}

2007-12-15 04:37:48 · answer #3 · answered by daa 7 · 0 0

A quick answer: change this...
void dashedLine(int a){};
{
...to this...
void dashedLine(int a)
{
...Otherwise, the compiler thinks the function is being defined as an empty function.

2007-12-15 04:25:43 · answer #4 · answered by Jim F 1 · 2 1

void dashedLine(int x)
{
for (int i=0;i System.out.print("-");
}
System.out.print("\n");
}

2016-03-17 11:50:41 · answer #5 · answered by ? 1 · 0 0

fedest.com, questions and answers