Nested For Loop To Capture Structure Of The Figure
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
ADVERTISEMENT
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
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
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
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
Jan 17, 2015
How to capture webcam image. I got this code online which work fine but i modify it by including canvas in the frame. My problem is how to get or draw the image in the canvas. The code below:
package webcam;
import com.lti.civil.CaptureDeviceInfo;
import com.lti.civil.CaptureException;
import com.lti.civil.CaptureObserver;
import com.lti.civil.CaptureStream;
import com.lti.civil.CaptureSystem;
[Code] .....
View Replies
View Related
Feb 10, 2014
Need to develop a Java application to listen to all user events occurring in the (initially only Windows) operating system to record some kind of record and to reproduce the sequence of events below ...
View Replies
View Related
Apr 15, 2009
I have a JTable that is within a JScrollPane. If I try to write add a MouseListener to the JTable it never fires. However, a mouse event does fire for the JScrollPane which it is added to. Is there a way to prevent the JScrollPane from capturing the event or passing it along to the JTable?
View Replies
View Related
Oct 22, 2014
I want to do an app that capture a selected area of a screen and save it. I did a few research and i did the code down below.
My questions are:
1 - How can i open a pdf file in this app ? (i tried use a method but it didnt work. I dont know exactly where to put it on the code)
2 - How can i save the selected area in a new file ? (a image file : JPEG, JPG,png)
3 - [the complex part] right now, the code only "save" one selected area each time. I want to capture a lot of parts of screen and save this in the same image file. one beside the other. How can i do this ?
Java Code: package javaapplication39;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class ScreenCaptureRectangle {
Rectangle captureRect;
ScreenCaptureRectangle(final BufferedImage screen) {
[Code] .....
View Replies
View Related
Nov 1, 2014
I have a combobox with this PopupMenuListener. All works fine, except that the popupMenuWillBecomeInvisible method does not capture the selection made and neither does it show the selection.
PopupMenuListener pmlMqMessagePurged = new PopupMenuListener() {
@SuppressWarnings("unchecked")
public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
try {
JdbcSqlServerDataSource ds = new JdbcSqlServerDataSource(
DbConnectionURL.UToolDbHostName, 0,
[Code] .....
The popupMenuWillBecomeInvisible does fire but the System.out.println() returns 'nul'l as answer to the value i have selected from the combobox.
View Replies
View Related
Oct 11, 2014
For one of my project, required to capture the audit history reports of all the activities of the users within the application.
We are using Servlet/JSP in J2EE environment.
For Ex: Whenever user updates some fields, we need to capture the Old value, new value, user id and time stamp. We have single form with more than 200 fields.
Just want to know the best way to do this..any frameworks available for it...or doing at the database side like using triggers..
View Replies
View Related
May 6, 2014
i am trying to do a program captures keystroke and mouseclick from user in a textarea and insert the details intoa text file using java. Mainly such as delay between keys, no. of times backspace being pressed, alt tab press and copy paste mouse click.
View Replies
View Related
Feb 6, 2015
Implement a Las Vegas slot machine! The machine works as follows. First, it generates three random integers (use import java.lang.Math.*; then call Math.random()*7 to generate a random number between [a,b)) that are in the range of 0-7. Once the numbers are generated, the following rules are used to determine the prize:
- If all three numbers are equal to 7, you are winning $1,000,
- If all three numbers are equal, but not equal to 7, you are winning $500,
- If two of the numbers are equal to 7 and the third one is six, you are winning $400,
- If two numbers are equal, you are winning $100,
- Otherwise you are not winning anything.
And for that I wrote:
import java.lang.Math;
public class Assn1_2150130 {
public static void main(String[] args) {
// Generate three random signle-digit integar from 0-7.
int n1 = (int)(Math.random()*7);
int n2 = (int)(Math.random()*7);
int n3 = (int)(Math.random()*7);
[code]...
But I just can't figure out a way to print out the "YOU WON NOTHING." independently.If I say that n1!=n2 && n2!=n3 && n3!=n1, and then write another line of println. It gives out the number as well as the "NOTHING".
View Replies
View Related