Results 1 to 6 of 6
Thread: Simple Java Homework Help
- 02-10-2013, 04:52 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Simple Java Homework Help (* NOW SOLVED*)
I am having trouble with this code, it does half of what I need it to do but not quite exactly.
Here is what it needs to do:
Write a method, named printHorizontalBars(), that prompts the user for four values and draws
corresponding bar graphs using an ASCII character. For example if the user entered 15, 12, 9
and 4, the program would draw.
* * * * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * *
Here is what I get with the current code output:
Enter four integers...
15 12 9 4
* * * *
* * * *
* * * *
* * * *
Can anyone help me figure out whats wrong?.gif)
Thanks
-Austin
Java Code:package patterns; import java.util.Scanner; /** * * @author Austin J Benner */ public class Patterns { public void HorizontalBars(){ int[] numberArr = new int [4]; // Creates and array to read inputs into Scanner sc = new Scanner (System.in); // Creates a new instance of the Scanner() method System.out.println("Enter four integers..."); int number_1 = sc.nextInt(); // Reads in the four integer inputs from user int number_2 = sc.nextInt(); int number_3 = sc.nextInt(); int number_4 = sc.nextInt(); numberArr[0] = number_1; numberArr[1] = number_2; numberArr[2] = number_3; numberArr[3] = number_4; System.out.println(); int max = 0; // Determines the largest integer value in the array for(int i = 0; i < numberArr.length;i++) { if (numberArr[i] > max) { max = numberArr[i]; } for(int j = 0; j < numberArr.length; j++) { if (numberArr[i] >= j) { System.out.print("* "); // Prints ASCII symbol if largest integer value } } System.out.println(); } } public static void main(String[ ] args) { // TODO code application logic here Patterns myTestDriver = new Patterns(); System.out.println("printHorizontalBars"); myTestDriver.HorizontalBars(); } }Last edited by ajb5876; 02-10-2013 at 07:00 PM.
- 02-10-2013, 06:12 PM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Simple Java Homework Help
I wont give you the solution to all of your problems since it's a homework, but can you spot anything wrong with the following part:
Will your program ever print out more * than the length of your array?Java Code:for(int j = 0; j < numberArr.length; j++) { if (numberArr[i] >= j) { System.out.print("* "); // Prints ASCII symbol if largest integer value } }
- 02-10-2013, 06:39 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Simple Java Homework Help
I'm not 100% sure what you are asking (VERY NEW TO JAVA) because it did display more than one *'s with that code segment...I just messed with the code a little bit, starting to go in the right direction, though it only correctly displays number <= 4 properly. Anything over that it just displays as 4.Java Code:for(int j = 1; j <= numberArr.length; j++) { if (numberArr[i] >= j) { System.out.print("* "); // Prints ASCII symbol if largest integer value } }
-Austin
- 02-10-2013, 06:41 PM #4
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Simple Java Homework Help
Never mind I think I understand Now.
Is this what you were alluding to?Java Code:for(int j = 1; j <= max; j++)
-Austin
- 02-10-2013, 06:43 PM #5
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Simple Java Homework Help
Et voila!
You solved it!
- 02-10-2013, 06:47 PM #6
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
New to Java and connot figure out simple java program- please help
By tm02943 in forum New To JavaReplies: 11Last Post: 03-22-2011, 06:33 AM -
Simple POS System in Java
By amsroxy1125 in forum New To JavaReplies: 5Last Post: 12-08-2009, 10:23 PM -
Simple Java program
By Rolle in forum New To JavaReplies: 3Last Post: 10-26-2009, 04:05 PM -
Simple java
By abhiN in forum New To JavaReplies: 0Last Post: 01-16-2008, 02:44 PM -
Simple java
By abhiN in forum New To JavaReplies: 1Last Post: 01-16-2008, 10:13 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks