Skip to content

Commit 883307d

Browse files
Completing Exercise 3.3 of the ThinkJava book
Created ascript that took a user input, compared it to a random numeber from 1-100 and calcualted the difference between the two.
1 parent 4377ac3 commit 883307d

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

ch03/Exercise_3_2.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import java.util.Scanner;
22

33
public class Exercise_3_2 {
4-
4+
55
public static void main(String[] args) {
6+
//initialize the Scanner
7+
Scanner in = new Scanner(System.in);
8+
final double nineDivFive = 1.8;
9+
10+
//Prompts Person inputs a temperature in Celsius
11+
System.out.print("Enter a temperature in Celsius: ");
12+
13+
//Take the input
14+
double tempInput = in.nextDouble();
615

16+
//Check for the proper input from tempInput
17+
System.out.printf("User input = %f\n", tempInput);
18+
19+
//Calculation. I used an assigned value for the 9/5 part of the conversion. since it doesn't change
20+
final double fahCalc = tempInput * nineDivFive + 32.0;
21+
22+
//Check for the proper input from tempInput
23+
System.out.printf("Final calculation are as follows %.1f C = %.1f F", tempInput, fahCalc);
724
}
825

926
}

ch03/Exercise_3_3.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Scanner;
2+
import java.util.Random;
3+
4+
5+
public class Exercise_3_3 {
6+
7+
public static void main(String[] args) {
8+
// importing the random module
9+
Random random = new Random();
10+
int number = random.nextInt(100) + 1;
11+
// System.out.println(number);
12+
13+
// importing the ability to get user input
14+
Scanner in = new Scanner(System.in);
15+
16+
// Initial prompt to user and input to their number
17+
System.out.println("I'm thinking of a number between 1 and 100.\nCan you guess what it is?");
18+
System.out.print("Type a number: ");
19+
int userInput = in.nextInt();
20+
System.out.printf("Your guess is: %d\n", userInput);
21+
System.out.printf("The number I was thinking of is: %d\n", number);
22+
23+
// Calculating how much they were off by
24+
int calc = (number - userInput);
25+
26+
// Telling the user how much they were off by
27+
System.out.printf("You were off by: %d ", calc);
28+
}
29+
30+
}

0 commit comments

Comments
 (0)