| View previous topic :: View next topic |
| Author |
Message |
nothing4me Newbie cheater
Reputation: 0
Joined: 27 Dec 2006 Posts: 10
|
Posted: Wed Oct 29, 2008 4:47 pm Post subject: Simple Java program? (Loops)? |
|
|
I need to create this using loops:
(The underlines are spaces)
____*
___***
__*****
_*******
*********
*********
_*******
__*****
___***
____*
Can anyone do it and post the source code?
Here is a chart in case you don’t feel like counting:
Row Spaces Stars
1 4 1
2 3 3
3 2 5
4 1 7
5 0 9
6 0 9
7 1 7
8 2 5
9 3 3
10 4 1
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Wed Oct 29, 2008 5:20 pm Post subject: |
|
|
Not your personal homework-monkeys. Try learning something.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Wed Oct 29, 2008 7:00 pm Post subject: |
|
|
why not do it in batch o.o
:start
echo ____*
echo ___***
echo __*****
echo _*******
echo *********
echo *********
echo _*******
echo __*****
echo ___***
echo ____*
goto :start
win o.o
_________________
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Thu Oct 30, 2008 5:05 pm Post subject: |
|
|
| yoyonerd wrote: | why not do it in batch o.o
win o.o |
Your program prints it repeatedly, thats not what he wants. He wants to print it once using loops, duh.
for(int i; i < 1; i++) {
System.out.println("____*");
System.out.println("echo ___***");
System.out.println("echo __*****");
System.out.println("echo _*******");
System.out.println("echo *********");
System.out.println("echo *********");
System.out.println("echo _*******");
System.out.println("echo __*****");
System.out.println("echo ___***");
System.out.println("echo ____* ");
}
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Thu Oct 30, 2008 6:13 pm Post subject: |
|
|
Single look at your chart for patterns the answers of stars are always odd.. and the counter could start at X.. so just start it at 4 now you understand the pattern
you will be going to use 2 for loops to make it look good.. with one counting down and other counting up
or you could use one while loop with a boolean and add a if statements if(counter == 0) then
boolean = true;
if(boolean) then
counter++
counterVal-= 2;
else
counter--;
counterVal+=2;
I'm 100% sure the teacher wants you to use a algorithm to solve this not just echo it out.. or u will get a low joe..
Anyways if you can't do this in a algorithm then u shouldnt be programming
_________________
|
|
| Back to top |
|
 |
nothing4me Newbie cheater
Reputation: 0
Joined: 27 Dec 2006 Posts: 10
|
Posted: Thu Oct 30, 2008 6:20 pm Post subject: |
|
|
| I already have it solved, thanks anyway.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Oct 30, 2008 6:54 pm Post subject: |
|
|
| Code: | #include <iostream>
const int width_max = 10;
int width = 10;
int count = 0;
int main(){
do{
int difference = (width / 2) - 1;
for(int i = 0; i < difference; i++){
std::cout << " ";
}width--;
for(int i = 0; i < (width_max - width); i++){
std::cout << "*";
}std::cout << "\n";
}while(width > 0);
return 0;
} |
I then realized I had not taken fractions into account, and that I was doing it a stupid way to begin with, got bored, and stopped. Don't feel like actually thinking for a few minutes now.
edit: Figured it out woop.
| Code: | #include <iostream>
int main()
{
int size, count = 0;
std::cin >> size;
for (int i = 0; size > i; count++, size--){
for (int i = 0; size > i; i++){
std::cout << " ";
}
for (int i = 0; (2 * count) >= i; i++){
std::cout << "*";
}std::cout << "\n";
}
return 0;
} |
Last edited by hcavolsdsadgadsg on Thu Oct 30, 2008 10:10 pm; edited 2 times in total |
|
| Back to top |
|
 |
nothing4me Newbie cheater
Reputation: 0
Joined: 27 Dec 2006 Posts: 10
|
Posted: Thu Oct 30, 2008 7:27 pm Post subject: |
|
|
| slovach wrote: | | Code: | #include <iostream>
const int width_max = 10;
int width = 10;
int count = 0;
int main(){
do{
int difference = (width / 2) - 1;
for(int i = 0; i < difference; i++){
std::cout << " ";
}width--;
for(int i = 0; i < (width_max - width); i++){
std::cout << "*";
}std::cout << "\n";
}while(width > 0);
return 0;
} |
I then realized I had not taken fractions into account, and that I was doing it a stupid way to begin with, got bored, and stopped. Don't feel like actually thinking for a few minutes now.  | That's not Java, but thanks for trying anyway.
|
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Fri Oct 31, 2008 3:32 am Post subject: |
|
|
Now this is spoon feeding. He's given you the code, now just convert it.
Surely you can tell where the loops are in that code etc.
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
nothing4me Newbie cheater
Reputation: 0
Joined: 27 Dec 2006 Posts: 10
|
Posted: Fri Oct 31, 2008 4:44 am Post subject: |
|
|
| --Pillboi-- wrote: | Now this is spoon feeding. He's given you the code, now just convert it.
Surely you can tell where the loops are in that code etc. | I don't need it...
Why don't people read posts? xD I already did it myself!
|
|
| Back to top |
|
 |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Fri Oct 31, 2008 7:11 am Post subject: |
|
|
edit: Figured it out woop.
| Code: | #include <iostream>
int main()
{
int size, count = 0;
std::cin >> size;
for (int i = 0; size > i; count++, size--){
for (int i = 0; size > i; i++){
std::cout << " ";
}
for (int i = 0; (2 * count) >= i; i++){
std::cout << "*";
}std::cout << "\n";
}
return 0;
} | [/quote]
I think you are missing the last half (this broke my head..., but with the help of your code, i figured it out at last...))
| Code: | #include <iostream>
int main()
{
int size, count = 0;
std::cin >> size;
for (int i = 0; size > i; count++, size--){
for (int i = 0; size > i; i++){
std::cout << " ";
}
for (int i = 0; (2 * count) >= i; i++){
std::cout << "*";
}std::cout << "\n";
}
count--;
for (int i = count; size < i; size++, count--){
for (int i = 0; size+2 > i; i++){
std::cout << " ";
}
for (int i = 0; (count * 2 -1) > i; i++){
std::cout << "*";
}std::cout << "\n";
}
std::cin.sync();
std::cin.ignore();
return 0;
} |
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sat Nov 01, 2008 6:33 am Post subject: |
|
|
Here's my old code, maybe someone of you recognizes it :D
| Code: | import java.util.Random;
import java.io.*;
class printtt {
public static void main(String[] args) {
/* Let's do one randomly */
Random generator = new Random();
int rand = generator.nextInt(50);
rand += 50;
/* And one by user input */
System.out.print("Width: "); /* Get the width */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String wid[] = {null, "\u0049", "\u0044\u0049\u0044\u004E\u0027\u0054", "\u004D\u0041\u004B\u0045", "\u0054\u0048\u0049\u0053", "\u004D\u0059\u0053\u0045\u004C\u0046"};
try {
wid[0] = br.readLine();
} catch (IOException ioe) {
System.out.println("Error");
}
/* DO THE PRINTING */
for( int i = 0; i<rand; ++i) { int j = generator.nextInt(5) + 1; System.out.println(wid[j]); }
}
} |
/me waits some random stupid kid (hackersrule or something) who knows nothing to derep me with a reason "he did someone's homework!" :)
|
|
| Back to top |
|
 |
nothing4me Newbie cheater
Reputation: 0
Joined: 27 Dec 2006 Posts: 10
|
Posted: Sat Nov 01, 2008 10:35 am Post subject: |
|
|
| Jani wrote: | Here's my old code, maybe someone of you recognizes it
| Code: | import java.util.Random;
import java.io.*;
class printtt {
public static void main(String[] args) {
/* Let's do one randomly */
Random generator = new Random();
int rand = generator.nextInt(50);
rand += 50;
/* And one by user input */
System.out.print("Width: "); /* Get the width */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String wid[] = {null, "\u0049", "\u0044\u0049\u0044\u004E\u0027\u0054", "\u004D\u0041\u004B\u0045", "\u0054\u0048\u0049\u0053", "\u004D\u0059\u0053\u0045\u004C\u0046"};
try {
wid[0] = br.readLine();
} catch (IOException ioe) {
System.out.println("Error");
}
/* DO THE PRINTING */
for( int i = 0; i<rand; ++i) { int j = generator.nextInt(5) + 1; System.out.println(wid[j]); }
}
} |
/me waits some random stupid kid (hackersrule or something) who knows nothing to derep me with a reason "he did someone's homework!"  | BUT I ALREADY DID IT! T_T_T_T_T_T_T
Why doesn't anyone read my posts???
|
|
| Back to top |
|
 |
|