Cannot accurately asses Strings - java

I wrote some code that is supposed to ask for input from the user and assign it to the String skillAssign. When I try to asses skillAssign, it returns false no matter what. Here is my code:
import java.util.*;
public class CharacterCustomization
{
public CharacterCustomization()
{
}
public static void Customization()
{
Scanner keyboard = new Scanner(System.in);
int skillPoints = 100;
String skillAssign = "";
int newMaxHealth = 0;
int newMaxMagic = 0;
int newMaxStamina = 0;
int assignmentValue = 0;
boolean isDone = false;
System.out.println("Welcome to character customization, you have 100 points to allocate to your skills.");
System.out.println("To allocate points, type name of skill, followed by the points you want to assign (blank for positive, - for negative, ex. -5)");
System.out.println("Put Skill on one line, and press enter, then the value on the next line");
System.out.println("Type \"stats\" to view full stats at any time");
System.out.println("Type \"done\" to finish");
while (true)
{
while (isDone == false)
{
skillAssign = keyboard.nextLine();
if (skillPoints == 0)
{
System.out.println("Max Health: " + newMaxHealth);
System.out.println("Max Magic: " + newMaxMagic);
System.out.println("Max Stamina: " + newMaxStamina);
System.out.println("Skill points left: " + skillPoints);
System.out.println("Type \"done\"to finish");
}
if ((!(skillAssign.equals("stats"))) || (!(skillAssign.equals("done"))))
{
assignmentValue = keyboard.nextInt();
if (((skillAssign.equals("health")) || (skillAssign.equals("Health"))) && (skillPoints - assignmentValue >=0))
{
skillPoints = (skillPoints - assignmentValue);
newMaxHealth = (assignmentValue + newMaxHealth);
}
else if (((skillAssign.equals("magic")) || (skillAssign.equals("Magic"))) && (skillPoints - assignmentValue >=0))
{
skillPoints = (skillPoints - assignmentValue);
newMaxMagic = (assignmentValue + newMaxMagic);
}
else if (((skillAssign.equals("stamina")) || (skillAssign.equals("Stamina"))) && (skillPoints - assignmentValue >=0))
{
skillPoints = (skillPoints - assignmentValue);
newMaxStamina = (assignmentValue + newMaxStamina);
}
else
{
//System.out.println("Sorry, I could not read that!");
System.out.println(skillAssign == "stats");
}
}
else if (skillAssign.equals("stats"))
{
System.out.println("Max Health: " + newMaxHealth);
System.out.println("Max Magic: " + newMaxMagic);
System.out.println("Max Stamina: " + newMaxStamina);
System.out.println("Skill points left: " + skillPoints);
}
else if ((skillAssign.equals("done")) || (skillAssign.equals("Done")))
{
isDone = true;
continue;
}
else
{
System.out.println("Sorry, I could not read that!");
}
}
System.out.println("Are you sure this is the setup you want? [y] [n]");
System.out.println("Max Health: " + newMaxHealth);
System.out.println("Max Magic: " + newMaxMagic);
System.out.println("Max Stamina: " + newMaxStamina);
System.out.println("Skill points left: " + skillPoints);
//skillAssign = keyboard.nextLine();
if (((keyboard.nextLine()).equals("y")) || ((keyboard.nextLine()).equals("Y")) || ((keyboard.nextLine()).equals("yes")) || ((keyboard.nextLine()).equals("Yes")))
{
Player player = new Player(newMaxHealth, newMaxMagic, newMaxStamina);
}
else
{
isDone = false;
}
}
}
}
Its not complete, but Im inputting stats when prompted and all evaluations of it are false
Is there a way I can get it to read the variable properly?

You will always get in inside this condition scope if ((!(skillAssign.equals("stats"))) || (!(skillAssign.equals("done"))))
It because that when one word failed to pass it, the other condition will let it pass, because they are different completely.
e.g:
let's take "stats", this will results with if (false || true) => true.
let's take "done", this will results with if (true || false) => true.
let's take "sTats", this will results with if (true || true) => true.
You can't escape it, you need to check it like that:
if (!(skillAssign.equals("stats") || skillAssign.equals("done")))
this way we're checking if either of the logical expression are true, then we change it to "not" and then checking if the whole condition is true.
P.S
A. You can use "abc".equalsIgnoreCase("AbC") instead what you're using now.
B. Don't add for every logical expression parenthesis it's making the code more complex and unclear for the first glance.

