In: Computer Science
USING JAVA (netbeans)
In your main, ask the user for an int. Do this by first printing a request for the int, then creating an int x, and using the Scanner to read an int from the user and put it in x, like this:
int x = scan.nextInt();
☑ Next read in two doubles d1 and d2 from the user. This will look a lot like what you did for the int, but the scanner reads doubles using
scan.nextDouble();
☑ Next read in a string s from the user. Scanner reads strings using
scan.next();
Part B
☑ if x is below 12, double it, otherwise halve it; either way print the result.
☑ if s is "Hello" print "Hi there!" otherwise print "No hello? RUDE!"
☑ if d1 and d2 are the same, print "same" otherwise print whichever of d1 or d2 is higher, with the word "MAX:" in front of it
CODE -
package sample;
import java.util.Scanner;
public class Sample {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer: "); // Prompting user to enter
an integer
int x = scan.nextInt(); // Reading an integer from user and storing
it in x
System.out.print("Enter a double: "); // Prompting user to enter a
double
double d1 = scan.nextDouble(); // Reading a double from user and
storing it in d1
System.out.print("Enter another double: "); // Prompting user to
enter another double
double d2 = scan.nextDouble(); // Reading an double from user and
storing it in d2
System.out.print("Enter a string: "); // Prompting user to enter a
string
String s = scan.next(); // Reading a string from user and storing
it in s
if(x<12)
x = x*2; // Multiplying x by 2 if x is below 12
else
x = x/2; // Dividing x by 2 otherwise
System.out.printf("x = %d\n", x); // Printing the result
if(s.equals("Hello"))
System.out.println("Hi there!"); // Printing "Hi there!" if user
entered "Hello"
else
System.out.println("No hello? RUDE!"); // Printing another message
otherwise
if(d1 == d2)
System.out.println("same"); // Printing "same" if d1 = d2
// Printing max of the two values otherwise
else if(d1 > d2)
System.out.printf("MAX: %.2f\n", d1);
else
System.out.printf("MAX: %.2f\n", d2);
}
}
SCREENSHOT -
If you have any doubt regarding the solution, then do comment.
Do upvote.