SlideShare a Scribd company logo
1 of 7
Download to read offline
Please observe the the code and validations of user inputs.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Random;
public class SlotMachineSimulation
{
public static void main(String[]args) throws FileNotFoundException
{
//Open File
File file = new File("input.txt");
int choice;
//Open File to write on
PrintWriter outputFile = new PrintWriter("output.txt");
// Create a Scanner object for keyboard input.
Scanner keyboard=new Scanner(System.in);
// Create a Scanner object for input File data.
Scanner inputFile = new Scanner(file);
Scanner readFile=new Scanner(file);
//Declaring Variables
int fr1,fr2,fr3;
String fruit;
int num;
int select;
int amountBet;
int won,lost = 0;
String decision;
int total=0;
System.out.println("Welcome to Guanyu Tian's Slot Machine!");
int n = inputFile.nextInt();
do{
System.out.println("You inserted " + n + " into the slot machine!" );
System.out.println("You currently have " + n + ", you can bet the multiple of $10.");
System.out.println("If you enter 1, you will bet $10 If you enter 2, you will bet $20, etc.");
do{
//ask user to insert the bet amount
System.out.println("Please enter how much do you want to bet this time: ");
num=keyboard.nextInt();
if(num<1 || num>(n/10))
{
amountBet=num*10;
System.out.printf("Sorry, you don't have $"+amountBet+" nowt");
System.out.println("You can only bet less than the amount of money you have.");
System.out.println("You currently have $"+n+", You can the multiple of $10.");
System.out.println("If you enter 1, you will bet $10 If you enter 2, you will bet $20, etc.");
//System.exit(0);
}
}while(1>num || (n/10)6 ){
System.out.println("Invalid selection. You must enter a number between 1 and 6 inclusive");
//System.exit(0);
}
} while(select<1 || select >6);
//Generating a random fruit
Random randNum=new Random();
fr1 = randNum.nextInt(6)+1;
fr2 = randNum.nextInt(6)+1;
fr3 = randNum.nextInt(6)+1;
if(select == fr1&&select==fr2&&select==fr3){
System.out.println(fr1+" "+fr2+" "+fr3);
won = amountBet *3;
total=n+won;
System.out.println("You have won 3 times bet amount."+won);
System.out.println("Your current balance."+total);
outputFile.println("You have won 3 times bet amount."+won);
outputFile.println(n);
}
else if(select==fr1&&select==fr2||select==fr2&&select==fr3||select==fr3&&select==fr1){
System.out.println(fr1+" "+fr2+" "+fr3);
won= amountBet *2;
total=n+won;
System.out.println("You have won 2 times bet amount."+won);
System.out.println("Your current balance."+total);
outputFile.println("You have won 2 times bet amount."+won);
outputFile.println(n);
}
else if(select == fr1||select == fr2||select == fr3){
System.out.println(fr1+" "+fr2+" "+fr3);
won= amountBet ;
total=n+won;
System.out.println("You have won 1 times bet amount."+won);
System.out.println("Your current balance."+total);
outputFile.println("You have won 1 times bet amount."+won);
outputFile.println(n);
}
else{
System.out.println(fr1+" "+fr2+" "+fr3);
won=0;
lost=amountBet;
total=n+won-lost;
System.out.println("None of the randomly selected words matched your selection. You won
$"+won);
System.out.println("Your current balance."+total);
outputFile.println("You Lost this iteration."+lost);
outputFile.println(lost);
}
if(total==0){ //warning the user about
his nill balance
System.err.println("You have 0 balance in your account. Please refill to continue the game");
break;
}
System.out.println("Do you want to continue playing (y/Y) or (n/N): ");
do{ //validations for user decision input
decision=keyboard.next();
if(!decision.equalsIgnoreCase("y")&&!decision.equalsIgnoreCase("n")){
System.err.println("Enter correct input (y/Y) or (n/N)");
}
}while(!decision.equalsIgnoreCase("y")&&!decision.equalsIgnoreCase("n"));
if(decision.equalsIgnoreCase("n")){
System.out.println("You currently have $"+total+". You have lost"+lost);
System.out.println("Thank you for choose Guanyu Tian's Slot Machine! Good bye...");
System.exit(0);
}
else if(decision.equalsIgnoreCase("y")){
n=total;
}
}while(decision.equalsIgnoreCase("y"));
keyboard.close();
inputFile.close();
outputFile.close();
readFile.close();
}
}
Solution
Please observe the the code and validations of user inputs.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Random;
public class SlotMachineSimulation
{
public static void main(String[]args) throws FileNotFoundException
{
//Open File
File file = new File("input.txt");
int choice;
//Open File to write on
PrintWriter outputFile = new PrintWriter("output.txt");
// Create a Scanner object for keyboard input.
Scanner keyboard=new Scanner(System.in);
// Create a Scanner object for input File data.
Scanner inputFile = new Scanner(file);
Scanner readFile=new Scanner(file);
//Declaring Variables
int fr1,fr2,fr3;
String fruit;
int num;
int select;
int amountBet;
int won,lost = 0;
String decision;
int total=0;
System.out.println("Welcome to Guanyu Tian's Slot Machine!");
int n = inputFile.nextInt();
do{
System.out.println("You inserted " + n + " into the slot machine!" );
System.out.println("You currently have " + n + ", you can bet the multiple of $10.");
System.out.println("If you enter 1, you will bet $10 If you enter 2, you will bet $20, etc.");
do{
//ask user to insert the bet amount
System.out.println("Please enter how much do you want to bet this time: ");
num=keyboard.nextInt();
if(num<1 || num>(n/10))
{
amountBet=num*10;
System.out.printf("Sorry, you don't have $"+amountBet+" nowt");
System.out.println("You can only bet less than the amount of money you have.");
System.out.println("You currently have $"+n+", You can the multiple of $10.");
System.out.println("If you enter 1, you will bet $10 If you enter 2, you will bet $20, etc.");
//System.exit(0);
}
}while(1>num || (n/10)6 ){
System.out.println("Invalid selection. You must enter a number between 1 and 6 inclusive");
//System.exit(0);
}
} while(select<1 || select >6);
//Generating a random fruit
Random randNum=new Random();
fr1 = randNum.nextInt(6)+1;
fr2 = randNum.nextInt(6)+1;
fr3 = randNum.nextInt(6)+1;
if(select == fr1&&select==fr2&&select==fr3){
System.out.println(fr1+" "+fr2+" "+fr3);
won = amountBet *3;
total=n+won;
System.out.println("You have won 3 times bet amount."+won);
System.out.println("Your current balance."+total);
outputFile.println("You have won 3 times bet amount."+won);
outputFile.println(n);
}
else if(select==fr1&&select==fr2||select==fr2&&select==fr3||select==fr3&&select==fr1){
System.out.println(fr1+" "+fr2+" "+fr3);
won= amountBet *2;
total=n+won;
System.out.println("You have won 2 times bet amount."+won);
System.out.println("Your current balance."+total);
outputFile.println("You have won 2 times bet amount."+won);
outputFile.println(n);
}
else if(select == fr1||select == fr2||select == fr3){
System.out.println(fr1+" "+fr2+" "+fr3);
won= amountBet ;
total=n+won;
System.out.println("You have won 1 times bet amount."+won);
System.out.println("Your current balance."+total);
outputFile.println("You have won 1 times bet amount."+won);
outputFile.println(n);
}
else{
System.out.println(fr1+" "+fr2+" "+fr3);
won=0;
lost=amountBet;
total=n+won-lost;
System.out.println("None of the randomly selected words matched your selection. You won
$"+won);
System.out.println("Your current balance."+total);
outputFile.println("You Lost this iteration."+lost);
outputFile.println(lost);
}
if(total==0){ //warning the user about
his nill balance
System.err.println("You have 0 balance in your account. Please refill to continue the game");
break;
}
System.out.println("Do you want to continue playing (y/Y) or (n/N): ");
do{ //validations for user decision input
decision=keyboard.next();
if(!decision.equalsIgnoreCase("y")&&!decision.equalsIgnoreCase("n")){
System.err.println("Enter correct input (y/Y) or (n/N)");
}
}while(!decision.equalsIgnoreCase("y")&&!decision.equalsIgnoreCase("n"));
if(decision.equalsIgnoreCase("n")){
System.out.println("You currently have $"+total+". You have lost"+lost);
System.out.println("Thank you for choose Guanyu Tian's Slot Machine! Good bye...");
System.exit(0);
}
else if(decision.equalsIgnoreCase("y")){
n=total;
}
}while(decision.equalsIgnoreCase("y"));
keyboard.close();
inputFile.close();
outputFile.close();
readFile.close();
}
}

More Related Content

Similar to Please observe the the code and validations of user inputs.import .pdf

Example of JAVA Program
Example of JAVA ProgramExample of JAVA Program
Example of JAVA Program
Trenton Asbury
 
TASK #1In the domain class you will create a loop that will prompt.pdf
TASK #1In the domain class you will create a loop that will prompt.pdfTASK #1In the domain class you will create a loop that will prompt.pdf
TASK #1In the domain class you will create a loop that will prompt.pdf
indiaartz
 
Construct a java method.   This method chooses the number of sti.pdf
Construct a java method.    This method chooses the number of sti.pdfConstruct a java method.    This method chooses the number of sti.pdf
Construct a java method.   This method chooses the number of sti.pdf
arihantmobilepoint15
 
import java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdfimport java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdf
stopgolook
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
anjandavid
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
optokunal1
 

Similar to Please observe the the code and validations of user inputs.import .pdf (8)

Example of JAVA Program
Example of JAVA ProgramExample of JAVA Program
Example of JAVA Program
 
RandomGuessingGame
RandomGuessingGameRandomGuessingGame
RandomGuessingGame
 
TASK #1In the domain class you will create a loop that will prompt.pdf
TASK #1In the domain class you will create a loop that will prompt.pdfTASK #1In the domain class you will create a loop that will prompt.pdf
TASK #1In the domain class you will create a loop that will prompt.pdf
 