The problem was that I was using || instead of && in the if ((!(skillAssign.equals("stats"))) || (!(skillAssign.equals("done"))))

What ever Orel said is pretty much it. Just wanna add if you are using System.out.println(skillAssign == "stats") to asses your skillAssign it will always return false, even if they have the same value.
Because == compare the reference of Strings not their values.
you must use equals() method.

Related

Java while loop won't break (I've set my condition to false, too)

I've made something that rolls five dice until it gets a five-of-a-kind. I will paste my code below so you can see it. I HAVE set my while condition to false if the five-of-a-kind is successful. Nothing happens after it says "Rolling...". Should I be using .equals()?
package omega;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class One {
public static void main(String[] args) throws InterruptedException {
Random random = new Random();
Scanner scan = new Scanner(System.in);
int one = 1+random.nextInt(6);
int two = 1+random.nextInt(6);
int three = 1+random.nextInt(6);
int four = 1+random.nextInt(6);
int five = 1+random.nextInt(6);
boolean rolling = true;
int rollCount = 0;
System.out.println("====================");
System.out.println(" Yahtzee! ");
System.out.println("====================");
System.out.println("");
System.out.println("Type 0 to roll.");
int roll = scan.nextInt();
System.out.println("Rolling...");
while(rolling == true) {
switch(roll) {
case 0 :
if(two == one && three == one && four == one) {
rollCount++;
rolling = false;
}
else {
rollCount++;
}
break;
default :
System.out.println("Invalid roll!");
}
}
System.out.println("Yahtzee!");
System.out.println("Took " + rollCount + " rolls!");
}
}
You're not rerolling inside the loop. If roll isn't 0, it'll be an infinite loop, printing "Invalid roll!" over and over and over.
You need to change rolling to false if roll is not 0.
default :
System.out.println("Invalid roll!");
rolling = false;
Thus when your while loop re-runs, it will notice the false condition and break.
There is few things that can lead to infinite loop here
First one is your roll variable always zero you are not
rolling again or not changing that variable.
Second one is there is less possibility that the one to five variables
going through the if condition.
if(two == one && three == one && four == one) {
rollCount++;
rolling = false;
}
Also you do not need to use while(rolling == true) instead you can use while(rolling) because your rolling variable initialized as true.
Here is what I came up with
while(rolling) {
switch(roll) {
case 0 :
if(two == one && three == one && four == one) {
rollCount++;
rolling = false;
}
else {
rollCount++;
System.out.println("If you want to stop roll press -1: ");
System.out.println("Or type 0 to roll again.");
roll = scan.nextInt();
if(roll == -1){
System.out.println("Yahtzee!");
System.out.println("Took " + rollCount + " rolls!");
return;
}
}
break;
default :
System.out.println("Invalid roll!");
roll = 0;
}
}
System.out.println("Yahtzee!");
System.out.println("Took " + rollCount + " rolls!");

A "Stick Game" program in Java not working correctly?

