| | |
Help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 5
Reputation:
Solved Threads: 0
I have the following program compiling and running. However, the program is supposed to print out stars equal to the user input, with each star representing 10. If the user inputs 100 the program should print out 10 stars. No matter what number I input the program is printing out 1 star. Can someone give me a clue as to what I am doing wrong??
import java.util.Scanner;
import java.io.*;
import java.math.*;
import java.util.*;
public class GrowthofaFunction
{
//Method to calculate function
public static int calculateFunction(int x)
{
int result = 0;
final int X;
final int y1;
final int y2;
final int y;
System.out.print("Enter value for x: ");
Scanner stdin = new Scanner(System.in);
x = stdin.nextInt();
y1= x+ (x*x);
y2 = (int) (Math.pow(2,x));
y = y1 + y2;
return y;
}
//Method to printStars
public static void printStars(int numberofStars)
{
for (int i = 0; i <= numberofStars; i++)
{
for (int j = 0; j >=1; j++)
{
int x = 0;
numberofStars = (calculateFunction (x)/10);
System.out.print(" ");
}
}
}
public static void main(String[] args)
{
int x = 0;
int numberofStars = 0;
calculateFunction(x);
printStars(numberofStars);
System.out.println("*");
}
}
import java.util.Scanner;
import java.io.*;
import java.math.*;
import java.util.*;
public class GrowthofaFunction
{
//Method to calculate function
public static int calculateFunction(int x)
{
int result = 0;
final int X;
final int y1;
final int y2;
final int y;
System.out.print("Enter value for x: ");
Scanner stdin = new Scanner(System.in);
x = stdin.nextInt();
y1= x+ (x*x);
y2 = (int) (Math.pow(2,x));
y = y1 + y2;
return y;
}
//Method to printStars
public static void printStars(int numberofStars)
{
for (int i = 0; i <= numberofStars; i++)
{
for (int j = 0; j >=1; j++)
{
int x = 0;
numberofStars = (calculateFunction (x)/10);
System.out.print(" ");
}
}
}
public static void main(String[] args)
{
int x = 0;
int numberofStars = 0;
calculateFunction(x);
printStars(numberofStars);
System.out.println("*");
}
}
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
I don't feel like reading through the code so something like this should work:
Java Syntax (Toggle Plain Text)
int x = 100; int numStarts = x/10; for (int i=0; i<numStars; ++i) { System.out.print("*"); }
•
•
Join Date: Mar 2006
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by RATED3X
I tried that but all it does is print out ten stars. It does not allow the user to input a number and print out the stars acording to the number that is inputted by the user.
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class GrowthofaFunction{ //Method to printStars public static void printStars(int numberofStars){ for (int i = 10; i <= numberofStars; i+=10){ System.out.print("*"); } } public static void main(String[] args){ System.out.print("Enter value for x: "); Scanner stdin = new Scanner(System.in); int x = stdin.nextInt(); printStars(x); } }
•
•
Join Date: Mar 2006
Posts: 5
Reputation:
Solved Threads: 0
I am getting the following errors when I try to compile the program:
----jGRASP exec: javac -g C:\Program Files\jGRASP\GrowthofaFunction1.java
GrowthofaFunction1.java:32: 'class' or 'interface' expected
public static void main(String[] args) {
^
GrowthofaFunction1.java:45: 'class' or 'interface' expected
}
^
GrowthofaFunction1.java:49: 'class' or 'interface' expected
^
3 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
import java.util.Scanner;
import java.io.*;
import java.math.*;
import java.util.*;
public class GrowthofaFunction1
{
//Method to calculate function
public static int calculateFunction(int x)
{
int result = 0;
System.out.print("Enter value for x: ");
Scanner stdin = new Scanner(System.in);
x = stdin.nextInt();
result=(x + (x*x) + (int)(Math.pow(2,x)));
return result;
}
//Method to printStars
public static void printStars(int numberofStars)
{
for (int i = 10; i <= numberofStars; i+=10)
System.out.print("*");
}
}
public static void main(String[] args) {
System.out.print("Enter value for x: ");
Scanner stdin = new Scanner(System.in);
int x = stdin.nextInt();
printStars(x);
}
}
}
----jGRASP exec: javac -g C:\Program Files\jGRASP\GrowthofaFunction1.java
GrowthofaFunction1.java:32: 'class' or 'interface' expected
public static void main(String[] args) {
^
GrowthofaFunction1.java:45: 'class' or 'interface' expected
}
^
GrowthofaFunction1.java:49: 'class' or 'interface' expected
^
3 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
import java.util.Scanner;
import java.io.*;
import java.math.*;
import java.util.*;
public class GrowthofaFunction1
{
//Method to calculate function
public static int calculateFunction(int x)
{
int result = 0;
System.out.print("Enter value for x: ");
Scanner stdin = new Scanner(System.in);
x = stdin.nextInt();
result=(x + (x*x) + (int)(Math.pow(2,x)));
return result;
}
//Method to printStars
public static void printStars(int numberofStars)
{
for (int i = 10; i <= numberofStars; i+=10)
System.out.print("*");
}
}
public static void main(String[] args) {
System.out.print("Enter value for x: ");
Scanner stdin = new Scanner(System.in);
int x = stdin.nextInt();
printStars(x);
}
}
}
![]() |
Other Threads in the Java Forum
- Previous Thread: Connecting modem via java
- Next Thread: Good Tuatorial...
Views: 2063 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component database derby design draw eclipse encryption error event exception file fractal game givemetehcodez google graphics gui helpwithhomework html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jmf jni jpanel julia linux list loop map method methods midlethttpconnection mobile multithreading netbeans newbie number object oracle print printing problem program programming project recursion reference remove ria scanner screen search server set size sms socket sort sourcelabs sql stop string swing test threads time tree ui unicode validation windows






