Java while loop not closing when getting specific input - java

I have a decent sized while loop that is supposed end when the answer to the following prompt is answered 'N' for no. Adding Break; after the last line of code causes my program to spit out error codes after reaching this prompt and entering a character. The issue may have to do when formatting as I get errors stating things like " Enter 'Y' to add another laptop to your purchase or 'N' to exit: y
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
Here is the while loop:
while (Character.toUpperCase(cont) == 'Y') {
dateTime = Calendar.getInstance();//Obtains current year
System.out.printf("%nTOP LAPTOPS OF %tY"
+ "%n%n1. %-23s %7s $%,9.2f"
+ "%n2. %-23s %8s %,9.2f"
+ "%n3. %-23s %8s %,9.2f"
+ "%n4. %-23s %8s %,9.2f"
+ "%n5. %-23s %8s %,9.2f"
+ "%n%nEnter your choice: ",
dateTime,
"HP Envy 13", " ", 799.99,
"Asus Zen Book 13 UX33FA", " ", 849.99,
"Dell XPS 13", " ", 989.99,
"Alienware Area 51-m", " ", 1999.99,
"Razer Blade Stealth", " ", 1299.00); //Prompt 1
choice = input.nextInt(); //Input is assigned to choice.
if (choice == 5) {
laptop = "Razer Blade Stealth";
price = 1299.00;
} else {
if (choice == 4) {
laptop = "Alienware Area 51 -m";
price = 1999.99;
} else {
if (choice == 3) {
laptop = "Dell XPS 131";
price = 989.99;
} else {
if (choice == 2) {
laptop = "Asus zenbook 13 UX33FA";
price = 849.99;
} else {
if (choice == 1) {
laptop = "HP Envy 13";
price = 799.99;
} else {
System.out.printf("%nInvalid choice! Try again");
}
}
}
}
}
if (choice > 0) {
if (choice < 6) {
System.out.printf("Enter the quantity for %s: ", laptop);
qty = input.nextInt();
lineItem = qty * price;
subtotal = subtotal + tax;
if (trigger == 1) {
orderSummary += String.format("%n%,-9d %-30s %9s %,17.2f", qty, laptop, " ", lineItem);
trigger = 0;
}//END if for $ sign.
else {
orderSummary += String.format("%n%,-9d %-30s %9s %,17.2f", qty, laptop, " ", lineItem);
}
}
}
//Empty call to nextLine()
input.nextLine();
System.out.printf("%nEnter 'Y' to add another laptop to your purchase or 'N' to exit: ");
char y = input.nextLine().charAt(0);
}//END while

Try this, since you didn't show the while block in full,
char y = 'Y';
while(y == 'Y' || y == 'y')
{
// do whatever you want...
// ..................
System.out.println("Enter Y to add another laptop to your purchase or any other char to exit: ");
y = input.next().charAt(0);
}

Related

