I can't seem to figure out why my for loop is not working. I have been on the for about an hour now and the closest I have come is this. I'm trying to find out how many McNuggets you can buy using the 6, 9, and 20 packs. All I need dopeChecker(x) to do is return a true or a false. I haven't implemented check the next number yet because it wont even find that a 6 pack can be bought yet. I know it's in the loop somewhere but I just can't find out where. This is form the MIT open course ware problem 2. I don't know if you guys have seen it but I'm just letting you know this is where I'm getting my info.
int x = 0, y = 0, z = 0;// These will the the pack of McNuggets that we can buy.
int testFor = 0; //This will be the number of McNuggets we are looking for.
int matches = 0; //This will be the number of consecutive matches we will be looking for.
public void dopeEquation(){
while (matches < 6){//It's 6 Because that is the smallest order of nuggets we can buy.
//Looking for smaller nuggets then we can buy would not make sense.
while (testFor < 6){
testFor++;
}
if (dopeChecker(testFor)){
matches++;
} else{
matches = 0;
System.out.println(x + "," + y +"," + z +"," + testFor + "," + matches);
}
}
}
private boolean dopeChecker(int testFor){
for ( x = 0 ; x*6 <= testFor; x++){
for ( y = 0 ; y*9 <= testFor; y++){
for (z = 0 ; z*20 <= testFor;){
System.out.println(x + "," + y +"," + z +"," + testFor + "," + matches);
if (x*6 + y*9 + z*20 == testFor){
matches++;
System.out.println("match");
return true;
}else{
System.out.println("no match");
}
}
}
}
return false;
}
}
The z variable is always 0, you're not changing it.
Your code goes inside the first while loop:
while (matches < 6){
Then increased testFor to 6 with the following code:
while (testFor < 6){
testFor++;
}
Then goes to dopeChecker:
dopeChecker(int testFor)
Then insde the 3rd loop, for z:
for (z = 0 ; z*20 <= testFor;) {
...
}
z is never incremented, so you need to write this as:
for (z = 0 ; z*20 <= testFor; z++){
}
The following is the innermost for loop. I have commented where the problem is. as you can see, z never gets implemented. therefore, the loop will never terminate, as 0 <= testFor, since TestFor >= 6.
for (z = 0 ; z*20 <= testFor;/* incrementor needed here*/){
System.out.println(x + "," + y +"," + z +"," + testFor + "," + matches);
if (x*6 + y*9 + z*20 == testFor){
matches++;
System.out.println("match");
return true;
}else{
System.out.println("no match");
}
}
Related
I understand what out of bounds exception means, and how it happens, but I can't find out why it's happening in my code. Also, the output "Count for side 1" always states 0. This is my first post, but I think I am posting this right.
This is where I think the problem is.
System.out.println("Now rolling " + chosenRollNumber + " times. ");
int[] count = new int[chosenRollNumber];
for (x = 0; x < chosenRollNumber; x++) {
dieNumber = RNG(randomNum, min, max);
System.out.println("dieNumber " + dieNumber);
count[dieNumber]++;
}
System.out.println("Done rolling all dice");
for(x = 0; x < numberOfSides; x++) {
System.out.println("Count for side " + (x + 1) + " is " + count[x]); }
while(true) {
Method RNG(randomNum, min, max) is expected to return values in the range [min...max] (inclusive), while dieNumber as the index in count array needs to be in the range [0; numberOfSides), and the following relation exists numberOfSides == max - min + 1.
So, a correction is needed to transform dieNumber into a valid index:
System.out.println("Now rolling " + chosenRollNumber + " times. ");
int[] count = new int[numberOfSides];
for (x = 0; x < chosenRollNumber; x++) {
dieNumber = RNG(randomNum, min, max);
System.out.println("dieNumber " + dieNumber);
int dieIndex = (dieNumber - min) % numberOfSides;
count[dieIndex]++;
}
This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 3 years ago.
I'm new to Java and while doing some homework I came across this example:
String result = " ";
for (int r = rows(); r >= 0; r++) {
result += ("___") + (r == 0 ? (" ") : ("_"));
}
for (int y = columns(); y >= 0; y++) {
for (int x = 0; x <= rows(); x++) {
result += ("|") + ((located && theLocation(y, x)) ? (youWin + "S"
+ " ") : (" " + (mysterySpot[y][x] == 'S' ? (" ") :
(mysterySpot[y][x])) + " "));
}
If I understand this correctly, the first for-loop should be equivalent to:
for (int r = rows(); r >= 0; r++) {
result += "___";
if (r == 0) {
result += " ";
}
else {
result += "_";
Am I reading it correctly? For the second part, it looks like there's an if-else statement within another if-else statement. This is the part I'm confused about, what would the code look like if I were to write it out as if-else statements?
Translated into ifs and elses, this looks something like the following.
for (int y = columns(); y >= 0; y++) {
for (int x = 0; x <= rows(); x++) {
if (located && theLocation(y,x)) {
result += "|" + youWin + "S ";
} else if (mysterySpot[y][x] == 'S') {
result += "| ";
} else {
result += "| " + mysterySpot[y][x] + " ";
}
}
}
Note that you should almost never use single-letter variable names, and if you do, you should try to make them meaningful. In this particular case, using y for a column number and x for a row number is the complete reverse of what anyone would expect.
Never write code like the code in this book. My best advice would be to learn from a different book.
So the "?" and the ":" are read like this....
If a is less than b put x else put y.
This statement is shown in code here.
if (a < b){
// Does a thing
x;
}else{
// Does a cooler thing
y;
}
Alternatively, we can write it like so...
a < b ? x : y
So the part before the "?" is asking if it is true. If a is indeed greater than b then do x the ":" is the else statement or the alternative
[the question] ? [option1 if true] : [option2 if false]
I have Loop and I am trying to print the nth term when the loop is less than 40, all of this without stoping the loop when is less than 40. I have tried to print it when the total is less than 40 but I it is pringting the last nth value.
All I need is th nth value when the loop drops bellow 40. I have almost complete my code but this problem is slowing me down.
Working example, This is what the output is supposed to be
Please input mark: 82
Please input number of days to display: 3
Scheme 1
(0) 82.0 (1) 73.80 (2) 66.42 (3) 59.78
This work can be up to 6 days late before failing.
.
.
This is what my program outputs
Please input mark: 82
Please input number of days to display: 3
Scheme 1
(0) 82.0 (1) 73.80 (2) 66.42 (3) 59.78
.
.
loop
int yourValue = -1;
// Loop
while (true)
{
if (numOfDays >= i) System.out.print("(" + i + ") ");
System.out.printf("%.02f",total);
System.out.print(" ");
total = total * 0.9;
if (total <= 20) {
if (numOfDays >= i) System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark);
break;
}
if (total < 40 && yourValue == -1) yourValue = i;
i++;
}
System.out.print("\nThis work can be up to " + yourValue + " days late before failing.");
I don´t know for sure if I correctly understand what you problem is, but try this code:
int yourValue = -1;
// Loop
for(i = 0;i <= numOfDays;i++){
System.out.print("(" + i + ") " + total+ " ");
total = total -5;
//yourValue will only be filled the first time total is below 40
if (total < 40 && yourValue == -1) yourValue = i;
if (total<=20){
System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark);
break;
}
} // End
if (yourValue > -1) System.out.print("\nThis work can be up to " + yourValue + " days late before failing.");
If total falls under 40, the value of i is stored in yourValue
If total falls under 20 the loop is exited and you can continue with the code behind the loop
Edit:
You can´t "get value beyond loop" but you easily can restructure you code:
(code not tested)
int total2 = total;
//Loop
for(i = 0;i <= numOfDays;i++){
System.out.print("(" + i + ") " + total+ " ");
total -= 5;
if (total<=20){
System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark);
break;
}
}
System.out.print("\nThis work can be up to " + Math.floor((total2 – 40) / 5)+ " days late before failing.");
Edit2:
Note that you either have to round the value for total into an integer or you have to use a double for total (In example i expect that total is a double)
int i = 0;
int yourValue = -1;
while (true)
{
if (numOfDays > i) System.out.print("(" + i + ") " + total+ " ");
total = total * 0.9;
if (total <= 20) {
if (numOfDays > i) System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark);
break;
}
if (total < 40 && yourValue == -1) yourValue = i;
i++;
}
System.out.print("\nThis work can be up to " + yourValue + " days late before failing.");
The loop is running as long as the total as greater than 20 or i is smaller than numOfDay, but for the output it is checked if i is still lower than numOfDays
Edit3:
double total2 = total;
//Loop
...
//End Loop
int counter = -1;
while(total2 >= 40) {
counter++;
total2 = total2 + 0.9;
}
System.out.print("\nThis work can be up to " + counter+ " days late before failing.");
Edit4:
I think I found a formula in exchange for this one Math.floor((total2 – 40) / 5)
try out following:
Math.floor((Math.log(40/total2)/log(0.9))+0.5)
had no time to check this out in practice but it should work.
I am not sure if I understand this correctly, but a code which outputs your expected behavior is something like:
boolean isFinished = false;
int lastValue = 0;
for(i = 0;i <= numOfDays;i++){
System.out.print("(" + i + ") " + total+ " ");
total = total -5;
if (total<=20){
System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ total);
break;
}
if (!isFinished && total < 40){
isFinished = true;
lastValue = i;
}
}
if(isFinished) {
System.out.print("\nThis work can be up to " + lastValue);
}
your if statement it out of the for the for loop block it suppose to be like this:
// Loop
for(int i = 0;i <= numOfDays;i++){
System.out.print("(" + i + ") " + total+ " ");
total = total -5;
if (total<=20){
System.out.print("\nBecause mark drops below 20, mark stays as 20.
final mark="+ finalMark);
return;
}
if (total < 40){
System.out.print("\nThis work can be up to " + i);
}
}
public class NormalNumbers {
public static void main(String[] args) {
int x = 1;
while ((x >= 1) && (x <= 100)) {
System.out.println("x = " + x);
x = x + 1;
}
}
}
The current output is:
x = 1
x = 2
...
x = 100
I want to change the format to:
x=1 x=2 x=3 x=4 x=5
x=6 x=7 x=8 x=9 x=10
and so on.
How do I achieve that?
Instead of using println(), which automatically inserts a newline character at the end of whatever you're printing, just use print() and add an extra space to pad your entries.
If you want to inject a newline after 5 entries specifically, you can do so with an empty println() and the modulus operator like so:
while ((x >= 1) && (x <= 100)) {
System.out.print("x = " + x);
if (x % 5 == 0) {
System.out.println();
}
x = x + 1;
}
Divide your counter by 5 using modulus division if there is no remainder then create a new line:
int x = 1;
while ((x >= 1) && (x <= 100))
{
System.out.print("x = " + x + " ");
if(x % 5 == 0)
{
System.out.print("\n");
}
x = x + 1;
}
println is next line, print is on the same line.
x % 5 == 0 checks that the x values is a multiple of 5 or not.
int x = 1;
while ((x >= 1) && (x <= 100)) {
if (x % 5 == 0) {
System.out.println("x="+x);
} else {
System.out.print("x=" +x+ " ");
}
x = x + 1;
}
That gives you output as
x=1 x=2 x=3 x=4 x=5
x=6 x=7 x=8 x=9 x=10
x=11 x=12 x=13 x=14 x=15
x=16 x=17 x=18 x=19 x=20
-----
System.out.println prints the text and adds a new line. Use System.out.print to print on the same line instead.
So it would be something like this:
System.out.print("x=" + x + " ");
To add a new line each 5 numbers, use:
// if x is multiple of 5, add a new line
if (x % 5 == 0) {
System.out.println();
}
PD: You can use x++ (increment operator) or x += 1 (in the case you want to increase in more than one unit) instead of x = x + 1.
PD2 : You might want to use a tabulation (\t) instead of a space for separating your numbers. That way, numbers with two digits will have the same indentation than numbers with one digit.
System.out.print("x=" + x + "\t");
I think that in your case the better way is using for(;;) statement:
for (int x = 1; x > 0 && x < 101;)
System.out.print("x = " + x + (x++ % 5 == 0 ? "\n" : " "));
The ternary operator x++ % 5 == 0 ? "\n" : " " is responsible for the new line and increment the x variable.
Output:
x = 1 x = 2 x = 3 x = 4 x = 5
x = 6 x = 7 x = 8 x = 9 x = 10
...
x = 96 x = 97 x = 98 x = 99 x = 100
I've got a for-loop running, and it asks them for the increment number. But when it outputs the table, it actually doubles the increment. So if I put in 10 for incr it will go up by 20. Please help?
for (double x = fah1; x <= fah2; x+=incr) {
double cel = 5/9.0 * (x-32);
if (x <= 99){
System.out.println(x + " " + df.format(cel);
x+= incr;
}
else {
System.out.println(x + " " + df.format(cel));
x+=incr;
}
}
You are incrementing twice per loop, once in either the if or else, and again at the end in the third statement of the for loop declaration.
Remove the increments in the if and else and rely on the one in the for loop declaration.
You are incrementing twice - once in the for loop declaration and once in the if-then-else statement:
Try changing it to this:
for (double x = fah1; x <= fah2; x+=incr) {
double cel = 5/9.0 * (x-32);
if (x <= 99){
System.out.println(x + " " + df.format(cel);
// Remove the increment from here
}
else
{
System.out.println(x + " " + df.format(cel));
// Remove the increment from here
}
}