Making Custom Checkerboard With Nested Loop
Feb 7, 2014
I'm trying to make a custom checkboard with given user input, in the form of (# of rows, # of columns, size of each square, filler character).
So the input 2 3 5 * would look like
*****.........*****
*****.........*****
*****.........*****
*****.........*****
*****.........*****
.........*****
.........*****
.........*****
.........*****
.........*****
I've made my loop, but I am unable to get more then a 1 1 1 checkerboard properly. I am stuck on how to divide the filler characters to make the proper square size. As of now they are all one lined.
import java.util.*;
public class Checker{
public static void main(String[] args) {
int col, row, size; char filler;
System.out.println("Please enter 3 numbers and a character."); //output
[Code] ..........
View Replies
ADVERTISEMENT
Feb 12, 2015
Im trying to create a checkerboard pattern with 2 nested for loops . I need to output asterisk characters. Im supposed o use an n int so I dont know if im limited to that 1 int. Im only getting 1 line of asterisk.
* * * * *
* * * * *
* * * * *
* * * * *
package checkerboard;
public class Checkerboard {
public static void main(String[] args) {
for (int row = 1; row < 6; row++){
System.out.print("* ");
for (int col = 6; col < col; col++) {
[code]...
View Replies
View Related
Mar 23, 2015
I have to make a pattern that is of a diamond shape such as this:
Java Code:
1
1 2 3
1 2 3 4 5
1 2 3
1
mh_sh_highlight_all('java');
I wrote this (for the variable name, this is just a small part of my program and I'm trying to get the pattern right so I didn't mind my variables at the moment):
Java Code:
public class NewFile{
public static void main(String []args){
int k = 0;
for (int i=1 ; i<=5 ; i++) {
{ for (int h=2 ; h >= i ; h--)
[Code] ....
My output is the following:
Java Code:
1
123
12345
1234567
123456789
mh_sh_highlight_all('java');
I realize I should divide the code into a lower and upper triangle using two loops. However, I don't know how to break the first part. I did find the "trend" but I don't see how to implement it.
View Replies
View Related
Jan 24, 2014
Now my issue is I have a JTable. The first column will contain Date objects all the way to the bottom. I want the JTable to display only the month and year. I created a custom cell renderer, but it appears the cell renderer method "getTableCellRendererComponent" is never called. (I tested it using System.out.println();) Here is my code for setting the cell renderer and the contents of the first column:
MonthYearTableCellRenderer renderer = new MonthYearTableCellRenderer();
tblView.setDefaultRenderer(Date.class, renderer);
for(int i = 0; i < 60; i++){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, i);
tableModel.setValueAt(cal.getTime(), i, 0);
}
The Date objects are set into the cells of column 0 correctly. The table shows the result of the "toString()" method for each cell in that column.
View Replies
View Related
Apr 5, 2014
just trying to learn it on my spare time and I'm doing do-while loops
public class help {
public static void main (String args[])
throws java.io.IOException {
char choice, ignore;
do{
System.out.println ("Choose one:");
System.out.println("1. if");
System.out.println("2.switch");
[code]....
It makes no difference in the program wither i delete this block or keep it..how while (choice < '1'| choice >'2'); translates? I would assume it would be while (choice >= '1'| choice =<'2');?
View Replies
View Related
Dec 17, 2013
Question: you are only allowed to use numbers from 1-6. Write a program to find all the permutations when three numbers are multiplied together to give a result 8. one number cannot occur twice in any permutation.
public class number4
{ public static void main(String[] args)
{
for(int a=1; a<=4; a++)
{
for(int b=1; b<=4; b++)
{
[Code]...
my program also prints out 2 2 2. but i'm not allowed to do that. how can I stop it from printing 2 2 2 ?
View Replies
View Related
Mar 6, 2015
Here's what "Why doesn't this work?" question. It concerns a small method which is part of a card game.
I'm trying to check a condition from a section of an array, without a predetermined number of times any given call to this method will require a check. How much of the array I'm checking could vary greatly.
Is it at all possible to nest a for loop yielding a variable number of boolean && conditions inside an if? Am I just missing the right bracketing or is this nesting (if that's the word) even possible in the language?
To clarify, below is broken code. Compiler isn't letting me accomplish this goal as I envision it.
public boolean isFlanking() {
boolean f;
int reach = Math.abs(selectorX - targetX);
if(rival.getDeck()[selectorX].getPile().isEmpty() == true &&
[Code] ....
View Replies
View Related
May 21, 2014
//Main method
public static void main(String args[])throws IOException{
boolean runProgram = true;
Scanner keyboard = new Scanner(System.in);
//runs program while runProgram is true
while (runProgram){
[code].....
View Replies
View Related
Oct 12, 2014
int count = 0;
for (int x = 0; x < 3; x++)
for (int y = x; y < 3; y++)
for (int z = y; z < 3; z++)count++;
What is the value of count after the code segment is executed?
The answer is 9, but I don't really understand how this answer was derived.
View Replies
View Related
Nov 5, 2014
for (int i = 0; i < 4; i++)
{
for (int k = 0; k < i; k++)
{
System.out.print(" ");
}
for (int j = 4; j > i; j--)
{
System.out.print("$");
}
System.out.println();
}
I was told that the answer when this code segment is printed look's like this:
$$$$
$$$
$$
$
Here's what I did:Looking at the outer for loop, (i) 0 < 4 so I went into the first inner nested loop. (k) 0 is not less than (i) 0 so I went to the 2nd inner nested loop and found that it worked, and I was able to repeat this loop 3 more times and then I exited the loop and printed the line out (giving me the first line of four $). I then went back to the outer for loop, increased i by 1 and (i) 1 < 4 so I went to the 1st inner nested loop. I used the 0 for the k first and (k) 0 < 1 so I printed out a space (now here's where I get lost) I then incremented k by 1, so k = 1, but 1 is not less than 1 (i) and so I moved on to the next nested for loop. So when this line is printed, I'll only have one space when there should be 4.
View Replies
View Related
Oct 12, 2014
int count = 0;
for (int x = 0; x < 3; x++)
for (int y = x; y < 3; y++)
for (int z = y; z < 3; z++)
count++;
What is the value of count after the code segment is executed?
The answer is 9, but I don't really understand how this answer was derived.
View Replies
View Related
May 20, 2014
I'm having trouble figuring out the Big-O notation for this loop. For now I only have it in pseudocode. I hope you understand it anyway
for i = 1 to n do
j = 1
while j <= n/2 do
print "*"
j = j + 1
end while
end for
View Replies
View Related
Feb 13, 2014
I don't know how to write a program based on for loop like pattern printing.etc
View Replies
View Related
Sep 27, 2014
how does this nested enhanced for loop execute? I dont know how to indent the code.
for (String exam : exams)
for (String level : levels)
for (String grade : grades)
System.out.println(exam+":"+level+":"+grade);
Note:
exams,levels,grades are arraylist
exams contain Java,Oracle
levels contain Basic,Advanced
grades contain pass,fail
View Replies
View Related
Jan 19, 2015
I built a java code to print the following pattern using nested loop:
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
And here is my code
public static void main(String[] args) {
for(int i=1; i<=8;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(" ");
[Code] .....
which gives the partially true output:
123456781
123456721
123456321
123454321
123454321
123654321
127654321
187654321
I couldn't figure out how to place the spaces.I have made some changes on the code, but it didn't work.
View Replies
View Related
Jun 14, 2014
I am having is not being able to print out the diamond the amount of times the user enters (for example, when it asks how many diamonds, I enter a 2 and only one diamond comes out.)
import java.util.Scanner;
public class Pattern5
{
public static void main(String[] args)
{
// For this pattern program, ask the user how many diamonds
// she would like to print to the screen. The following pattern is one diamond.
[code]...
View Replies
View Related
Jan 23, 2015
Implementation of the nested for loop. I thought it was going to go smoothly but apparently not.Purpose: This program is meant to print out all the prime numbers up to a certain range using the algorithm Sieve of Eratosthenes.
Update output: Works as expected.
Java Code:
import java.util.Scanner;
import java.util.ArrayList;
public class PrimeFactors
{
static ArrayList<Double> primeList = new ArrayList<>();
static double range = 0;
static double maxPrime = 0;
static double primeTester = 0;
[code]....
View Replies
View Related
Jan 24, 2014
I just started playing around with Java for awhile, but got caught up in a problem when using ArrayList.
CinemaAppMain
public class CinemaAppMain {
public static void main(String[] args) {
new CinemaApp().start();
[Code]....
I am trying to get the movie name and theatre title from their ArrayList, and I know I need to go through the movie list & theatre list to find their name & title, and compare it with the user input and print out the result.
I use for(int i=0;i<movies.size();i++) to go through the the movie list, and I tried using for(int i=0;i<theatres.size();i++) to go through the theatre list. But when I printing multiple time with different user input value, it will show me strange result (screening have same movie name & theatre name, the else if statement is printed below the user input, user input is printed more than once after the first time).
Edit: I know its wrong for me to use nested for loop,if not, another solution is needed to get the item inside both of the list. (getting the "title" from movie list & "name" from theatre list)
View Replies
View Related
Sep 26, 2014
Write a nested loop for a pyramid pattern. Well here's an ugly description of what it should look like:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 128 64 32 16 8 4 2 1
I managed to make the pyramid using addition. However, how to turn it into multiplication x 2.
import java.util.*;
public class NumbersPyramid
{
public static void main(String[] args) {
int odd = 1;
int numSpaces = 7;
[code]....
View Replies
View Related
Oct 1, 2014
I am working on my java program it prints out a table of angles and the sin, cos, and tan. For extra credit he wanted us to use nest loops to create a space after every five lines. I have come real close to getting this to work but have a problem with the very end of the table. The table needs to stop at 180...
public static void printTable(double angle,double sin,double cos,double tan) { // Method to create a table for the values
for (double j = 0; j <= 180;) {
for (double i = 1; i <= 5; i++) {//loop to print five lines of code
angle = Math.toRadians(j);
[Code] .....
Here is the end of the output of the table:
175.00.0872-0.9962-0.0875
176.00.0698-0.9976-0.0699
177.00.0523-0.9986-0.0524
178.00.0349-0.9994-0.0349
179.00.0175-0.9998-0.0175
180.00.0-1.00.0
181.0-0.0175-0.99980.0175
182.0-0.0349-0.99940.0349
183.0-0.0523-0.99860.0524
184.0-0.0698-0.99760.0699
View Replies
View Related
Jan 26, 2014
I am having problem with some problems like this nested for-loop for instance ....
-----1-----
----333----
---55555---
--7777777--
-999999999-
Now I have to write a method called printDesign that produces the following output but i am not even entirely sure how to start it out now of course i know how to make a for-loop but and i guess if i was to do something for this i would do this .....
Java Code:
for(int*j=0;j<N-k;j++){
System.out.print("-");*
for(int*j=0;j<i;j++){
System.out.print(i);*
for(int*j=0;j<N-k;j++){
System.out.print("-");*
System.out.println();*
}
}
} mh_sh_highlight_all('java');
View Replies
View Related
Apr 2, 2015
Four experiments are performed, each consisting of six tests. The number of test results for each experiment is entered by the user. Write a Java program using a nested loop to compute and display the average of the test results for each experiment.
You can run the program by entering the following test results:
Experiment 1 results:23.231.516.927.525.428.6
Experiment 2 results:34.845.227.936.833.439.4
Experiment 3 results:19.416.810.220.818.913.4
Experiment 4 results:36.939.549.245.142.750.6
View Replies
View Related
Sep 9, 2014
I have my CheckerboardViewer done, my component is what I need finishing. My final product needs to be a checkerboard with alternating red and grey squares but the background is already grey. My code for the Viewer is:
import javax.swing.JFrame;
public class CheckerBoardViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setTitle("CheckerBoardViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CheckerBoardComponent component = new CheckerBoardComponent();
frame.add(component);
frame.setVisible(true);
}
}
And this is what I have of the Component:
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
[code]....
I've created one square so far, but I'm having trouble creating the code for at least the second square.
View Replies
View Related
Apr 27, 2014
Here is a program that is suppose to display a checkerboard on an applet as shown below but it is not.
/*
23.7 (Draw a Checkerboard Pattern) Write an applet that draws a checkerboard pattern as follows:
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
*/
import java.awt.Graphics;
import javax.swing.JApplet;
public class Exercise23_7 extends JApplet
{
// method paint to paint a checker board pattern
public void paint( Graphics g )
[Code] .....
View Replies
View Related
Mar 8, 2015
Anyway I am creating a game for my A2 coursework, most commonly known as Checkers. I have completed my code and everything works as I had planned except that the CheckerBoard itself as well as the checkerpieces do not appear to be showing.
The section of were my board should be present is just a black space. Although my board does not appear to be displaying, all of the actions I perform on it such as clicking certain section produces the planned response, and although I've checked through my code I cannot work out what I've done wrong.
CheckerBoard content = new CheckerBoard(); // Sets the CheckerBoard values into the content to be used in the next line
application.setContentPane(content); // Container holds the values together, Content pane of the CheckerBoard
application.pack(); // Use preferred size of content to set size of application.
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
application.setLocation( (screensize.width - application.getWidth())/2,
(screensize.height - application.getHeight())/2 );
[Code] ....
I have noticed is that within the BoardComplete(), if you change the setBackground(Color.BLACK) to WHITE, it will change the colour of the section to white, but the checkboard is still unshown.
View Replies
View Related
Apr 23, 2014
Driver:
*initiating theGrid[][]*
public Zhang()
{
con.setLayout(new FlowLayout());
for (int xDimension = 0; xDimension<theGrid.length; xDimension++)
[Code] ....
All I'm getting as a blank screen and I've searched for what to put into object oriented code to make the image show up how to program using 2d arrays. I don't get any error messages (which is good I suppose) but how to make the image show up because I have stuff under the graphics and draw methods, did I do something wrong where initializing the array?
View Replies
View Related