From 990dd614d4565172f375970c42157ec257d9fa32 Mon Sep 17 00:00:00 2001 From: Aldrin Misquitta Date: Sat, 13 Mar 2021 14:51:42 +0400 Subject: [PATCH 1/2] Ported 66 Number to Java --- 66 Number/java/Number.java | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 66 Number/java/Number.java diff --git a/66 Number/java/Number.java b/66 Number/java/Number.java new file mode 100644 index 00000000..2e481fca --- /dev/null +++ b/66 Number/java/Number.java @@ -0,0 +1,62 @@ +import java.util.Scanner; + +public class Number { + + public static void main(String[] args) { + printIntro(); + int points = 100; //start with 100 points for the user + + Scanner scan = new Scanner(System.in); + boolean done = false; + while (!done) { + System.out.print("GUESS A NUMBER FROM 1 TO 5? "); + int g = scan.nextInt(); + + //Initialize 5 random numbers between 1-5 + var r = randomNumber(1); + var s = randomNumber(1); + var t = randomNumber(1); + var u = randomNumber(1); + var v = randomNumber(1); + + if (r == g) { + points -= 5; + } else if (s == g) { + points += 5; + } else if (t == g) { + points += points; + } else if (u == g) { + points += 1; + } else if (v == g) { + points -= points * 0.5; + } else { + continue; //Doesn't match any of our random numbers, so just ask for another guess + } + + if (points > 500) { + done = true; + } else { + System.out.println("YOU HAVE " + points + " POINTS."); + } + } + + System.out.println("!!!!YOU WIN!!!! WITH " + points + " POINTS.\n"); + } + + private static int randomNumber(int x) { + //Note: 'x' is totally ignored as was in the original basic listing + return (int) (5 * Math.random() + 1); + } + + private static void printIntro() { + System.out.println(" NUMBER"); + System.out.println(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.println("\n\n\n"); + System.out.println("YOU HAVE 100 POINTS. BY GUESSING NUMBERS FROM 1 TO 5, YOU"); + System.out.println("CAN GAIN OR LOSE POINTS DEPENDING UPON HOW CLOSE YOU GET TO"); + System.out.println("A RANDOM NUMBER SELECTED BY THE COMPUTER."); + System.out.println("\n"); + System.out.println("YOU OCCASIONALLY WILL GET A JACKPOT WHICH WILL DOUBLE(!)"); + System.out.println("YOUR POINT COUNT. YOU WIN WHEN YOU GET 500 POINTS."); + } +} From 4e5d65b621bad96621c2e06fac8e97098f85b384 Mon Sep 17 00:00:00 2001 From: Aldrin Misquitta Date: Sat, 13 Mar 2021 14:52:18 +0400 Subject: [PATCH 2/2] Deleted main.class. Not sure what it is doing here --- 66 Number/java/main.class | 69 --------------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 66 Number/java/main.class diff --git a/66 Number/java/main.class b/66 Number/java/main.class deleted file mode 100644 index c3df4ebf..00000000 --- a/66 Number/java/main.class +++ /dev/null @@ -1,69 +0,0 @@ - -import java.time.temporal.ValueRange; -import java.util.Arrays; -import java.util.Random; -import java.util.Scanner; - -public class Number { - - public static int points = 0; - - public static void printempty() { System.out.println(" "); } - - public static void print(String toprint) { System.out.println(toprint); } - - public static void main(String[] args) { - print("YOU HAVE 100 POINTS. BY GUESSING NUMBERS FROM 1 TO 5, YOU"); - print("CAN GAIN OR LOSE POINTS DEPENDING UPON HOW CLOSE YOU GET TO"); - print("A RANDOM NUMBER SELECTED BY THE COMPUTER."); - printempty(); - print("YOU OCCASIONALLY WILL GET A JACKPOT WHICH WILL DOUBLE(!)"); - print("YOUR POINT COUNT. YOU WIN WHEN YOU GET 500 POINTS."); - printempty(); - - try { - while (true) { - print("GUESS A NUMBER FROM 1 TO 5"); - - - Scanner numbersc = new Scanner(System.in); - String numberstring = numbersc.nextLine(); - - int number = Integer.parseInt(numberstring); - - if (!(number < 1| number > 5)) { - - Random rand = new Random(); - - int randomNum = rand.nextInt((5 - 1) + 1) + 1; - - if (randomNum == number) { - print("YOU HIT THE JACKPOT!!!"); - points = points * 2; - } else if(ValueRange.of(randomNum, randomNum + 1).isValidIntValue(number)) { - print("+5"); - points = points + 5; - } else if(ValueRange.of(randomNum - 1, randomNum + 2).isValidIntValue(number)) { - print("+1"); - points = points + 1; - } else if(ValueRange.of(randomNum - 3, randomNum + 1).isValidIntValue(number)) { - print("-1"); - points = points - 1; - } else { - print("-half"); - points = (int) (points * 0.5); - } - - print("YOU HAVE " + points + " POINTS."); - } - - if (points >= 500) { - print("!!!!YOU WIN!!!! WITH " + points + " POINTS."); - return; - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } -}