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+\" now\\t\");
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.
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();
}
}