I have a counter variable and for every 3 input in the range from 1-5 I need to add an Asterisk to show the total. So if 7 answers in the range 1-5 occurred, it would only print out 2 Asterisks. I need to somehow take the counter total, put it in a loop I'm assuming and for every 3, add the asterisks to the total below.
if(value >= 1 && value <= 5){
counter++;
}
The print out would look something like this
Answers from 1 - 5: ** // from 7 in this range
Answers from 1 - 5: *** // from 9 in this range
This should give the correct result.
int value = counter / 3;
System.out.print("Answers from 1 - 5: ");
for (int i = 0; i < value; i++)
System.out.print("*");
System.out.println();
end should give you the correct result.
Given that the number you want is an integer it will store it as a whole number (e.g. 7/3 = 2). You can use System.out.print() to start the line, print on the same line as many asterisks you need and then end the line.
Hope this helps.
Related
I desire a code output that looks like this:
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Keep in mind that my code takes in the size of the pyramid through input before
My code now looks like:
for(int numRows=sizePyr;numRows>=1;numRows--){
for(int i=sizePyr;i>=numRows;i--){
System.out.print(i + " ");
}
System.out.println();
}
Changing the nested for loop to for (int i = numRows; i >= 1; i--) fixed the issue
You want to start printing i with the current numRows value, then work the way down to 1.
Your current code start printing i with sizePyr (which is a constant 6 throughout the function), then work the way down to numRows.
for(int numRows=sizePyr;numRows>=1;numRows--){
for(int i=numRows; i >= 1; i--){
System.out.print(i + " ");
}
System.out.println();
}
For the first line, you want to start with sizePyr (as your inner loop does), but want to end with 1 (which your loop decidedly does not). In fact, every line should end with 1. Change your loop to reflect this.
I am supposed to print the following output by using loops:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
The highest number in this pattern (in this example, 7) is determined by user input. Here is the applicable code for the pattern:
index=patternLength+1; n=1; //These values are all previously intitialized
while (index!=1) {
index--;
printSpaces((index*2)-2); //A static method that prints a certain number of spaces
while(n!=1) {
n--;
System.out.print(n + " ");
}
System.out.print("\n");
n=patternLength+1-index;
}
And here is the incorrect output for the user input "7":
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
There are two blank lines preceding the incorrect output; these lines have the correct number of spaces necessary for the complete/correct pattern, but for some reason, the actual numbers start printing too "late" in the loop. In other words, the spaces that appear before the "1, 2 1" in the correct example are in the incorrect output. It's some of the numbers that are missing and make the incorrect example incorrect.
OK, I got it.
index=patternLength+1; n=1;int nSetter=1;
//Loop C
System.out.println("Pattern C:");
while (index!=1) {
index--;
printSpaces((index*2)-2);
while(n!=0) {
System.out.print(n + " ");
n--;
}
System.out.print("\n");
nSetter++;
n = nSetter;
}
My problem was that my "n" needed to go both up and down, so the extra variable "nSetter" seems to have solved that, although this may be a round-about solution. Whatever. Thanks to #Andreas for pointing me in the correct direction and #JohnKugelman for the helpful edit.
Please try this code your second while loop is not correct.
int index = patternLength + 1;
int n = 2; //These values are all previously intitialized
int i = 1;
while (index != 1) {
index--;
printSpaces((index * 2) - 2); //A static method that prints a certain number of spaces
while (n != 1) {
n--;
System.out.print(n + " ");
}
System.out.print("\n");
i++;
n = i+1;
}
I have created an int array to hold 100 values and have initialized them to be random integer values from 1-100.
Now my task is to output this array in a 10x10 block separated by tabs.
for example:
48 49 88 ..... (until 10 numbers appear on this line)
45 1 55 ..... (until 10 numbers appear on this line)
59 21 11 ..... (until 10 numbers appear on this line)
until 10 numbers appear going down as well
I'm kind of lost on how to do this, any tips?
A for loop is the easiest way to do this. Assuming x is the name of your single-dimensional, 100-element int array:
for(int i = 0; i < x.length; ++i)
{
System.out.print(x[i]);
if((i % 10) == 9)
{
System.out.print("\n");
//or System.out.println();
//both print a new line
}
else
{
System.out.print("\t");
}
}
\t gives you a tab, \n gives you a new line.
The (i % 10) == 9 part checks to see if you are on the 10th column. If you are, then you create a new line with the \n character.
This will iterate to the end of the array as well and no further, so if you ended up having a different number of columns than 100, this would not run the risk of trying to access an element that does not exist.
What you have to do, is to put a a newline-sign "\n" after each 10th element and a tab "\t" after each other one
Let's say a is your array. Then you could accomplish this at example by doing:
for(int i = 1; i <= a.length; i++){
//print out the element at the i-th position
System.out.print(a[i-1])
//check, if
if(i % 10 == 0){
//you could also simply use System.out.println()
System.out.print("\n");
}else{
System.out.print("\t");
}
}
If you just want to print them in that maner. Use som form of while loop that prints out 10 elements with a tab between them and last element Will have a new line sign because of a If statement for 10th element. This van be done through some form of counter.
Hello i am having a tough time trying to write a function that can create an array that holds integers, that equal my simple math problem.
my problem is not adding integers into the array but finding the correct integers that could add up to a math problem.
for example i have a simple math problem like: 10 + 10 = ? we know it equals 20
so i want my array to hold up to ten integers, that when added all together equal 20.
this is what i have been trying in code but not getting the results i want.
while (totalCount != answer
&& count < setCount) {
randomNumber = rand.nextInt((int) answer / 2) + 1;
if(count < setCount) {
sumOfBalloons.add(randomNumber);
totalCount += randomNumber;
count++;
}
if(totalCount > answer) {
count = 0;
totalCount = 0;
sumOfBalloons.clear();
}
}
i am trying to find random numbers that add up to the math problems answer so i can draw them on balloons. problem is i can never get ten numbers to equal the answer in my while loop.
does anyone know some way of doing something like this?
need array to hold 3 - 10 integers that equals my math problems answer.
** update on code thanks to the advice i received i managed to fix my while loop now it looks like this
had to post like this cause my rep is very low. sorry.
while (totalCount != answer) {
randomNumber = rand.nextInt((int) answer / 2) + 1;
if(totalCount + randomNumber > answer) {
randomNumber = rand.nextInt((int) answer - totalCount) + 1;
}
if(count + 1 == setCount) {
randomNumber = answer - totalCount;
}
if(count < setCount) {
sumOfBalloons.add(randomNumber);
totalCount += randomNumber;
count++;
}
if(totalCount > answer
|| totalCount == answer
&& count < setCount
|| totalCount != answer
&& count == setCount) {
count = 0;
totalCount = 0;
sumOfBalloons.clear();
}
}
this is what i got in my console from this code
Total count = 10
Total totalCount = 20
sumOfBalloons 0 = 2
sumOfBalloons 1 = 3
sumOfBalloons 2 = 3
sumOfBalloons 3 = 2
sumOfBalloons 4 = 1
sumOfBalloons 5 = 4
sumOfBalloons 6 = 2
sumOfBalloons 7 = 1
sumOfBalloons 8 = 1
sumOfBalloons 9 = 1
I think there are a few options here re: generating random numbers that sum to 20.
Here's one possible solution:
Create an array of length 4, for example.
Generate random number between 1 and 6 for each of the first 3 indices of your array.
At this point you'll have an array of the form: { 4, 5, 2, _ } (where our 4th element hasn't been chosen yet).
Sum our first 3 elements: 4 + 5 + 2 = 11. Determine 4th element by calculating 20 - current_total (11) = 9.
Set myArray[3] = 9;
A few things to note:
You may need to modify the range of possible random numbers ( 1-6 ) I've given. Consider what happens if the array we generate turns out to be { 2, 1, 2, _ }...then there's no digit that will ensure the elements sum to 20.
Another option is to use an arrayList instead of an array. The benefit to this is that you can keep adding elements to your arrayList until you either hit 20 (then you're done) or go over (in which case you delete the most recent element and begin adding again). You also won't need (or be able) to know the length of your arrayList in advance.
I get some number from URL, if the number is b/w 1 to 4 then I want's the result to be 4, .. and number b/w 4 to 8 then I want result 8 ......And so on.
This is my code. and this is get from url in count value.
adltavailable = Integer.parseInt(count.get(i).toString());
for(int x =0; x<adltavailable; x++)
{
c = "Adultavailable";
category.add(c);
}
//Here is assign the table
int k = 0;
int size = category.size();
while(k < size)
{
for(int z=0; z<size; z++)
{
if(category.get(z).equals("Adultavailable"))
{
mycirimgs[k].setBackgroundResource(R.drawable.adultadd);
}
k++;
}
}
I get total seats value from url .And the value is assigned in table.If suppose i got 3 seats means I assign the table in 3 seats is not look like good.But this three seats assign 4 instead of 3.like wise.So I want the result If I get total seats 1 or 2 or 3 or 4 means I assign the table in 4 seats and 5 or 6 or 7 or 8 means I assign the table in 8 seats like wise. Thanks giving ur support.
I get some number from URL, if the number is b/w 1 to 4 then I want's the result to be 4, .. and number b/w 4 to 8 then I want result 8 ......And so on.
To round x to the next multiple of 4, write
(x + 3) & ~3
where + 3 rounds up and & ~3 clears the bottom two bits making it a multiple of 4.
To round up your input to the next multiple of 4, you can try the following:
Integer result;
if (input % 4 == 0) //Input is already a multiple of 4.
{
result = input
}
else // round up
{
result = ((input / 4) + 1)*4
}
Hope this is what you were looking for.