Welcome Guest [Log In] [Register]
Welcome to the Heavenly Host Elementary School: A Corpse Party Forum. We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Java Help
Topic Started: December 12, 2015, 2:58 am (440 Views)
logonin
Member Avatar
Corpse Party Fanboy
I know this not a programming forum but there seems to be some very smart programmers here on the forums so I guess its worth a shot to ask this. In java I need to make a variation of the famous "FizzBuzz" program where I have to count up to any number the user inputs and then only print 11 numbers per line. My code can do all of that but for some reason it repeats the same number 11 times each line. Can someone please help me fix it? Here is my code

Code:
 
import javax.swing.JOptionPane;
public class CozaLoza2
{
public static void main(String[] args)
{

String n = JOptionPane.showInputDialog("Please input an interger");
int result = Integer.parseInt(n);
for (int i=0; i<=result; i++){
for (int j=0; j<=10; j++){

System.out.print(" ");

if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) {
System.out.print("CozaLozaWoza");}

if (i % 3 == 0 && i % 5 == 0) {
System.out.print("CozaLoza");

} else if (i % 5 == 0) {
System.out.print("Loza");

} else if (i % 3 == 0) {
System.out.print("Coza");

} else if (i % 7 == 0) {
System.out.print("Woza");

} else {
System.out.print(" "+i);
}
}
}
System.out.println();
}
}


and this is the output I get if I input "50"

"CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 4 4 4 4 4 4 4 4 4 4 4 Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza 8 8 8 8 8 8 8 8 8 8 8 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza 11 11 11 11 11 11 11 11 11 11 11 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 13 13 13 13 13 13 13 13 13 13 13 Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza 16 16 16 16 16 16 16 16 16 16 16 17 17 17 17 17 17 17 17 17 17 17 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 19 19 19 19 19 19 19 19 19 19 19 Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 22 22 22 22 22 22 22 22 22 22 22 23 23 23 23 23 23 23 23 23 23 23 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza 26 26 26 26 26 26 26 26 26 26 26 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza 29 29 29 29 29 29 29 29 29 29 29 CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza 31 31 31 31 31 31 31 31 31 31 31 32 32 32 32 32 32 32 32 32 32 32 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 34 34 34 34 34 34 34 34 34 34 34 Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 37 37 37 37 37 37 37 37 37 37 37 38 38 38 38 38 38 38 38 38 38 38 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza 41 41 41 41 41 41 41 41 41 41 41 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 43 43 43 43 43 43 43 43 43 43 43 44 44 44 44 44 44 44 44 44 44 44 CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza 46 46 46 46 46 46 46 46 46 46 46 47 47 47 47 47 47 47 47 47 47 47 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza"

Any help would be appreciated. Thanks :)
Offline Profile Quote Post Goto Top
 
Rohzek
Member Avatar
Leader
I'm not exactly familiar with this particular idea so I don't know what I'm looking for exactly but... If the output itself looks fine, EXCEPT that it's repeated 11 times... What is the second for loop supposed to do? It's looping through 11 times... could be your culprit?

Posted Image
Offline Profile Quote Post Goto Top
 
Luph
Member Avatar
╰ᕦ╯( O++O )╰ᕤ╯
It's the inner loop. What you're doing is printing each number from 0 to number that user input 11 times.

If you want to have 11 numbers on each line, then what you can do is to have a counter variable that keeps track how many numbers you have printed in a single line and if you have printed 11 numbers, you print a newline character and reset the counter.
Edited by Luph, December 13, 2015, 12:37 am.
Ain't no party like Corpse Party

Satoshi x Yoshiki is canon
Offline Profile Quote Post Goto Top
 
logonin
Member Avatar
Corpse Party Fanboy
Hi I am a beginner to Java and I really need some help. I know this is not a programming forum but I have gotten some good advice from here in the past. I have to add to some code that has been provided to do the following "Write a method isEventhat determines whether an integer is even or odd. The method should take an integer argument and return true if the integer is even. If the integer is odd, it should return false."

Here is the code I was provided

Code:
 