I've recently decided that I want to make a program that plays a game called "Nim," which is a game in which you start with a predetermined amount of "sticks" and each player takes turns removing between 1 and 3 sticks. Whoever removes the last stick loses.
Anyway, I have written my program and it compiles and runs almost flawlessly. There's only one small problem. After the game is over, it shows the "good game" screen twice, with the game's very first line appearing in the middle (I'll post screenshots at the end here). It's very strange, and I was just wondering if you guys could give it a look.
I'm cutting a chunk of the program out (only one class, named Cup()), because it's somewhat long, so if you see a class you don't recognize then just ignore it. It's pretty self explanatory what the class does in the program, and it's not where the error is occurring. Here's the code.
class SticksGame
{
public static void main(String[] args) throws InputMismatchException
{
Random r = new Random();
int score1 = 0, score2 = 0;
Cup c = new Cup();
int j = 0, d = 0, i = 0, k = 0;
boolean b = true;
String exit = "default";
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Sticks Game! Last Stick loses! Must pick 1 - 3 sticks.");
System.out.println();
do
{
i = r.nextInt(15) + 9;
System.out.println("We begin with " + i + " sticks");
System.out.println();
while (b == true)
{
System.out.println("Your move");
k = input.nextInt();
if (k > 3)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else if (k < 1)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else
{
j = i;
i = i - k;
if (i <= 0)
{
System.out.println("Computer wins!");
score2 = (score2 + 1);
b = false;
}
else
{
System.out.println("We now have " + i + " sticks.");
}
d = c.select();
System.out.println("Computer removes " + d + " sticks");
i = i - d;
System.out.println("We now have " + i + " sticks");
if (i <= 0)
{
System.out.println("You Win!");
score1 = (score1 + 1);
b = false;
}
}
}
System.out.println();
System.out.println("Good game!");
System.out.println("Your score: " + score1 + " Computer's Score: " + score2);
System.out.println("Press enter if you'd like to play again. Otherwise, type \"quit\"");
exit = input.nextLine();
b = true;
}
while(!"quit".equals(exit));
}
}
Any helps are appreciated! Thanks :)
~Andrew
CODE EDITED FOR JANOS
A little late, I know, but here is the FULL GAME for anyone who wants to play! feel free to copy and paste it into your notepad and execute using cmd(YOU MUST KEEP MY NAME AS A COMMENT ON TOP!) :)
//Andrew Mancinelli: 2015
import java.util.*;
import java.io.*;
class Cup
{
private ArrayList<Integer> c = new ArrayList<Integer>();
public Cup()
{
c.add(1);
c.add(2);
c.add(3);
}
public int count()
{
return c.size();
}
public int select()
{
int index = (int)(c.size() * Math.random());
return c.get(index);
}
public void remove(Integer move)
{
c.remove(move);
}
}
class SticksGame
{
public static void help()
{
System.out.println();
System.out.println("Okay, so here's how it works... The object of the game is to NOT have the last stick. Whoever ends up with the very last stick loses.");
System.out.println();
System.out.println("Rule 1: You will each take turns removing sticks. you may only remove 1, 2, or 3 sticks in a turn");
System.out.println();
System.out.println("Rule 2: The beginning number of sticks is always random between 9 and 24 sticks");
System.out.println();
System.out.println("Rule 3: Whoever chooses the last stick, LOSES!");
System.out.println();
System.out.println("And that's it! Simple, right?");
}
public static void main(String[] args) throws InputMismatchException
{
Random r = new Random();
int score1 = 0, score2 = 0;
Cup c = new Cup();
int j = 0, d = 0, i = 0, k = 0;
boolean b = true;
String exit = "default", inst = "default";
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Sticks Game! Last Stick loses!");
System.out.println();
System.out.println("Need some instructions? Type \"help\" now to see the instructions. Otherwise, press enter to play!");
inst = input.nextLine();
if (inst.equals("help"))
{
help();
System.out.println();
System.out.println("press \"enter\" to begin!");
inst = input.nextLine();
}
do
{
i = r.nextInt(15) + 9;
System.out.println();
System.out.println("We begin with " + i + " sticks");
System.out.println();
while (b == true)
{
System.out.println("Your move");
k = input.nextInt();
if (k > 3)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else if (k < 1)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else
{
j = i;
i = i - k;
if (i <= 0)
{
System.out.println("Computer wins!");
score2 = (score2 + 1);
b = false;
break;
}
else
{
System.out.println("We now have " + i + " sticks.");
}
d = c.select();
i = i - d;
if (i >= 0)
{
System.out.println("Computer removes " + d + " sticks");
System.out.println("We now have " + i + " sticks");
}
if (i <= 0)
{
System.out.println("You Win!");
score1 = (score1 + 1);
b = false;
break;
}
}
}
System.out.println();
System.out.println("Good game!");
System.out.println("Your score: " + score1 + " Computer's Score: " + score2);
System.out.println("Press enter if you'd like to play again. Otherwise, type \"quit\"");
input.nextLine();
exit = input.nextLine();
b = true;
}
while(!"quit".equals(exit));
}
}
The problem is that this condition is always true:
while (exit != "quit");
Because != means "not identical",
and the exit variable and "quit" are not identical.
Use the equals method for checking logical equality.
In this example, change the loop condition to this instead:
while (!"quit".equals(exit));
For your other problem of not properly starting a second game,
you need to reinitialize the state variables,
for example reset b = true.
Lastly, note that input.nextInt() doesn't read the newline character that you pressed when entering a number. So when exit = input.nextLine() runs, it reads that newline character, and doesn't actually give you a chance to type "quit". To solve this, add input.nextLine(); right before exit = input.nextLine();
The unexpected retry was because of the use of input.nextLine(); the program assumed that you already pressed [enter].
From previous work, the two options is to insert one more input.nextline();
input.nextLine();
exit = input.nextLine();
Or use input.next(); instead, although enter will not work for this method so you may need to enter any key or "quit" to exit;
exit = input.next();

