You are viewing our Forum Archives. To view or take place in current topics click here.
Some simple java help +rep
Posted:
Some simple java help +repPosted:
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
For an assignment I have to make a write the code so that it asks the user to type y for a random pyramid of X's to be made until the user types something that isn't a y
X
X X
X X X
X X X X
So far I have this but it doesn't work and I cant figure it out
public class DrawTriangle
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Do you want to see a pyramid? if so type y, if not type anything else");
String yes = scan.next();
boolean check = false;
final int b = 20;
final int a = 1;
int height = (int) (Math.random() * (b - a + 1) + a);
if (yes.equals("y") || yes.equals("Y"))
{
check = true;
}
else
{
check = false;
}
while (check == true)
{
System.out.println("Do you want to see a pyramid? if so type y, if not type anything else");
scan.next();
for(int i = 0; i < height; i++)
{
for(int j = 0; j < height - i;j++)
{
System.out.print(" ");
}
for(int k=0; k <= i ;k++)
{
System.out.print("X ");
}
System.out.println();
}
}
}
}
can someone give me some help please
X
X X
X X X
X X X X
So far I have this but it doesn't work and I cant figure it out
public class DrawTriangle
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Do you want to see a pyramid? if so type y, if not type anything else");
String yes = scan.next();
boolean check = false;
final int b = 20;
final int a = 1;
int height = (int) (Math.random() * (b - a + 1) + a);
if (yes.equals("y") || yes.equals("Y"))
{
check = true;
}
else
{
check = false;
}
while (check == true)
{
System.out.println("Do you want to see a pyramid? if so type y, if not type anything else");
scan.next();
for(int i = 0; i < height; i++)
{
for(int j = 0; j < height - i;j++)
{
System.out.print(" ");
}
for(int k=0; k <= i ;k++)
{
System.out.print("X ");
}
System.out.println();
}
}
}
}
can someone give me some help please
#2. Posted:
Status: Offline
Joined: Nov 16, 20149Year Member
Posts: 544
Reputation Power: 25
#3. Posted:
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Without even executing the code I can see the problem.
This will only run once, it checks the first scan.next, and placed the input into variable "yes" then inside the while loop it doesn't update this variable.
the solution is, you need to move this code
inside of your while loop, after the scan.next() line.
and the command scan.next(); inside the loop change to
yes = scan.next();
this will update your "yes" variable on each loop iteration.
BUT
This will still run the pyramid, one more time, even if you answer anything other than 'y'
To correct your code, move the question below your for loop.
Full answer here
I have also set the random inside the loop to give you different size pyramid on each iteration ;)
Please don't just copy and paste the answer, look through the code and understand what i have done
PS I have not actually tested this myself, but pretty certain this should do what you need.
This will only run once, it checks the first scan.next, and placed the input into variable "yes" then inside the while loop it doesn't update this variable.
the solution is, you need to move this code
if (yes.equals("y") || yes.equals("Y"))
{
check = true;
}
else
{
check = false;
}
inside of your while loop, after the scan.next() line.
and the command scan.next(); inside the loop change to
yes = scan.next();
this will update your "yes" variable on each loop iteration.
BUT
This will still run the pyramid, one more time, even if you answer anything other than 'y'
To correct your code, move the question below your for loop.
Full answer here
public class DrawTriangle {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Do you want to see a pyramid? if so type y, if not type anything else");
String yes = scan.next();
boolean check = false;
final int b = 20;
final int a = 1;
if (yes.equals("y") || yes.equals("Y")) {
check = true;
} else {
check = false;
}
while (check == true) {
int height = (int) (Math.random() * (b - a + 1) + a);
for(int i = 0; i < height; i++) {
for(int j = 0; j < height - i;j++) {
System.out.print(" ");
}
for(int k=0; k <= i ;k++) {
System.out.print("X ");
}
System.out.println();
}
System.out.println("Do you want to see a pyramid? if so type y, if not type anything else");
yes = scan.next();
if (yes.equals("y") || yes.equals("Y")) {
check = true;
} else {
check = false;
}
}
}
}
I have also set the random inside the loop to give you different size pyramid on each iteration ;)
Please don't just copy and paste the answer, look through the code and understand what i have done
PS I have not actually tested this myself, but pretty certain this should do what you need.
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.