Initializing a variable within an if statement? (For a game) [duplicate] - java

This question already has answers here:
variable access outside of if statement
(5 answers)
What is 'scope' in Java?
(2 answers)
Closed 2 years ago.
I'm new to programming and I thought it would be fun to build a game, but I've run into an issue. I want to make it to where the user can collect an item once but once they collect it they can't collect it anymore. The way I'm seeing if they collect it is by changing the value of that variable from 0 to 1. This is what I've got for the collection, but it doesn't seem to be working. It's saying it can't find the variable yesCookie. I also want to point out that this is just the section that allows you to take it or not, the variable stuff is somewhere else.
if(moldyCookie == 0)
{
String yesCookie = "In a cavity within the wall you see a mold-covered cookie. Bleh. \nYou obtained the Rotten Cookie!\n";
}
else
{
String yesCookie = "There's nothing but an empty cavity in the wall. ";
}
System.out.println("You walk forward towards the painting, moving it aside."+yesCookie+"You return to the door.");
System.out.println();

Related

Performance and variables [duplicate]

This question already has answers here:
Java performance vs. code-style: Making multiple method calls from the same line of code
(7 answers)
Calling getters on an object vs. storing it as a local variable (memory footprint, performance)
(6 answers)
Closed 5 years ago.
I had a performance issue when I was thinking about changing the style of the code. Previously, I liked everything to minimize, for it seemed like something to increase performance.
Example 1
new Run(path).start();
From the point of view of refactoring, it is better to write so:
Run run = new Run(path);
run.start();
So it's easier to read the code, but the question is, will I lose something because of this? Because the run is placed in the RAM?
Example 2
The line from my project (yes, creepy):
tabPane.getTabs().get(i).setUserData(Paths.get(newFile.substring(0, indexOfDifference) + name + path.toString().substring(indexOfDifference + nameOfDifference.length(), path.toString().length())));
It is difficult to read this code to a person, I understand, but if I sign every step:
String firstHalf = newFile.substring(0, indexOfDifference);
String secondHalf = path.toString().substring(indexOfDifference + nameOfDifference.length());
Path path = Paths.get(firstHalf + name + secondHalf);
tabPane.getTabs().get(i).setUserData(path);
Will I lose significant performance?
P.S. Sorry if the topic was repeated, but did not know how to write a search query.

Select all element that start with a specific numbers [duplicate]

This question already has answers here:
Retrieving the first digit of a number [duplicate]
(9 answers)
Closed 6 years ago.
Here is my code:
if(Rs.getInt("Number") == 100){
//Do something
}
Rs is a PreparedStatement.
Instead of defining the exact number, I want to look for numbers
which start with 1. Maybe there are '110' or '120' ... what should i write in the loop
instead of the operator == ?
This guy here seems to have the right idea.
Retrieving the first digit of a number
if ("1".equals(Integer.parseInt(Rs.getInt(number).substring(0, 1)))){
//do stuff
}

java string validation for 3 choices [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 6 years ago.
I'm trying to make a user validation for multiple choices using a while loop and If the user doesn't match the choices then the program has to terminate. This is what I got so far
while(input!="air" || input!="water" || input!="steel"){
System.exit(0);
}
Unfortunately, it doesn't work.
I would prefer to do exceptions, but my professor is strictly against using it for this assignment. I really want to grow as a programmer so any advice, tips and tricks are welcomed, thank you all for trying to help me out.
Change it as
while(!input.equals("air") && !input.equals("water") && !input.equals("steel")){
System.exit(0);
}
Or
while(!(input.equals("air") || input.equals("water") || input.equals("steel"))){
System.exit(0);
}

What is this element and how does one use it? [duplicate]

This question already has answers here:
"loop:" in Java code. What is this, and why does it compile?
(12 answers)
Closed 7 years ago.
In this code:
search :
for (int i = 1; i <= 30; i++) {
System.out.println(i);
if (i == 20) {
break search;
}
}
What is search and how does one use it?
And wouldn't this code do the same without it?
search: is a label. You use labels with loops (break and continue) to indicate which loop is to be exited or continued.
Tutorial: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
In this case, there's only one loop. So yes I think the program would work the same without the label. However, occasionally I've seen labels used as a kind of comment to indicate what exactly is being done, and I guess that's what the coder here has done

How could I fix this side effect? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I am having a problem with the code below; the issue is that after you don't say map the first time, it keeps repeating No try again with the input box, no matter if you type "map" or not.
System.out.println("My cousin Diego is in trouble in the Majestic palace. But how do we get there?");
System.out.println("Who do we call when we don't know where to go? Huh? I didn't get that...");
imTheMap=robotMagic.next();
if (imTheMap.equals("map"))
{
System.out.println("That's right!"); //the issue is here
}
else
{
while(imTheMap!=("map"))
{
System.out.println("No! try again");
imTheMap=robotMagic.next();
}
}
By changing
while(imTheMap!=("map"))
to
while(!imTheMap.equals("map"))
you always should use equals() method to check string equality. as in your if statement.

Categories