Trying to have program return to main menu after user answers yes to continue or have program end and display message of total if user answers no

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean start = true;
while(start)
System.out.printf("%70s %n", " ##### Zoos Australia ##### " + "\n");
System.out.printf("%57s %n", "Main Menu" + "\n");
System.out.printf("%72s %n", "Zoo has the following ticketing options:");
System.out.print("\n");
System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
System.out.printf("%60s %n", "3 = Senior (60+ yrs)");
System.out.println("\n");
String choose1 = "";
String choose2 = "";
String choose3 = "";
String selected = "";
int option = 0;
{
System.out.print("Please select an option: ");
option = input.nextInt();
if (option == 1) {
choose1 = "Child";
selected = choose1;
} else if (option == 2) {
choose2 = "Adult";
selected = choose2;
} else if (option == 3) {
choose3 = "Senior";
selected = choose3;
}
}
// done
System.out.println("\n");
int price = 0;
int tickets = 0;
System.out.print("Enter the number of tickets: ");
tickets = input.nextInt();
if (selected == choose1) {
price = 10;
} else if (selected == choose2) {
price = 20;
} else if (selected == choose3) {
price = 15;
}
System.out.println("\n");
System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");
System.out.println("\n");
int confirm = 0;
System.out.print("Press 1 to confirm purchase: ");
confirm = input.nextInt();
if (confirm != 1) {
System.out.print("Incorrect Key. Please return to Main Menu");
System.out.println("\n");
} else {
break;
}
System.out.println("\n");
int total = tickets;
price = total * price;
System.out.print("Total amount for " + selected + " tickets: " + "$" + price);
System.out.println("\n");
String pick = "";
System.out.print("Do you wish to continue: ");
input.next();
System.out.println("\n");
if (pick == "no") {
System.out.print("Total amount payable is: " + "$" + price);
System.out.println("\n");
System.out.print("Have a nice day!");
System.out.println("\n");
}}}
Trying to do this at the end of the program where user is asked "Do you wish to continue" using a method or something but cant get it to work. Either the program returns to main menu only or the program ends and displays the total message "Total amount payable..." etc. I have tried using while with continue and break. Using boolean with true and false. But no luck. Thank you anyone that may be able to clear this up for me please.
First, you have to assign the users's input to a variable: pick = input.next(). After that, the problem is that you compare the user's input string with a "no" string by using == operator. When comparing reference types (objects) (and String is an object), in most cases == operator gives you an unpredictable result, because it compares the reference (address of an object in memory) and not the actual content. Please remember, that you always have to use the .equals() method instead. You also have to break from your loop, when the user's input is "no".
There is plenty of material concerning this issue. You can check, for instance, this one How do I compare strings in Java?
P.S. I quickly looked at the rest of your code and put some additional comments, which might help you to improve it. Good luck with learning Java!
Scanner input = new Scanner(System.in);
// boolean start = true; you don't need this line
while(true) { // 'true' condition makes it an infinite loop until you use break
// You also have to surround your while loop with curly braces,
// otherwise you fall into an infinite loop
System.out.printf("%70s %n", " ##### Zoos Australia ##### \n");
System.out.printf("%57s %n", "Main Menu\n");
System.out.printf("%72s %n", "Zoo has the following ticketing options: \n");
System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
System.out.printf("%60s %n", "3 = Senior (60+ yrs)\n");
String choose1 = "";
String choose2 = "";
String choose3 = "";
String selected = "";
int option = 0;
System.out.print("Please select an option: ");
option = input.nextInt();
if (option == 1) {
choose1 = "Child";
selected = choose1;
} else if (option == 2) {
choose2 = "Adult";
selected = choose2;
} else if (option == 3) {
choose3 = "Senior";
selected = choose3;
}
System.out.println(); // "\n" is a redundant argument
int price = 0;
int tickets = 0;
System.out.print("Enter the number of tickets: ");
tickets = input.nextInt();
if (selected.equals(choose1)) { // you should never compare strings with == operator! Always use .equals() instead
price = 10;
} else if (selected.equals(choose2)) {
price = 20;
} else if (selected.equals(choose3)) {
price = 15;
}
System.out.println();
System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");
System.out.println();
int confirm = 0;
System.out.print("Press 1 to confirm purchase: ");
confirm = input.nextInt();
if (confirm != 1) {
System.out.print("Incorrect Key. Please return to Main Menu");
System.out.println("\n");
} else {
//break; you cannot use 'break' in the if statement! You have to figure out another way, how to handle an invalid input
}
System.out.println();
int total = tickets;
price = total * price;
System.out.print("Total amount for " + selected + " tickets: " + "$" + price);
System.out.println();
String pick = "";
System.out.print("Do you wish to continue: ");
pick = input.next(); // you have to assign the input to a variable
System.out.println();
if (pick.equals("no")) { // You have to ALWAYS use .equals() when comparing Strings or any other reference types! == works correctly only with primitive types
System.out.print("Total amount payable is: " + "$" + price);
System.out.println();
System.out.print("Have a nice day!");
System.out.println();
break; // you need to break from the loop in the end
}
}
}

variable from if statement doesn't correctly count up