Construct a java method.   This method chooses the number of sti.pdf
Construct a java method.    This method chooses the number of sti.pdfConstruct a java method.    This method chooses the number of sti.pdf
Construct a java method.   This method chooses the number of sti.pdf
 
import java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdfimport java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdf
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 

More from apexjaipur

The active transport of sodium chloride by the ascending loop of Hen.pdf
The active transport of sodium chloride by the ascending loop of Hen.pdfThe active transport of sodium chloride by the ascending loop of Hen.pdf
The active transport of sodium chloride by the ascending loop of Hen.pdf
apexjaipur
 
import java.awt.; victimisation AWTs Graphics and Color impo.pdf
import java.awt.;  victimisation AWTs Graphics and Color impo.pdfimport java.awt.;  victimisation AWTs Graphics and Color impo.pdf
import java.awt.; victimisation AWTs Graphics and Color impo.pdf
apexjaipur
 
Federated identity management is mainly used by security leaders. It.pdf
Federated identity management is mainly used by security leaders. It.pdfFederated identity management is mainly used by security leaders. It.pdf
Federated identity management is mainly used by security leaders. It.pdf
apexjaipur
 

More from apexjaipur (20)

[PDF]Solution [PDF].pdf
 [PDF]Solution [PDF].pdf [PDF]Solution [PDF].pdf
[PDF]Solution [PDF].pdf
 
The key here is that the relative rates of appearancedisappearance .pdf
  The key here is that the relative rates of appearancedisappearance .pdf  The key here is that the relative rates of appearancedisappearance .pdf
The key here is that the relative rates of appearancedisappearance .pdf
 
the molar ratio of C with O = 11, However, the a.pdf
                     the molar ratio of C with O = 11, However, the a.pdf                     the molar ratio of C with O = 11, However, the a.pdf
the molar ratio of C with O = 11, However, the a.pdf
 
pOH = -log[OH-] = -log(0.061) = 1.21 pH = 14 - pO.pdf
                     pOH = -log[OH-] = -log(0.061) = 1.21 pH = 14 - pO.pdf                     pOH = -log[OH-] = -log(0.061) = 1.21 pH = 14 - pO.pdf
pOH = -log[OH-] = -log(0.061) = 1.21 pH = 14 - pO.pdf
 
Ne and F2 are both uncharged molecules so there c.pdf
                     Ne and F2 are both uncharged molecules so there c.pdf                     Ne and F2 are both uncharged molecules so there c.pdf
Ne and F2 are both uncharged molecules so there c.pdf
 
