A. Introduction:
Check out this program PhoneCharge.java, you will see the minutes is set to 1987. What if you'd like to calculate charges for different minutes? A program shows results in output, it takes in data as input. In this case, number of minutes is the input, which might change each time the program runs.
B. Take a look at
AlwaysThree. Notice the comments on Scanner, in.nextLine( ), and in.next( ). This is how you read input from keyboard and store them in variables. We will learn more about Scanner later on, for now, you just need to know we can read input data from the keyboard.
import java.util.Scanner; // You have to add this line to use Scanner class
class AlwaysThree {
public static void main(String[] args) {
// The line below creates a Scanner variable named "in" to read input
Scanner in = new Scanner(System.in);
String name = "";
String done = "";
System.out.println("Hello, what's your name?");
name=in.nextLine(); // read user name from the keyboard
System.out.println("Hey " + name + ", think of a number. Type 'done' when you are ready.");
done= in.next(); // read 'done' from keyboard
System.out.println("OK, this is your original number. Now, multiply it by 2.");
System.out.println("Type 'done' when you are done.");
done= in.next();
System.out.println("Good, now multiply the new number by 5. Type 'done' when you are done.");
done= in.next();
System.out.println("Great, here is the last step but it's a bit tricky.");
System.out.println("You need to divide your current number by the original number,");
System.out.println("then subtract 7. Type 'done' when you are done.");
done= in.next();
System.out.println("Is your answer 3? ;). Type 'true' if I am correct.");
done= in.next();
System.out.println("Thanks for playing with me, " + name +". Come back soon!");
in.close();
}
}
C. Try:
Once you had fun playing the above, try to create your own game by following these steps:
1) Create a new class "MyNumber" in Codiva. Copy and paste the code below, run it.
2) Answer all questions in the comments
3) Modify x, y, z and lines after "//game plan" to create a new number game.
4) Invite your family and friends to play your game and improve it in your code.
import java.util.Scanner; // What does the line do?
class MyNumber {
public static void main(String[] args) {
String name="";
int x=9, y=13, z=7;
double v =3.5;
// What does the line below do?
Scanner in = new Scanner(System.in);
System.out.println("Hello, what's your name?");
name=in.nextLine(); // what does this line do?
System.out.println("Hey " + name + ", you want to guess my number? Type 'true' if yes.");
in.next(); // what does this line do?
//game plan
System.out.println("I have an integer, after I multiply it by " + y + " and divide it by " +z);
System.out.println("The remainder is " + x*y%z);
System.out.println("What is my number?");
// why do you think we use in.nextInt() below? Can you find the difference between next() and nextInt()?
int answer = in.nextInt();
if(answer == x){
System.out.println("You got it!");
}
else {
System.out.println("Not exactly. My number is " + x);
}
in.close();
}
}
https://repl.it/@DaveyYu/ImAnAwesomeDude