I made a quiz program in java. Everything works, but the counter at the end that counts the amount of questions that have been answered correctly doesn't correctly count up, and always says "3 out of 3" even if all questions are answered wrong. What did I do wrong?
import java.util.Scanner;
class quiz {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int q1, q2, q3, result;
boolean onepoint, twopoint, threepoint;
result = 0;
onepoint = false;
twopoint = false;
threepoint = false;
System.out.println("Interactive quiz");
System.out.println("");
System.out.println("Q1) Which country is new york located in?");
System.out.println(" 1) Canada");
System.out.println(" 2) United States");
System.out.println(" 3) China");
System.out.println(" 4) Russia");
System.out.println("");
System.out.print("> ");
q1 = input.nextInt();
if (q1 == 2) {
System.out.println("Your answer is correct.");
onepoint = true;
}
else if (q1 != 2) {
System.out.println("Your answer is incorrect.");
threepoint = false;
}
System.out.println("");
System.out.println("Q2) Which of these animals are not warm blooded?");
System.out.println(" 1) Bear");
System.out.println(" 2) Crow");
System.out.println(" 3) Elephant");
System.out.println(" 4) Frog");
System.out.println("");
System.out.print("> ");
q2 = input.nextInt();
if (q2 == 4) {
System.out.println("Your answer is correct.");
twopoint = true;
}
else if (q2 != 4) {
System.out.println("Your answer is incorrect.");
threepoint = false;
}
System.out.println("");
System.out.println("Q3) Which of these plants are carnivorous?");
System.out.println(" 1) Dandelion");
System.out.println(" 2) rafflesia");
System.out.println(" 3) cape sundew");
System.out.println(" 4) titan arum");
System.out.println("");
System.out.print("> ");
q3 = input.nextInt();
if (q3 == 3) {
System.out.println("Your answer is correct.");
threepoint = true;
}
else if (q3 != 3) {
System.out.println("Your answer is incorrect.");
threepoint = false;
}
System.out.println("");
if (onepoint = true) {
result = result + 1;
}
if (twopoint = true) {
result = result + 1;
}
if (threepoint = true) {
result = result + 1;
}
System.out.println("Your final score is " + result + " out of 3.");
}
}
I have a feeling that it probably has to do with my if statements near the end. I tried playing around with them, but still couldn't get it to work.
These are all assignments, point = true. Use the equality operator or the boolean as the condition
if (onepoint == true) {
result = result + 1;
}
or
if (onepoint) {
result = result + 1;
}
If you are using the booleans as point trackers you could consider adding the point values immediately after judging the answer. ex:
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int result = 0;
System.out.println("Interactive quiz\n");
System.out.println("Q1) Which country is new york located in?");
System.out.println(" 1) Canada");
System.out.println(" 2) United States");
System.out.println(" 3) China");
System.out.println(" 4) Russia");
System.out.println("");
System.out.print("> ");
if(input.nextInt() == 2){
System.out.println("Your answer is correct.");
result++
}else
System.out.println("Your answer is incorrect.");
System.out.println("");
System.out.println("Q2) Which of these animals are not warm blooded?");
System.out.println(" 1) Bear");
System.out.println(" 2) Crow");
System.out.println(" 3) Elephant");
System.out.println(" 4) Frog");
System.out.println("");
System.out.print("> ");
if (input.nextInt() == 4) {
System.out.println("Your answer is correct.");
result++;
}else
System.out.println("Your answer is incorrect.");
System.out.println("");
System.out.println("Q3) Which of these plants are carnivorous?");
System.out.println(" 1) Dandelion");
System.out.println(" 2) rafflesia");
System.out.println(" 3) cape sundew");
System.out.println(" 4) titan arum");
System.out.println("");
System.out.print("> ");
if (input.nextInt() == 3) {
System.out.println("Your answer is correct.");
result++;
}else
System.out.println("Your answer is incorrect.");
System.out.println("");
System.out.println("Your final score is " + result + " out of 3.");
}

How do I make it so when I enter itemType it prints out the an actual type not just an Int

