Design and implement an application that simulates a simple

Design and implement an application that simulates a simple slot machine in which three numbers
between 0 and 9 are randomly selected and printed side by side. Print an appropriate statement
if all three of the numbers are the same, or if any two of the numbers are the same. Continue
playing until the users choose to stop.

And below is my code:

import java.util.Random;
import java.util.Scanner;
public class Coding_Practice {

   public static void main(String[] args) {
  
       Scanner scan=new Scanner(System.in);
       int x,y,z; String ans=\"y\";
       Random ram=new Random();
       System.out.println(\"Start a slot? (y/n) :\");
       ans=scan.nextLine();
       while(ans.equalsIgnoreCase(\"y\"))
       {
           x=ram.nextInt(10);
           y=ram.nextInt(10);
           z=ram.nextInt(10);
           System.out.println(\"The numbers are:\"+x+y+z);
           if(x==y&&y==z)
           {
               System.out.println(\"Jackpot\");
           }
              
           else if(x==y||y==z||x==z)
           {
               System.out.println(\"Just miss\");
           }
              
           else
               System.out.println(\"Sorry\");
           System.out.print(\"Play again? (y/n)\");
           ans=scan.nextLine();
           System.out.println();
       }
       System.out.println(\"Thank you!\");
}
}

Here is my question : Why do I need to add\" The numbers are:\" before x+y+z statement ? If I don\'t add that , just type x+y+z inside the println()

it won\'t display three numbers side by side

Solution

Why do I need to add\" The numbers are:\" before x+y+z statement ? If I don\'t add that , just type x+y+z inside the println()

The problem here is x, y, and z are integers, and if there is no string in the println() statement, all the integers will operate using the + operator, i.e., all the numbers will be summated, and the sum will be displayed. i.e., for example if the x = 3, y = 4, z = 5, then System.out.println(x+y+z); will print the result of the operation x+y+z = 3 + 4 + 5 = 12 will be printed.

Whereas, when you write a string before this x+y+z, now, System.out.println(\"Some string\" + x + y + z); In this case the + is being applied on a string and an integer, i.e., The integer will be appended at the end of the string, and that will continue for all the three integers. Note that here + can be overloaded based on different values. If you apply a + between 2 integers, the integers will be added and the result will be returned, whereas, if it applied between a string and an integer, then the integer will be appended to the string.

If you want the integers to be printed side-by-side without the string \"The numbers are: \", you can still achieve that by converting the first integer to a string by using the string method. Here is how you can do that:

System.out.println(Integer.toString(x)+y+z); Here the first integer x is converted to string, then the string + y will append y at the end of first string, and the result is a string. Finally the string + z will append z to the string. And the string will be printed.

Design and implement an application that simulates a simple slot machine in which three numbers between 0 and 9 are randomly selected and printed side by side.
Design and implement an application that simulates a simple slot machine in which three numbers between 0 and 9 are randomly selected and printed side by side.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site