need help in java

Jan 7, 2007
29
0
0
heres what i have so far
class prog3 {
public static void main(String[] args){
int count = 1;
while (count < 21) {
System.out.print("########");
System.out.print(" ");

count++;
}
}
}




which gives out this
######## ######## ######## ######## ########
######## ######## ######## ######## ########
######## ######## ######## ######## ########
######## ######## ######## ######## ########


now i dont know how to proceed since requirements are to only use those print statements once
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Use an inner and outer loop (rows and columns).
Find a relation between the rows and columns, and when the hash marks should be printed.
If you need to print more than one consecutive space character, loop that call as well the required number of times.
 
Sep 29, 2004
18,656
67
91
Sometimes the easiest way to figure things out is to write the code to work (ignoring your teachers rules) and then figuring out how to write a loop. That is, use those three lines as often as you would want and then optimize your code.

 

Onund

Senior member
Jul 19, 2007
287
0
0
so wait, you have to recreate that checkerboard pattern but the three print statements can only appear in your program once? mundane is right, you have to put your print("###...") and print(" ") in different loops...

or if you want to piss off whoever marks this, and you are allowed to use function calls you can do something horribly mean like this:

public void f(){
System.out.print(" ")
}
public void u(){
System.out.print("########");
}
public void man(){
System.out.print("\n");
}

public static void main(String[] args){
f();
f();
f();
f();
f();
f();
f();
f();
u();
f();
f();
...
}

but really, stick to fixing up your loops.
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,161
510
126
Originally posted by: Onund
so wait, you have to recreate that checkerboard pattern but the three print statements can only appear in your program once? mundane is right, you have to put your print("###...") and print(" ") in different loops...

or if you want to piss off whoever marks this, and you are allowed to use function calls you can do something horribly mean like this:

public void f(){
System.out.print(" ")
}
public void u(){
System.out.print("########");
}
public void man(){
System.out.print("\n");
}

public static void main(String[] args){
f();
f();
f();
f();
f();
f();
f();
f();
u();
f();
f();
...
}

but really, stick to fixing up your loops.

lol!!! I liked that one.

I would say it is all about making a few function calls so that you only have the "System.out.print" things in there once, just like the above example did. Your instructor's rules said you can only do it once in the program, and this is the way to do it. Then you might want to create some extra functions like

public void w() {
for(int i=0; i<6;i++ {
f();
}
}

public void print_white_black_line() {
w();
u();
w();
u();
w();
u();
w();
u();
f();
new_line_character();
}

public void print_black_white_line() {
u();
w();
u();
w();
u();
w();
u();
w();
new_line_character();
}

public void print white_black_checker() {
print_white_black_line();
print_white_black_line();
print_white_black_line();
print_white_black_line();
}

public void print black_white_checker() {
print_black_white_line();
print_black_white_line();
print_black_white_line();
print_black_white_line();
}

etc...etc... etc...

Basically you can call the functions whatever you want. Making it logical will probably score more points for style. And you can keep building on the functions until you have a final single function and just have "main" call that one function.
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Underscores are for indentation. Hopefully there are no typos, but the logic should be good.

for (int i = 0; i < 32; i++) {
__for (int j = 0; j < 8; j++) {
____if ((i/4 + j)%2 == 0) {
_______for (int k = 0; i < 8; k++) {
_________System.out.print(" ");
_______}
____} else {
_______System.out.print("########");
____}
__}
__System.out.print("\n");
}
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: esun
Underscores are for indentation. Hopefully there are no typos, but the logic should be good.

for (int i = 0; i < 32; i++) {
__for (int j = 0; j < 8; j++) {
____if ((i/4 + j)%2 == 0) {
_______for (int k = 0; i < 8; k++) {
_________System.out.print(" ");
_______}
____} else {
_______System.out.print("########");
____}
__}
__System.out.print("\n");
}

Great, just give him the answer... :roll:
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Originally posted by: tfinch2
Great, just give him the answer... :roll:

People can learn by seeing solutions. And if he chooses to copy without understanding, then he'll fail the next assignment anyway (and if not that, the exams). Either way, it's not a bad way to help someone (especially given that the previous replies are giving him horribly inefficient ways to do the same thing, probably leading him down the wrong track).
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |