I really need help. I did the code, but I know it is all wrong. I am not sure how to fix it. This program has boggled me for a week.
I am watching the growth of the function. First I have to calculate a function after entering the value of x from a prompt. Then I have to use printstars to print the relative size for values from 1 to 10.
I am a bad programmer. I already know this.
Does anyone see anything wrong with my code?

:cry:

import java.io.*;
import java.math.*;


public class GrowthofaFunction {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		double x;
		int y;
		
		System.out.println("Enter value for x: ");
		public int calculateFunction(int x);
		{
			y = x + (x*x) + Math.pow(2,x);
			return y;
		}
		
		//printStars has input x
		   for (int y = 0; x > y; x++)
		   {
			   x = y/10;
		   }
		      System.out.print("*");

	}
    return;
}

Dani AI

Generated

Short version of the fix and why the current attempts fail.

The program should do three simple things in order: read an integer x, compute y = x + x*x + 2^x, then print floor(y/10) stars. Key problems in the posted attempts are: declaring a method inside main (Java does not allow access modifiers or nested method declarations there), using wrong loop conditions (e.g. i > 0 or overwriting loop variables), and mixing returns/types incorrectly. is right about the loop mechanics — compute the number of stars once, then loop that many times. , move method code out of main and keep I/O, computation and printing separate.

Algorithm (step-by-step)

  • Read an int x from the console.
  • Compute result = x + x*x + 2^x (use Math.pow and cast, or a shift for small non‑negative x).
  • Compute stars = Math.max(0, result / 10).
  • Loop i from 0 to stars-1 and print '*' each iteration, then print a newline.

Example (minimal, corrected structure)

import java.util.Scanner;

public class GrowthOfFunction {
    static int compute(int n) {
        return n + n*n + (int)Math.pow(2, n);
    }

    static void printStars(int count) {
        for (int i = 0; i < count; i++) System.out.print('*');
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter value for x: ");
        int x = in.nextInt();
        int result = compute(x);
        printStars(Math.max(0, result / 10));
    }
}

Notes and cautions: Math.pow returns a double — cast carefully. For larger x (roughly x ≥ 31) 2^x will overflow an int; use long or BigInteger if you expect big inputs. Keep methods small and single‑purpose (I/O, compute, print) to avoid the scope/return mistakes seen earlier.

Recommended Answers

All 6 Replies

I don't see any code taking input. Look into the bufferedreader.

I don't see any code taking input. Look into the bufferedreader.

Yeah that would be a solution. I knew something important was missing like that. :o

I will post the new code and hope that someone else could tell me what i did wrong. I don't think I did like horribly bad. But I know those printStars are wrongggggg. I never got increments and decrements even when I took C++.

Sad huh? :eek:

import java.io.*;
import java.math.*;
import java.util.*;


public class GrowthofaFunction 
{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		public void calculateFunction(int x)
		{
			final int y1;
			final int y2;
			final int y;
			
			Scanner stdin = new Scanner(System.in);
			
			System.out.print("Enter value for x: ");
			x = stdin.nextInt();
			
			y1 = x + (x*x);
			y2 =(int) (Math.pow(2,x));
			y = y1 + y2;
			return;
			
			
             //	printStars has input x
			
			for (int i = 0; i > 0; i++)
			   {
				   x = y/10;
			   }
			      System.out.print("*");
			      return;
			 
			      
		}  
		
}

This is the updated code. I just accomplished. I hope this is right. Anyone see anything worth checking? I finally figured it out. Thank God.

Yeah, you've got a problem with the loop. Fix that, and then I'll take a look at the rest.

for (int i = 0; i < 10; i++)
			   {
				   x = y/10;
			           System.out.print("*");
			   }

You want to increment while a condition is true. For instance, in this case we start with:
i = 0;
which is our couting variable.

The second thing is the condition. The condition here is:
i<10;

That means we will keep looping as long as 'i' is less than 10.

The third thing in the loop, is the increment statement. In this example:

i++

means that for each round in the loop, 'i' will be incremented by one. The first time you program runs the looping looks like this;


i=0
i IS less than 10
i is incremented by one(now it is 1)
a start is printed

Second time:
i=1 //from the first increment
i is still less than 10
i is incremented //now equals 2
Another star is printed.


It goes on and on until the looping condition is not true(i is greater than or equal to 10).

Opps, you might want to take the x = y/10 out of the loop. There's no reason to do that 10 times!

Opps, you might want to take the x = y/10 out of the loop. There's no reason to do that 10 times!

See apparently I am suppose to take the answer of the function and then print the stars.........say for instance the answer is 62 then divide it by 10 and i will get 6 stars because you know it is truncated.

So now I am guess, I am doing that wrong.

for (int i = 0; i <= 0; i++)
			   {
				   y = stdin.nextInt();
				   i = y/10;
			   }
			      System.out.print("*");
			      return;

Ok I changed it up a little bit. I am trying here. There is nothing better than that right. :o

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.