Having an issueif else and if else statements.

I'm really new to java (third week of class), but I've been trying to work on this code for hours and I just can't seem to find an answer to what I'm doing. javac tells me I only have three errors, but I'm wondering if there's more than that.
Here's my code, and I know my average section still needs work but i just cant figure out what's going on with the middle section of if and else statements. Sorry if this is really dumb, and im sure my syntax is all over the place:
import java.util.Scanner;
public class Program1
{
static public void main( String args [ ] )
{
int grade;
int A,B,C,D,F;
A = 0;
B = 0;
C = 0;
D = 0;
F = 0;
System.out.println( "*************** Grade Computer *************");
// ********************** //
Scanner kbd = new Scanner (System.in);
System.out.println("Enter Students First Name: ");
String fname = kbd.next( );
System.out.println("Enter Students Middle Initial: ");
String mi = kbd.next( );
System.out.println("Enter Students Last Name: ");
String lname = kbd.next( );
System.out.println("Enter First Exam Grade: ");
int firstexam = kbd.nextInt( );
System.out.println("Enter Second Exam Grade: ");
int secondexam = kbd.nextInt( );
System.out.println("Enter Third Exam Grade: ");
int thirdexam = kbd.nextInt( );
System.out.println("Was the bonus done? [yes/no] : ");
boolean b = kbd.nextBoolean( );
boolean yes = true;
boolean no = false;
// *********************** //
if(true)
{
{
if((firstexam >= (secondexam * 0.60 ) ) & (firstexam >= (thirdexam * 0.80 )));
{
System.out.println(firstexam);
}
else if((secondexam * 0.60) >= (thirdexam * 0.80));
{
System.out.println(secondexam * 0.60);
}
else {
System.out.println(thirdexam * 0.80);
}
}
if(true)
{
if((secondexam >= firstexam) & ((thirdexam * 0.80) >= secondexam));
{
if(secondexam >= (thirdexam * 0.80));
{
System.out.println(secondexam);
}
}
else {
System.out.println(thirdexam * 0.80);
}
}
else {
System.out.println(firstexam);
System.out.println(secondexam);
System.out.println(thirdexam);
}
}
// ********************** //
System.out.println(" **********Grade Summary********** ");
double average = calcAverage(firstexam, secondexam, thirdexam);
System.out.println("Grade Report For: " + fname);
if (true)
{
System.out.println("Bonus was done so grades are adjusted if appropriate.");
}
else
{
System.out.println("Bonus was not done.");
}
System.out.println("Exam 1: " + firstexam);
System.out.println("Exam 2: " + secondexam);
System.out.println("Exam 3: " + thirdexam);
System.out.println("The average is: " + average);
determineGrade(average);
}
public static double calcAverage(int firstexam, int secondexam, int thirdexam)
{
double average = (firstexam + secondexam + thirdexam) / 3.0;
return average;
}
public static void determineGrade(double average)
{
if (average>90)
{
System.out.println("Grade: A");
}
else if (average>=80)
{
System.out.println("Grade: B");
}
else if (average>=70)
{
System.out.println("Grade: C");
}
else if (average>=60)
{
System.out.println("Grade: D");
}
else if (average<60)
{
System.out.println("Grade: F");
}
}
// ************** //
}
Your if statements having ; in the end
if((firstexam >= (secondexam * 0.60 ) ) & (firstexam >= (thirdexam * 0.80 )));
They are considering as statements and proceeding further.
Remove all of them in the end of each statement.
if((firstexam >= (secondexam * 0.60 ) ) & (firstexam >= (thirdexam * 0.80 ))) (;)
The ; shouldn't be here.
Difference between & and && :
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
(x != 0) & (1/x > 1) <-- this means evaluate (x != 0) then evaluate (1/x > 1) then do the &. the problem is that for x=0 this will throw an exception.
(x != 0) && (1/x > 1) <-- this means evaluate (x != 0) and only if this is true then evaluate (1/x > 1) so if you have x=0 then this is perfectly safe and won't throw any exception if (x != 0) evaluates to false the whole thing directly evaluates to false without evaluating the (1/x > 1).
An other thing :
if(true)
{
{
This should be deleted because it just adds more code , it will be executed every time so no need to add it.
Besides the colon the end of the if statement you also should keep in mind that if you use
if(true){
}else{
}
The else statement will never execute cos the if will always be true, so you should be using the yes/no variables as flags for your if statement instead of the "true" itself.
If your statements inside the if should always be executed then you don't need the conditions at all.

How to assign a random number a string value in my program

as the title suggests I am doing a program for homework that is a slot machine. I have searched around and I am pretty satisfied that the program works correctly enough for me. The problem Im having is on top of generating the random numbers, I am supposed to assign values for the numbers 1-5 (Cherries, Oranges, Plums, Bells, Melons, Bars). Then I am to display the output instead of the number when my program runs. Can anyone get me pointed in the right direction on how to do this please?
import java.util.Random;
import java.util.Scanner;
public class SlotMachineClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int Coins = 1000;
int Wager = 0;
System.out.println("Steve's Slot Machine");
System.out.println("You have " + Coins + " coins.");
System.out.println("Enter your bet and press Enter to play");
while (Coins > 0)
{
int first = new Random().nextInt(5)+1;
int second = new Random().nextInt(5)+1;
int third = new Random().nextInt(5)+1;
Wager = input.nextInt();
if(Wager > Coins)
Wager = Coins;
System.out.println(first + " " + second + " " + third);
if(first == second && second == third)
{ Coins = Coins + (Wager * 3);
System.out.println("You won " + (Wager * 3) + "!!!!" + " You now have " + Coins + " coins.");
System.out.println("Enter another bet or close program to exit");}
else if((first == second && first != third) || (first != second && first == third) || (first != second && second == third))
{ Coins = Coins + (Wager * 2);
System.out.println("You won " + (Wager * 2) + "!!!" + " You now have " + Coins + " coins.");
System.out.println("Enter another bet or close program to exit");}
else {Coins = Coins - Wager;
System.out.println("You Lost!" + "\nPlay Again? if so Enter your bet.");}
}
while (Wager == 0)
{
System.out.println("You ran out of coins. Thanks for playing.");
}
}
}
If you have an int and want to have some String associated with that, there are a couple of ways to do that.
The first one is to have an array of Strings and look them up.
public static String[] text = new String[] {"Cherry", "Bell", "Lemon", "Bar", "Seven"};
public String getNameForReel(int reelValue) {
return text[reelValue];
}
// And to call it...
System.out.println(getNameForReel(first)); //etc...
Or, you can do it in a switch statement (I don't prefer this, but you might):
public String getNameForReel(int reelValue) {
switch(reelValue) {
case 0: return "Cherry";
case 1: return "Bell";
case 2: return "Lemon";
case 3: return "Bar";
case 4: return "Seven";
}
}
You need a lookup table:
String[] text = new String[] {"Cherry", "Bell", "Lemon", "Bar", "Seven"};
Then you can just do
System.out.println(text[first] + " " + text[second] + " " + text[third]);
without creating more methods.
The non-array solution most likely to be used a by new programmer in an intro course would be a nested if-else:
String fruitToPrint = "";
if (num == 0)
fruitToPrint = "Cherries";
else if (num == 1)
fruitToPrint = "Oranges";
else if (num == 2)
fruitToPrint = "Plums";
else if (num == 3)
fruitToPrint = "Bells";
else if (num == 4)
fruitToPrint = "Melons";
else if (num == 5)
fruitToPrint = "Bars";
else
System.out.println("Couldn't assign fruit from num=" + num);
System.out.println("The corresponding fruit was " + fruitToPrint);
Create an array:
String[] s = {Cherries, Oranges, Plums, Bells, Melons, Bars};
Then you can print s[num-1] instead of num (where num is the random int). E.g. if your random int came out to be 2, print s[2-1] i.e. s[1] which will be Orange.
Here's an alternative solution to the question which I think follows best programming practices. This is probably even less allowed for your assignment than an array, and will be a dead giveaway that you got your answer on StackOverflow, but the problem would lend itself to using an enum type with an int->enum mapping:
enum Fruit {
Cherries(1),
Oranges(2),
Plums(3),
Melons(4),
Bars(5);
private static final Map<Integer, Fruit> lookupMap = new HashMap<Integer, Fruit>();
static {
for (Fruit fruit : Fruit.values()) {
lookupMap.put(fruit.getLookup());
}
}
static Fruit fromLookup(int lookup) {
return lookupMap.get(lookup);
}
private final int lookup;
private Fruit(int lookup) {
this.lookup = lookup;
}
int getLookup() {
return lookup;
}
}
void printEnumExample() {
int fruitToPrint = 4;
System.out.println(Fruit.fromLookup(fruitToPrint)); // <- This will print "Melons"
}

why does my break statement breaks out of the entire program?

The break; statement in my Exception clause stops the entire program if my have improper input to the JOptionPane, it would not execute what I have after the catch block, why is that?
public static void main(String[] args) {
// TODO Auto-generated method stub
Customer forrest = new Customer("Forrest Grump", 1,
"42 New Street, New York, New York");
Customer random = new Customer("Random Name", 2,
"44 New Street, New York, New York");
Customer customer[] = { null, forrest, random };
int whichOption = 1;
int id = 0;
char action = ' ';
char accSrc = ' ';
char accDest = ' ';
double amount = 0;
BankAccount src = null;
do {
try{
// process JOptionPane input information
String input = JOptionPane
.showInputDialog("Please enter your transaction information: ");
Scanner s = new Scanner(input);
id = Integer.parseInt(s.next());
action = Character.toUpperCase((s.next().charAt(0)));
if (action == 'T') {
amount = s.nextDouble();
accSrc = s.next().charAt(0);
accDest = s.next().charAt(0);
} else if (action == 'G' || action == 'I') {
accSrc = s.next().charAt(0);
} else {
// if D,W
amount = s.nextDouble();
accSrc = s.next().charAt(0);
}
} catch (Exception e){
System.out.println("Invalid input!");
break;
}
// taking action accordingly (T)ransfer, (D)eposit, (W)ithdraw, (I)nterest
if (action == 'T') {
(customer[id].getAccount(accSrc)).transfer(amount,
customer[id].getAccount(accDest));
} else if (action == 'G') {
System.out.println("The current balance on your " + accSrc
+ " account is "
+ customer[id].getAccount(accSrc).getBalance() + "\n");
} else if (action == 'D') {
(customer[id].getAccount(accSrc)).deposit(amount);
//You have successfully depositted $xx.xx
} else if (action == 'W') {
(customer[id].getAccount(accSrc)).withdraw(amount);
} else if (action == 'I') {
(customer[id].getAccount(accSrc)).computeInterest();
}
whichOption = JOptionPane
.showConfirmDialog(null , "Do you want to continue?");
System.out.println("The balance on " + customer[id].getName()
+ " auto account is " + customer[id].A.balance);
System.out.println("The balance on " + customer[id].getName()
+ " savings account is " + customer[id].S.balance);
System.out.println("The balance on " + customer[id].getName()
+ " checkings account is " + customer[id].C.balance);
System.out.println("The balance on " + customer[id].getName()
+ " loan account is " + customer[id].L.balance + "\n");
} while (whichOption == 0);
}
}
because using break you are jumping out of the loop (and that is the end of your program), if you wish to execute after catch block part, simply remove break;
See
document
I suspect you wish to continue, not break. See the difference here:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
continue tells the loops to skip all of the following statements in the loop body and return to the top of the loop body, recheck the condition, and then continue looping normally from there.
break tells the loop to end immediately, ignoring any further instructions in the loop body.
break escapes from the enclosing loop, which in your case means the end of main...
You're breaking out of the loop and there's nothing left after the loop in your main method. If you want to continue looping, replace the break; with continue;.

Categories