package Homework;
import java.util.Scanner;
public class Homework2LibraryFines {
public static void main(String[] args) {
// Variables
int cardNumber;
int age;
int overdue;
int itemType;
double overdueFine;
int fineAdjustment;
int books = 1;
int magazines = 2;
int dvd = 3;
Scanner input = new Scanner(System.in);
System.out.println("Enter patron's library card number");
cardNumber = input.nextInt();
System.out.println("Enter patron's age");
age = input.nextInt();
System.out.println("Enter item type 1.Book, 2.Magazine, 3.DVD");
itemType = input.nextInt();
System.out.println("Enter number of days overdue");
overdue = input.nextInt();
System.out.println("Enter overdue fine");
overdueFine = input.nextDouble();
System.out.println("Enter fine adjustment");
fineAdjustment = input.nextInt();
if (overdueFine == books) {
overdueFine += 0.50;
} else if (overdueFine == magazines) {
overdueFine += 0.25;
} else if (overdueFine == dvd) {
overdueFine += 1.50;
} else if (age > 70) {
overdueFine += 0;
} else if (age >= 6 && age <= 17) {
overdueFine += 1.00;
}
System.out.println(cardNumber + " is " + age + " years old " + "and has an overdue " + itemType + "." + "The "
+ itemType + " is " + overdue + " days overdue");
}
}
OUTPUT
Enter patron's library card number
222222
Enter patron's age
30
Enter item type 1.Book, 2.Magazine, 3.DVD
3
Enter number of days overdue
10
Enter overdue fine
2.00
Enter fine adjustment
3
222222 is 30 years old and has an overdue 3.The 3 is 10 days overdue
Not that beautiful but it works:
string itemName = "";
if (itemType == 1) itemName = "Book";
else if (itemType == 2) itemName = "Magazine";
else if (itemType == 3) itemName = "DVD";
And when printing it out just replace itemType by itemName.
You could add a switch statement to get the actual String value depending on the number entered
String item="Book";//making Book the default option
switch (itemType) {
case 1:
item = "Book";
break;
case 2:
item = "Magazine";
break;
case 3:
item = "DVD";
break;
}
And print item in the print statement.
Or even a somewhat ugly ternary condition
String item = itemType == 1 ? "Book" : itemType == 2 ? "Magazine" : itemType == 3 ? "DVD" : "Book";
Besides the if and switch solutions already suggested, you can create an enum. These are a special kind of class with limited values. You can override the toString() method to print out whatever you wish.
You could declare and array of itemTypes, and while printing, use the user input as an offset in the array.
For storing,
String itemTypesArr[] = {"", "Book", "Magazine", "DVD"};
Now, itemTypesArr[1] refers to "Book" and so on for others.
For printing,
System.out.println(cardNumber + " is " + age + " years old " + "and has an overdue " + itemTypesArr[itemType] + "." + "The " + itemTypesArr[itemType] + " is " + overdue + " days overdue");
One option is to make a switch statement.
The itemType variable stores the option and a second variable store the string
String itemT = "";
switch(itemType)
{
case 1:
itemT = "book"
break;
case2:
...
}

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();

Need to run Java code in GWT