x = 10 + (1p^2) Elasticity , E = (dxdp)(px) .pdf
                     x = 10 + (1p^2)  Elasticity , E = (dxdp)(px) .pdf                     x = 10 + (1p^2)  Elasticity , E = (dxdp)(px) .pdf
x = 10 + (1p^2) Elasticity , E = (dxdp)(px) .pdf
 
The second phenol can resonance stabilize once th.pdf
                     The second phenol can resonance stabilize once th.pdf                     The second phenol can resonance stabilize once th.pdf
The second phenol can resonance stabilize once th.pdf
 
Value of the stock = Dividend Required returnValue of the stock .pdf
Value of the stock = Dividend  Required returnValue of the stock .pdfValue of the stock = Dividend  Required returnValue of the stock .pdf
Value of the stock = Dividend Required returnValue of the stock .pdf
 
The phenotypes of parental arePPSS - purple smoothppss - yellow.pdf
The phenotypes of parental arePPSS - purple smoothppss - yellow.pdfThe phenotypes of parental arePPSS - purple smoothppss - yellow.pdf
The phenotypes of parental arePPSS - purple smoothppss - yellow.pdf
 
H3PO3 Phosphorous acid H3AsO4 Arsenic acid HC.pdf
                     H3PO3 Phosphorous acid  H3AsO4 Arsenic acid  HC.pdf                     H3PO3 Phosphorous acid  H3AsO4 Arsenic acid  HC.pdf
H3PO3 Phosphorous acid H3AsO4 Arsenic acid HC.pdf
 
The active transport of sodium chloride by the ascending loop of Hen.pdf
The active transport of sodium chloride by the ascending loop of Hen.pdfThe active transport of sodium chloride by the ascending loop of Hen.pdf
The active transport of sodium chloride by the ascending loop of Hen.pdf
 
dy = -2.4 .pdf
                     dy = -2.4                                      .pdf                     dy = -2.4                                      .pdf
dy = -2.4 .pdf
 
ENTROPY INCEREASES for liquid to gas solid to g.pdf
                     ENTROPY INCEREASES for  liquid to gas  solid to g.pdf                     ENTROPY INCEREASES for  liquid to gas  solid to g.pdf
ENTROPY INCEREASES for liquid to gas solid to g.pdf
 
Solution - Debt to assets ratio = 0.60Weight of debt in capital s.pdf
Solution - Debt to assets ratio = 0.60Weight of debt in capital s.pdfSolution - Debt to assets ratio = 0.60Weight of debt in capital s.pdf
Solution - Debt to assets ratio = 0.60Weight of debt in capital s.pdf
 
d) More hydrogen bonds will form with water due t.pdf
                     d) More hydrogen bonds will form with water due t.pdf                     d) More hydrogen bonds will form with water due t.pdf
d) More hydrogen bonds will form with water due t.pdf
 
Plant is a dicot showing dichotomous branching.Growth form The p.pdf
Plant is a dicot showing dichotomous branching.Growth form  The p.pdfPlant is a dicot showing dichotomous branching.Growth form  The p.pdf
Plant is a dicot showing dichotomous branching.Growth form The p.pdf
 
Non-accountant means financial statements in order to understand the.pdf
Non-accountant means financial statements in order to understand the.pdfNon-accountant means financial statements in order to understand the.pdf
Non-accountant means financial statements in order to understand the.pdf
 
Cerium exhibits three oxidation states, +2, +3 an.pdf
                     Cerium exhibits three oxidation states, +2, +3 an.pdf                     Cerium exhibits three oxidation states, +2, +3 an.pdf
Cerium exhibits three oxidation states, +2, +3 an.pdf
 
import java.awt.; victimisation AWTs Graphics and Color impo.pdf
import java.awt.;  victimisation AWTs Graphics and Color impo.pdfimport java.awt.;  victimisation AWTs Graphics and Color impo.pdf
import java.awt.; victimisation AWTs Graphics and Color impo.pdf
 
Federated identity management is mainly used by security leaders. It.pdf
Federated identity management is mainly used by security leaders. It.pdfFederated identity management is mainly used by security leaders. It.pdf
Federated identity management is mainly used by security leaders. It.pdf
 

Recently uploaded

