public class PrintStatement
{
public static int posNum(int x)
{
while (x <=0)
{
System.out.println("Enter a number greater than 0");
}
return x;
}
public static void main(String[] args)
{
int myNumEntry, howAreYou; // variable declaration
Scanner input= new Scanner( System. in);
System.out.println("Enter an integer number : "); //prompt user for input
myNumEntry = input.nextInt();
howAreYou = posNum(myNumEntry); // pass input to method getPos
for (int i = 0; i <= howAreYou; i++)
System.out.println("How Are You?" );
}
}
24 Replies - 1560 Views - Last Post: 05 July 2011 - 11:04 PM
#1
Java method: prints statement number of times entered by user
Posted 02 July 2011 - 12:37 PM
Replies To: Java method: prints statement number of times entered by user
#2
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 12:45 PM
In this case, you need simply to add an import statement. Can you figure out what import statement is required?
This post has been edited by GregBrannon: 02 July 2011 - 12:46 PM
#3
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 01:14 PM
#4
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 01:21 PM
this while will loop for ever if ever it is entered (if x <= 0)
#5
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 02:11 PM
import java.util.*;
public class printStatement
{
public static int posNum(int x)
{
if (x <=0)
{
System.out.println("Enter a number greater than 0");
}
return x;
}
public static void main(String[] args)
{
int myNumEntry, howAreYou;
Scanner input= new Scanner( System. in);
System.out.println("Enter an integer number : "); for input
myNumEntry = input.nextInt();
howAreYou = getPos(myNumEntry);
for (int i = 1; i <= helloThere; i++)
System.out.println("HowAreYou" );
}
}
#6
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 02:13 PM
System.out.println("Enter an integer number : "); for input
getPos() is not defined
neither helloThere
This post has been edited by pbl: 02 July 2011 - 02:16 PM
#7
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 02:25 PM
#8
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 02:31 PM
import java.util.*;
public class PrintStatement
{
public static int getPos(int x)
{
if (x <=0)
{
System.out.println("Enter a number greater than 0");
}
return x;
}
public static void main(String[] args)
{
int myNumEntry, helloThere;
Scanner input= new Scanner( System. in);
System.out.println("Enter an integer number : ");
myNumEntry = input.nextInt();
helloThere = getPos(myNumEntry);
for (int i = 1; i <= helloThere; i++)
System.out.println("Hello There" );
}
}
#9
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 02:52 PM
Are you compiling and running at a command line prompt? If so, show us what you're typing and then the response you're getting. Cut and paste the entire session from start to finish.
#10
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 03:01 PM
May be you should consider:
static int getPos() {
Scanner scan = new Scanner(System.in);
while(true) {
System.out.print("Enter a number > 0: ");
int x = scan.nextInt();
if(x > 0)
return x;
}
}
*Edited nextInt() not nextInput()
This post has been edited by pbl: 02 July 2011 - 05:41 PM
#11
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 03:02 PM
- you have space between System. and in it should be
Scanner input= new Scanner( System.in); // no space
#12
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 03:44 PM
smohd, on 02 July 2011 - 03:02 PM, said:
- you have space between System. and in it should be
Scanner input= new Scanner( System.in); // no space
WOW!! my code is fine and I ran it and get the results I want. It was that SINGLE space I put in. And @pbl, why is my method useless? I tried it and it works fine (i.e. the user puts in "3" and the result is "Hello There Hello There Hello There". Does it look as if something is still wrong. Thanks so much smohd!!
OH! but I do need it to run again if, say, 0 or a negative number is entered because if that happens then it just stops. Would something need to be added to the first method so it can ask again and again until a positive number is entered?
#13
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 03:50 PM
lolitacharm, on 02 July 2011 - 06:44 PM, said:
And @pbl, why is my method useless?
Right now your method prints "Enter a number greater than 0" if the parameter received is <= 0
and does nothing else... like asking for a number > 0
All the -1 you want to give me will change nothing
Your method displays "Enter a number greater than 0" and then does nothing to correct it
If the user inputs -10 in the main program, you will prompt an do not read any answer back
This is a major bug in your design despite the fact you changed your while() for a simple if... as useless
This post has been edited by pbl: 02 July 2011 - 03:57 PM
#14
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 03:57 PM
Quote
Yes, something like what pbl did in his previous post. Look at his previous post he has shown you how to make it make sense
#15
Re: Java method: prints statement number of times entered by user
Posted 02 July 2011 - 04:00 PM
|
|

New Topic/Question
Reply



MultiQuote




|