public class Lesson18Test
{
public static void main( String[] args )
{
MethodPractice mp = new MethodPractice();

boolean isEvenTest = false;
boolean multipleTest = false;
double minimum3Test = 0.0;
int gcdTest = 0;

//Problem 1 Test
isEvenTest = mp.isEven(6);
System.out.println( "isEven(6) output: " + isEvenTest );


And here is my attempt at doing what the project asks
Code:
 
public class NewTest2aq
{
public static void main( String[] args )
{
MethodPractice mp = new MethodPractice();
{
boolean isEvenTest = false;
boolean multipleTest = false;
double minimum3Test = 0.0;
int gcdTest = 0;
}
}
//Problem 1 Test
public int isEvenTest;
int Test = 6;
int setisEvenTest ( int Test ){

int Hamburgers = Test;

return Hamburgers;

isEvenTest = mp.isEven(6);
System.out.println( "isEven(6) output: " + isEvenTest );
}
}


I am getting the following errors from this code
" [line: 5]
Error: cannot find symbol
symbol: class MethodPractice
location: class NewTest3

[line: 5]
Error: cannot find symbol
symbol: class MethodPractice
location: class NewTest3

[line: 22]
Error: cannot find symbol
symbol: variable mp
location: class NewTest3"

So how can I fix these errors? I am confused with the code my teacher has provided. I am also pretty sure we are not aloud to change it and I am positive we are not aloud to simply delete it. So yeah any advice will be greatly appreciated. Thanks :)

Edited by logonin, January 7, 2016, 1:30 am.
Offline Profile Quote Post Goto Top
 
Skybird
Member Avatar
Grindblues

I've combined these two topics as they are of similar subject.
[ - Gran is the real deal. - ]
Offline Profile Quote Post Goto Top
 
Rohzek
Member Avatar
Leader
Line 5 instantiates a class named "MethodPractice" under the variable name mp...

It seems you don't have a class called MethodPractice and you're trying to instantiate one.

Since that was all the code you were provided, I assume you were supposed to create it?

I was also going to say "Isn't this the same topic as the last one?" haha.

Posted Image
Offline Profile Quote Post Goto Top
 
logonin
Member Avatar
Corpse Party Fanboy
Shinohara_Seiko
January 7, 2016, 2:13 am
Line 5 instantiates a class named "MethodPractice" under the variable name mp...

It seems you don't have a class called MethodPractice and you're trying to instantiate one.

Since that was all the code you were provided, I assume you were supposed to create it?

I was also going to say "Isn't this the same topic as the last one?" haha.
Actually I do... I just found this file along with the other one my teacher provided us

Code:
 
public class MethodPractice
{
public MethodPractice()
{
}

//Insert the Problem #1 method here

//Insert the Problem #2 method here

//Insert the Problem #3 method here

//Insert the Problem #4 method here

}


So do I just have to copy and paste everything I did in there under the problem 1 section in the code?
Edited by logonin, January 7, 2016, 2:45 am.
Offline Profile Quote Post Goto Top
 
Rohzek
Member Avatar
Leader
Not to sound rude friend... but without proper context (and due to lack of sleep) I'm not entirely sure what's going on in your version of the class (NewTest2aq).. I don't mean anything by it, Sorry. ●︿●


Don't know how much help you want and I feel bad for helping you with I assume homework so I'll be as brief as possible but...

Line 13 of the original (Lesson18Test) class is calling for a method in MethodPractice called isEven which should return a boolean.

Code: Problem 1 text
 
//Problem 1 Test
isEvenTest = mp.isEven(6);
System.out.println( "isEven(6) output: " + isEvenTest );


So under Problem#1 you'll be needing to create that.
Code: Empty method
 
public boolean isEven(int num){}


And then inside of that you'll need to check the number to see if it's even or odd. Two quick ways I know of doing that is modulus division by 2 (if the answer is 0 it's even) or checking the bit to see if it's even or odd. Both can be done in one line of code so neither is easier or harder (but modulus division can be a little slower if you have to do a lot of it.. won't make a difference here)

Posted Image
Offline Profile Quote Post Goto Top
 
logonin
Member Avatar
Corpse Party Fanboy
Shinohara_Seiko
January 7, 2016, 3:17 am
Not to sound rude friend... but without proper context (and due to lack of sleep) I'm not entirely sure what's going on in your version of the class (NewTest2aq).. I don't mean anything by it, Sorry. ●︿●


Don't know how much help you want and I feel bad for helping you with I assume homework so I'll be as brief as possible but...

Line 13 of the original (Lesson18Test) class is calling for a method in MethodPractice called isEven which should return a boolean.

Code: Problem 1 text
 
//Problem 1 Test
isEvenTest = mp.isEven(6);
System.out.println( "isEven(6) output: " + isEvenTest );


So under Problem#1 you'll be needing to create that.
Code: Empty method
 
public boolean isEven(int num){}


And then inside of that you'll need to check the number to see if it's even or odd. Two quick ways I know of doing that is modulus division by 2 (if the answer is 0 it's even) or checking the bit to see if it's even or odd. Both can be done in one line of code so neither is easier or harder (but modulus division can be a little slower if you have to do a lot of it.. won't make a difference here)
Thank you so much! You are a life saver!
Offline Profile Quote Post Goto Top
 
Rohzek
Member Avatar
Leader
It's nothing, always happy to help! o(^▽^)o

Posted Image
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
Go to Next Page
« Previous Topic · Science Lab · Next Topic »
Add Reply