Develop - Lucky7 Game
Closed this issue · 14 comments
- Using Random Number between 0-9
- Analyse Loop : which is better, for or while?
Hint:-
Random r = new Random();
sout(r.nextInt(10));
package luckyseven;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author m-lan
*/
public class LuckySeven {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {
int x, y, z, count = 0;
String key;
while (true) {
key = JOptionPane.showInputDialog("Enter G to generate (any key to exit)");
if (key.equalsIgnoreCase("G")) {
Random r = new Random();
x = r.nextInt(10);
if (x == 7) {
count++;
switch (count) {
case 1:
JOptionPane.showMessageDialog(null, "7\nYou are Lucky 7");
break;
case 2:JOptionPane.showMessageDialog(null, "77\nYou are Super Lucky 7"); break; case 3: JOptionPane.showMessageDialog(null, "777\nYou are Awesome Lucky 7"); break; default: break; } } else { count--; if (count <= 0) { count = 0; } JOptionPane.showMessageDialog(null, "Your Number is " + x + " Try Again..."); } } else { break; }
}
}
-
}
Code:
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package luckyseven;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author NiiRosh
*/
public class LuckySeven {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random r = new Random();
String ch = "y";
JOptionPane.showMessageDialog(null, "Welcome to Lucky 7 Game\nClick on ok to generate 7");
while (ch.equalsIgnoreCase("y")) {
int x = r.nextInt(10);if (x == 7) { JOptionPane.showMessageDialog(null, "Congratulation!!! You generated 7\nYou are lucky"); } else { JOptionPane.showMessageDialog(null, "Sorry you generated:" + x + "\nBetter luck next time"); } ch = JOptionPane.showInputDialog("Do you want to try your luck again??(y/n)");
}
}
-
}
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package lucky.pkg7;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author DELL
*/
public class Lucky7 {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int x, c = 0;
String check = "y";
Random r = new Random();JOptionPane.showMessageDialog(null, " LUCKY 7\n Welcome To The Game");
JOptionPane.showMessageDialog(null, "Its Time to get to know your luck, Click on Ok to continue");
while (check.equalsIgnoreCase("y")) {
x = r.nextInt(10);
JOptionPane.showMessageDialog(null, "You got number : " + x);
if (x == 7) {
c++;
if (c >=2) {
check = JOptionPane.showInputDialog("You are very lucky today\nWanna try again once more?\nYes : y\nNo : n");} check = JOptionPane.showInputDialog("Congratulation, you've got lucky number 7!\nYou have won the game\nAgain do you wish to try your luck?\nYes : y\nNo : n"); } else { check = JOptionPane.showInputDialog("Sorry Bad luck!!!.\n Would you like to try again?(y/n)"); }
}
}
-
}
}
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package luckyseven;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author Lenovo
*/
public class LuckySeven {/**
- @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String s = JOptionPane.showInputDialog("Do you wanna know if you are lucky? Enter y or n"), ch = "y";
while (ch.equalsIgnoreCase("y")) {
Random r = new Random();
int x;
x = r.nextInt(10);
if (x == 7) {
System.out.println("Wow!! you are lucky...you got 7");
} else {
System.out.println("Sorry the number generated is " + x);
}
ch = JOptionPane.showInputDialog("Do you wanna try again?\t\t (y/n)");
}
}
- @param args the command line arguments
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package day13.pkg3;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author nissus
*/
public class Day133 {/**
- @param args the command line arguments
*/
public static void main(String[] args) {
Random r=new Random();
JOptionPane.showMessageDialog(null, " Welcome To Lucky7");
JOptionPane.showMessageDialog(null, " Lets try your luck");
JOptionPane.showMessageDialog(null, " Click OK to Generate the number");
String ch="y";
while (ch.equalsIgnoreCase("y")){
int c=r.nextInt(10);
if (c==7){
JOptionPane.showMessageDialog(null, "7UP, you are lucky today");
}
else
{JOptionPane.showMessageDialog(null, "Sorry, its"+c+" for you today");
}
ch= JOptionPane.showInputDialog(null, " Do you want to try again [Y/N]");
}
// TODO code application logic here
}
- @param args the command line arguments
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package lucky7;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author raBbn
*/
public class Lucky7 {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int x, y, z, count = 0;
String key;
String ch="y";
while (ch.equalsIgnoreCase("y")){
JOptionPane.showMessageDialog(null,"play the game");
Random r = new Random();
x = r.nextInt(10);
y = r.nextInt(10);
z = r.nextInt(10);
JOptionPane.showMessageDialog(null, x);
JOptionPane.showMessageDialog(null, y);
JOptionPane.showMessageDialog(null, z);
if (x == 7 || y == 7 || z == 7) {
JOptionPane.showMessageDialog(null,"you are lucky");
}else if (x == 7 && y == 7 && z == 7) {
JOptionPane.showMessageDialog(null,"you are very lucky");
}else{
JOptionPane.showMessageDialog(null,"you are out of luck ..SORRY");
}ch=JOptionPane.showInputDialog("do you want to play again(y/n)??");
}
}
}
-
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package bunkerarray;
import java.util.Random;
/**
*
-
@author Amrit Duwal
*/
public class Randomnumberni {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random r = new Random();
int x;
System.out.println("Enter f6 to start the game");
x = r.nextInt(10);
System.out.print(x);
if (x == 7) {System.out.println("\nyou won");
} else {
System.out.println("\ntry again enter(f6)");
}
}
-
package pkg40lucky7;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author jagdish
*/
public class Main {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {
//String luckyNo = JOptionPane.showInputDialog("Press enter to check your luck");
int Check;
do{
Random r = new Random();
int x;
x = r.nextInt(9); //nextInt(6)
if(x==7)
JOptionPane.showMessageDialog(null,"You are Mr. Lucky 7 Broda.");
else
JOptionPane.showMessageDialog(null,"Sorry. Luck is not with you this time.");Check = JOptionPane.showConfirmDialog(null,"Lets Check your Luck");
}while(Check == 0);
}
-
}
public class RandomNumber {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random r = new Random();
String ch = "y";
JOptionPane.showMessageDialog(null, "Welcome");
while (ch.equalsIgnoreCase("y")) {
int x = r.nextInt(10);
if (x != 7) {
JOptionPane.showMessageDialog(null, "sorry!" + x + "\nbest of luck nexttime");
} else {
JOptionPane.showMessageDialog(null, "Congratulation");
}
ch = JOptionPane.showInputDialog("Do you want to try your luck again??(y/n)");
}
}
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package lucky7;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author sarumdr
*/
public class Lucky7 {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String s = "y";
while (s.equalsIgnoreCase("y")
{
JOptionPane.showMessageDialog(null, "Welcome to Lucky 7 Game");
JOptionPane.showMessageDialog(null, "lets check your luck");
Random r = new Random()
);
int x = r.nextInt(100);
if (x == 7) {
JOptionPane.showMessageDialog(null, "Wow you got 7.\n Your luck is with you");
} else if (x == 77) {
JOptionPane.showMessageDialog(null, "superb you got" + x + ".\n super lucky");
} else if (x == 777) {
JOptionPane.showMessageDialog(null, "babaal you got" + x + ".\n you will have a great day today");
} else {
JOptionPane.showMessageDialog(null, "Oops you got" + x + ".\n May be luck is not with u today.");
s = JOptionPane.showInputDialog("try again????");}
}
}
}
-
}
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package lucky7;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author Dell
*/
public class Lucky7 {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random r = new Random();
int a = 7;
String check = "y";
while (check.equalsIgnoreCase("y")) {
if (a == r.nextInt(10)) {JOptionPane.showMessageDialog(null, "lucky"); } else { JOptionPane.showMessageDialog(null, "notlucky"); } String check1 = JOptionPane.showInputDialog("want to continue?y/n"); check = check1;
}
}
-
package randomnumbergeneration;
import java.util.Random;
import java.util.Scanner;
/**
*
-
@author User
*/
public class RandomNumberGeneration {private static int r1;
/**
- @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
String check = "y";
while (check.equalsIgnoreCase("y")) {
System.out.println("Generating random number from 1 to 9...");
randomm();
if (r1 == 7) {
System.out.println("Congratulations!!! You Win");
} else {
System.out.println("Sorry! You Lose!!!");
}
System.out.println("Would you like to try again? Y/N");
check = in.next();
}
}
public static void randomm() {
Random r = new Random();
r1 = r.nextInt(10);
System.out.println("The number you got is:" + r1);
if (r1 == 0) {
randomm();
}
}
}
OUTPUT:
- @param args the command line arguments
CODE:
package lucky7diag;
import static java.lang.Integer.parseInt;
import static java.time.Clock.system;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author User
*/
public class Lucky7Diag {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {int name;
String check = "y";
Random r = new Random();
int rnd = r.nextInt(7);
while (check.equalsIgnoreCase("y")){
name = parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 7 "));
JOptionPane.showMessageDialog(null, "The number you entered is " + name + "\nThe number generated by computer is " + rnd);
if( name == rnd ){JOptionPane.showMessageDialog(null, "Congratulations !!" + "\nYou are so lucky");
}
else
{
JOptionPane.showMessageDialog(null, "Oops !!" + "\nBetter luck next time");
check = JOptionPane.showInputDialog("Do you want to continue? \nYes : y\nNo : n");
}
} -
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package magic7;
import static java.lang.Integer.parseInt;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
-
@author Dragon15423
*/
public class Magic7 {/**
-
@param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String ch = (JOptionPane.showInputDialog("Welcome !\nWant to test your luck?(yes/no)")), res = "";int[] result = {};
result = new int[3];
boolean test = true;
if (ch.equalsIgnoreCase("yes")) {while (test == true) { Random r = new Random(); for (int i = 0; i < result.length; i++) { result[i] = r.nextInt(10); res += result[i] + ""; if (result[i] == 7) { System.out.println("You sure are lucky (>.<)"); } else { switch (parseInt(res)) { case 77: System.out.println("You are super lucky (>.<)"); break; case 777: System.out.println("Overkill (>.<)"); break; } } } System.out.println("Your number=" + res); String ch1 = (JOptionPane.showInputDialog("Want to test your luck again?(yes/no)")); test = ch1.equalsIgnoreCase("yes"); res = ""; }
}
}
-