public static void main(String[] args) {
// TODO code application logic here
int b=10;
int a= 5;
jmp0:
while (b> 10)
{ if (a>5)
continue jmp0;
else
continue jmp1;
}
jmp1: System.out.print("Zulfi");
}
}
I have a question related to above code. Is using “continue jmp0” same as just using “continue;” in the above code and “continue jmp1;” is giving an error because "jmp1" is outside the block?
continue isn't a jump that you can use to go anywhere. It will just move the execution of the code to the start of the loop you have labelled.
Labels are only used to mark loops you'll want to continue to or break from later. Not random lines of code you want to jump to. So yeah, your jmp1 label is totally out of scope
If you want to use continue with label then your label must be a loop label.
Related
I just started learning how to program in Java a month ago. I am trying to make my robot (karel) put a beeper the amount of times that is indicated in the "put" integer only, not the total amount the object has. However, it is not a set number and karel.putBeeper(put); does not get accepted in the compiler due to the class not being applied to given types. Any help would be greatly appreciated, and I am starting to understand why Stack Overflow is a programmer's best friend lol. Note: I might not respond to to any helpful tips until tomorrow.
import java.io.*;
import java.util.*;
public class Lab09 {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.println("Which world?");
String filename = input.nextLine();
World.readWorld(filename);
World.setSize(10,10);
World.setSpeed(6);
Robot karel = new Robot(1,1,World.EAST,0);
int pick=0;
int put=0;
for(int i=0; i<8; i++) {
while(karel.onABeeper()) {
karel.pickBeeper();
pick++;
karel.move();
}
for(i=0; pick>i; pick--) {
put++;
}
if(!karel.onABeeper()) {
karel.move();
}
while(karel.onABeeper() && put>0) {
karel.putBeeper(put);
}
}
}
}
If I got your question right, you're trying to putBeeper put times, which is done by the following code:
while (karel.onABeeper() && put > 0) {
karel.putBeeper(put);
}
The issue I see here is that you're not changing the value of put after calls to putBeeper, hence this while loop will never terminate: for instance, if the value of put was 5 during the first loop iteration, it will always remain 5, which is larger than 0. Also, as you've mentioned, putBeeper doesn't take any arguments, hence trying to pass put as an argument won't work - the compiler catches that error for you.
If your intent is to call putBeeper put times then what you can do is decrement put after every invocation of putBeeper - put will eventually reach 0, at which point you've called putBeeper exactly put times. And since you're just learn to program in Java, I'll leave the actual implementation to you as an exercise. Good luck!
I have a simple hangman game that has a Jlabel that is supposed to show how many times the word was guessed right. It uses a simple counter wins++ and will show up properly after the first win but any after won't work. The counter won't add more so do I need to use a loop in some way?
if (word.equals(dashes.toString()))
{
wordOutput.setText("You Win!");
wins++; //add 1 to win counter
winsOutput.setText("Wins: " + wins);
}
Seems like this should be simple but I don't know what's wrong
I think you made the win variable as a local, which will get its initial stage after every execution, make the win variable outside of the method and you can also make it static.
public class Hangman {
// here is the variable..
private int wins = 0;
// Here are your other methods..
public void processGame(){
}
}
By the way, it will also works fine without static if your game does not have more than one classes etc. Give it a try with static and without it, but the main point is initializing it outside of the class.
edit: As I was writing this post, I made my code simpler (lost arrays entirely) and got it working. Yet I am still not sure why this specific code won't work, so I'll keep the question.
Hello.
I am writing a small puzzle game in Java (using Eclipse 4.4.2) and stumbled upon a problem inside one of my methods. Basically - it won't complete the method, it just exits the method after the for loop is done without any warnings or errors (I'm not catching exceptions either). I hope I missed something simple..
Details:
I have a method to set the colors of an object and up to 5 other objects that are linked to it through lines. I set up the color of the main object, then find the linked objects through for-loops and in the end change their colors as well. (Double checked the code for Lines, there are simple return methods and nestA and nestB as data - no problem there). lines is an array with a length of 50, nests as its members.
Here's the code:
public void highlightNests(Nest nest) {
//setting the color of the main object (a nest).
Mappers.setColor(nest, nestHighlight);
//resetting the array. Temp solution, had a return method earlier,
//this is part of the debugging.
connectedNests = null;
connectedNests = new Nest[5];
int i = 0;
Gdx.app.log("highlightNests()", "starting the loop");
for (int j=0; j<lines.length; j++) {
if (lines[j].getNestA() == nest) {
connectedNests[i] = lines[j].getNestB();
i++;
}
if (lines[j].getNestB() == nest) {
connectedNests[i] = lines[j].getNestA();
i++;
}
}
//This is where the program exits the method. The following
//lines are not run.
Gdx.app.log("highlightNests()", "entering loop");
for (int l=0; i<connectedNests.length; l++) {
Mappers.setColor(connectedNests[l], nestHighlight);
Gdx.app.log("highlightNests", "set color");
}
}
Deleting the middle section makes the end part run, so there are no errors in the last part.
Your second loop is completely wrong, you declare the counter as l and increment another counter i, you should use l<connectedNests.length change it like this:
for (int l=0; l<connectedNests.length; l++) {
Mappers.setColor(connectedNests[l], nestHighlight);
Gdx.app.log("highlightNests", "set color");
}
And the program won't finish method and exits before the loop because, it doesn't even enter the loop as it's incorrect.
You have to use l instead of i in condition like,
for (int l=0; l<connectedNests.length; l++)...
//---------^ l not i
This question already has answers here:
Can java labels be used appropriately outside of for loops?
(2 answers)
Closed 7 years ago.
Somewhere going through java good articles, I found that such code compiles perfectly.
public int myMethod(){
http://www.google.com
return 1;
}
description says that http: word will be treated as label and //www.google.com as comment
I am not getting how Java Label is useful outside loop?
Under what situation Java Label outside loop should be used?
Here is one benefit of using labels in Java:
block:
{
// some code
if(condition) break block;
// rest of code that won't be executed if condition is true
}
Another usage with nested-loops:
outterLoop: for(int i = 0; i < 10; i++)
{
while(condition)
{
// some code
if(someConditon) break outterLoop; // break the for-loop
if(anotherConditon) break; // break the while-loop
// another code
}
// more code
}
or:
outterLoop: for(int i = 0; i < 10; i++)
{
while(condition)
{
// some code
if(someConditon) continue outterLoop; // go to the next iteration of the for-loop
if(anotherConditon) continue; // go to the next iteration of the while-loop
// another code
}
// more code
}
Just because it compiles does not mean it is useful.....
Labels are often overlooked in Java (who uses labelled break, and continue ...? ). But, just because the label is not used does not mean it is illegal.
OK, so I created a console app that, among other things, takes an array of numbers and prints them out one by one, line by line. Now, I have to take the class that I created for that console app, and pop it into a separate GUI app we're creating. I have all of the other methods working fine, but for the life of me I cannot get the array method to print out correctly. It just gives me the last number I typed into the text field. I'm hoping someone can give me a nudge to help me figure this part out so I can move along, and get to the whole SpringLayout stuff, (the main part of the new assignment) I am limited in what I can show you here because this is a current assignment, so I will have to stick to this stuff as specifically as I can. And please, don't just post the code as an answer, (because then I can't use it), thanks.
Here's what I had for my original project for the array method:
int [] getArray(int x)
{
breakUpNum(x);
return numAry;
}
From there, inside my new class I have this, in an attempt to get it to print:
private class ButtonTest implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Lab1 tester = new Lab1();
int[] test4 = tester.getArray(num);
for(int i = 0; i < test4.length; i ++)
{
crossTest.getArrCross.setText("" + test4[i]);
}
}
}
Any help pointing me in the right direction would be greatly appreciated, thanks!
setText does just that, sets the text you pass to as the current text content, it does not append it.
If you were to use JTextArea, you could use it's append method...however, for a JTextField you need to have a different approach.
Now you could use getArrCross.setText(getArrCross.getText() + test4[i])...but to quite frank, that's rather inefficient, as each call to setText is going to stage a paint event...
StringBuilder sb = new StringBuilder(128);
for(int i = 0; i < test4.length; i ++)
{
sb.append(test4[i]);
}
crossTest.getArrCross.setText(sb.toString());
Now, if you want to separate each element, you need to add
if (sb.length() > 0) {
sb.append(", ");
}
Before sb.append(test4[i]);
The last bit of actionPerformed in the for loop isn't working right. setText replaces the current text with its argument, and it doesn't seem like you want to do that. To fix it, replace the line in the for loop with this:
crossTest.getArrCross.setText(crossTest.getArrCross.getText() + test4[i]);