Recently uploaded (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 

Please observe the the code and validations of user inputs.import .pdf

  • 1. Please observe the the code and validations of user inputs. import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import java.util.Random; public class SlotMachineSimulation { public static void main(String[]args) throws FileNotFoundException { //Open File File file = new File("input.txt"); int choice; //Open File to write on PrintWriter outputFile = new PrintWriter("output.txt"); // Create a Scanner object for keyboard input. Scanner keyboard=new Scanner(System.in); // Create a Scanner object for input File data. Scanner inputFile = new Scanner(file); Scanner readFile=new Scanner(file); //Declaring Variables int fr1,fr2,fr3; String fruit; int num; int select; int amountBet; int won,lost = 0; String decision; int total=0; System.out.println("Welcome to Guanyu Tian's Slot Machine!"); int n = inputFile.nextInt(); do{ System.out.println("You inserted " + n + " into the slot machine!" ); System.out.println("You currently have " + n + ", you can bet the multiple of $10."); System.out.println("If you enter 1, you will bet $10 If you enter 2, you will bet $20, etc.");
  • 2. do{ //ask user to insert the bet amount System.out.println("Please enter how much do you want to bet this time: "); num=keyboard.nextInt(); if(num<1 || num>(n/10)) { amountBet=num*10; System.out.printf("Sorry, you don't have $"+amountBet+" nowt"); System.out.println("You can only bet less than the amount of money you have."); System.out.println("You currently have $"+n+", You can the multiple of $10."); System.out.println("If you enter 1, you will bet $10 If you enter 2, you will bet $20, etc."); //System.exit(0); } }while(1>num || (n/10)6 ){ System.out.println("Invalid selection. You must enter a number between 1 and 6 inclusive"); //System.exit(0); } } while(select<1 || select >6); //Generating a random fruit Random randNum=new Random(); fr1 = randNum.nextInt(6)+1; fr2 = randNum.nextInt(6)+1; fr3 = randNum.nextInt(6)+1; if(select == fr1&&select==fr2&&select==fr3){ System.out.println(fr1+" "+fr2+" "+fr3); won = amountBet *3; total=n+won; System.out.println("You have won 3 times bet amount."+won); System.out.println("Your current balance."+total); outputFile.println("You have won 3 times bet amount."+won); outputFile.println(n); } else if(select==fr1&&select==fr2||select==fr2&&select==fr3||select==fr3&&select==fr1){ System.out.println(fr1+" "+fr2+" "+fr3); won= amountBet *2; total=n+won;
  • 3. System.out.println("You have won 2 times bet amount."+won); System.out.println("Your current balance."+total); outputFile.println("You have won 2 times bet amount."+won); outputFile.println(n); } else if(select == fr1||select == fr2||select == fr3){ System.out.println(fr1+" "+fr2+" "+fr3); won= amountBet ; total=n+won; System.out.println("You have won 1 times bet amount."+won); System.out.println("Your current balance."+total); outputFile.println("You have won 1 times bet amount."+won); outputFile.println(n); } else{ System.out.println(fr1+" "+fr2+" "+fr3); won=0; lost=amountBet; total=n+won-lost; System.out.println("None of the randomly selected words matched your selection. You won $"+won); System.out.println("Your current balance."+total); outputFile.println("You Lost this iteration."+lost); outputFile.println(lost); } if(total==0){ //warning the user about his nill balance System.err.println("You have 0 balance in your account. Please refill to continue the game"); break; } System.out.println("Do you want to continue playing (y/Y) or (n/N): "); do{ //validations for user decision input decision=keyboard.next(); if(!decision.equalsIgnoreCase("y")&&!decision.equalsIgnoreCase("n")){ System.err.println("Enter correct input (y/Y) or (n/N)"); }
  • 4. }while(!decision.equalsIgnoreCase("y")&&!decision.equalsIgnoreCase("n")); if(decision.equalsIgnoreCase("n")){ System.out.println("You currently have $"+total+". You have lost"+lost); System.out.println("Thank you for choose Guanyu Tian's Slot Machine! Good bye..."); System.exit(0); } else if(decision.equalsIgnoreCase("y")){ n=total; } }while(decision.equalsIgnoreCase("y")); keyboard.close(); inputFile.close(); outputFile.close(); readFile.close(); } } Solution Please observe the the code and validations of user inputs. import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import java.util.Random; public class SlotMachineSimulation { public static void main(String[]args) throws FileNotFoundException { //Open File File file = new File("input.txt"); int choice; //Open File to write on PrintWriter outputFile = new PrintWriter("output.txt"); // Create a Scanner object for keyboard input.
  • 5. Scanner keyboard=new Scanner(System.in); // Create a Scanner object for input File data. Scanner inputFile = new Scanner(file); Scanner readFile=new Scanner(file); //Declaring Variables int fr1,fr2,fr3; String fruit; int num; int select; int amountBet; int won,lost = 0; String decision; int total=0; System.out.println("Welcome to Guanyu Tian's Slot Machine!"); int n = inputFile.nextInt(); do{ System.out.println("You inserted " + n + " into the slot machine!" ); System.out.println("You currently have " + n + ", you can bet the multiple of $10."); System.out.println("If you enter 1, you will bet $10 If you enter 2, you will bet $20, etc."); do{ //ask user to insert the bet amount System.out.println("Please enter how much do you want to bet this time: "); num=keyboard.nextInt(); if(num<1 || num>(n/10)) { amountBet=num*10; System.out.printf("Sorry, you don't have $"+amountBet+" nowt"); System.out.println("You can only bet less than the amount of money you have."); System.out.println("You currently have $"+n+", You can the multiple of $10."); System.out.println("If you enter 1, you will bet $10 If you enter 2, you will bet $20, etc."); //System.exit(0); } }while(1>num || (n/10)6 ){ System.out.println("Invalid selection. You must enter a number between 1 and 6 inclusive"); //System.exit(0); }
  • 6. } while(select<1 || select >6); //Generating a random fruit Random randNum=new Random(); fr1 = randNum.nextInt(6)+1; fr2 = randNum.nextInt(6)+1; fr3 = randNum.nextInt(6)+1; if(select == fr1&&select==fr2&&select==fr3){ System.out.println(fr1+" "+fr2+" "+fr3); won = amountBet *3; total=n+won; System.out.println("You have won 3 times bet amount."+won); System.out.println("Your current balance."+total); outputFile.println("You have won 3 times bet amount."+won); outputFile.println(n); } else if(select==fr1&&select==fr2||select==fr2&&select==fr3||select==fr3&&select==fr1){ System.out.println(fr1+" "+fr2+" "+fr3); won= amountBet *2; total=n+won; System.out.println("You have won 2 times bet amount."+won); System.out.println("Your current balance."+total); outputFile.println("You have won 2 times bet amount."+won); outputFile.println(n); } else if(select == fr1||select == fr2||select == fr3){ System.out.println(fr1+" "+fr2+" "+fr3); won= amountBet ; total=n+won; System.out.println("You have won 1 times bet amount."+won); System.out.println("Your current balance."+total); outputFile.println("You have won 1 times bet amount."+won); outputFile.println(n); } else{ System.out.println(fr1+" "+fr2+" "+fr3); won=0;
  • 7. lost=amountBet; total=n+won-lost; System.out.println("None of the randomly selected words matched your selection. You won $"+won); System.out.println("Your current balance."+total); outputFile.println("You Lost this iteration."+lost); outputFile.println(lost); } if(total==0){ //warning the user about his nill balance System.err.println("You have 0 balance in your account. Please refill to continue the game"); break; } System.out.println("Do you want to continue playing (y/Y) or (n/N): "); do{ //validations for user decision input decision=keyboard.next(); if(!decision.equalsIgnoreCase("y")&&!decision.equalsIgnoreCase("n")){ System.err.println("Enter correct input (y/Y) or (n/N)"); } }while(!decision.equalsIgnoreCase("y")&&!decision.equalsIgnoreCase("n")); if(decision.equalsIgnoreCase("n")){ System.out.println("You currently have $"+total+". You have lost"+lost); System.out.println("Thank you for choose Guanyu Tian's Slot Machine! Good bye..."); System.exit(0); } else if(decision.equalsIgnoreCase("y")){ n=total; } }while(decision.equalsIgnoreCase("y")); keyboard.close(); inputFile.close(); outputFile.close(); readFile.close(); } }