I created a new google web application project (using eclipse) and noticed the code runs in the .server package. I have a slot machine game i wrote in java and am trying to implement it into GWT, but the syntax seems to be totally different. (for example, i noticed \n doesn't work)
Here is the code i want to implement - how the heck would i do that?
// clear console
static void clearConsole()
{
for (int i=0; i<25; i++)
{
System.out.println("\n");
}
}
// actual slot machine algorithm
static void slots()
{
Scanner input = new Scanner (System.in);
char newGame = ' ';
int chips = 100;
int bet = 0;
do{
System.out.println( "Try your luck on the SLOT MACHINE." +
"\nLine up (=^^=) or ))<>(( symbols to win big!\n\n" +
"You currently have " + chips + " chips.\nHow much do you want to bet? ");
//check for accidental char input
try{
bet = input.nextInt();
}
catch(Exception e){
input.nextLine();
System.out.println("NOT A VALID NUMBER\nHow much do you want to bet? ");
bet = input.nextInt();
}
if (bet<=chips && bet>0){
// to add some realism, slot machine will not execute until 'enter' pressed
// then console cleared
input.nextLine();
System.out.println( "\nPress 'enter' to start the slot machine.");
input.nextLine();
clearConsole();
String[] machine = {"(=^^=)", "(=^^=)", "))<>((", " XXXX ", " XXXX ", " :^{) ",
" :^{) ", " (>_<)", " (>_<)", " [-O-]", " [-O-]"};
Random rand = new Random();
int slot1 = rand.nextInt(11);
int slot2 = rand.nextInt(11);
int slot3 = rand.nextInt(11);
int slot4 = rand.nextInt(11);
int slot5 = rand.nextInt(11);
int slot6 = rand.nextInt(11);
int slot7 = rand.nextInt(11);
int slot8 = rand.nextInt(11);
int slot9 = rand.nextInt(11);
System.out.println( "-----------------------");
System.out.printf( "%-7s %-7s %-7s %n%n", machine[slot1], machine[slot2], machine[slot3]);
System.out.printf( "%-7s %-7s %-7s %n%n", machine[slot4], machine[slot5], machine[slot6]);
System.out.printf( "%-7s %-7s %-7s %n", machine[slot7], machine[slot8], machine[slot9]);
System.out.println( "-----------------------\n\n\n\n");
// 3 wild cards
if (slot4 == 2 && slot5 == 2 && slot6 == 2 ){
bet = bet*100;
chips = chips + bet;
System.out.println( "JACKPOT! ");
}
//3 cats (inclusive of wild card)
else if ( slot4 <3 && slot5 <3 && slot6 <3 ){
bet = bet*5;
chips = chips + bet;
System.out.println( "YOU WIN! ");
}
// 3 of any other symbol (inclusive of wild card)
else if( ((slot4==2 || slot4==3 || slot4==4) && (slot5==2 || slot5==3 ||
slot5==4) && (slot6==2 || slot6==3 || slot6==4))
|| ((slot4==2 || slot4==5 || slot4==6) && (slot5==2 || slot5==5 ||
slot5==6) && (slot6==2 || slot6==5 || slot6==6))
|| ((slot4==2 || slot4==7 || slot4==8) && (slot5==2 || slot5==7 ||
slot5==8) && (slot6==2 || slot6==7 || slot6==8))
|| ((slot4==2 || slot4==9 || slot4==10) && (slot5==2 || slot5==9 ||
slot5==10) && (slot6==2 || slot6==9 || slot6==10)) ){
bet = bet*3;
chips = chips + bet;
System.out.println( "YOU WIN! ");
}
// 2 cats
else if ( slot4<2 && slot5<2 || slot4<2 && slot6<2 || slot5<2 && slot6<2){
bet = bet*2;
chips = chips + bet;
System.out.println( "YOU WIN! ");
}
// 1 cat
else if ( slot4 < 2 || slot5 < 2 || slot6 < 2 ){
chips = chips + bet;
System.out.println( "YOU WIN! ");
}
// nothing
else{
chips = chips - bet;
System.out.print( "You Lose... ");
}
// display current amount of chips
System.out.println( "You have " + chips + " chips.");
}else{
System.out.println( "You do not have enough chips to make that bet!");
}
if (chips > 0){
System.out.println( "\nDo you wanna play this game again? (y/n): ");
newGame = input.next().charAt(0);
clearConsole();
}
} while (newGame =='y' && chips > 0 );
input.close();
System.out.println( "\nGame Over\n\n");
}
You could replace System.out.println with putting raw text (not even HTML) into an element with fixed space etc (something like the "pre" element).
I'd suggest you switch to something like Button for "enter", "y"/"n"... and and DoubleBox for the amount they want to bet.
There may be a more general "console" in browser approach but it would be a bit odd?
Maybe best starting with one of the GWT samples and slowly converting to what you want? Quite a bit of bootstrapping to get a GWT app up and running.

Categories