Write a complete program in a single class called RandomStrings . Here is what R
ID: 3539600 • Letter: W
Question
Write a complete program in a single class called RandomStrings.
Here is what RandomStrings should do: first, it reads in two Strings from the keyboard. Then it should generate a single random number using the random() method from the Math class. If that number is strictly less than .66, the program should print out the first String you read in; otherwise it should print the second String you read in.
Use the Scanner class to read in the two Strings.
Here are two examples of sample program behavior:
Enter first string:
dog
Enter second string:
donkey
[random value .23451555 is generated]
dog
---
Enter first string:
boy
Enter second string:
girl
[random value .73451785 is generated]
girl
Explanation / Answer
import java.util.Scanner;
public class RandomStrings {
public static void main(String s[])
{
Scanner in = new Scanner(System.in);
String first,second;
double rand;
System.out.println("Enter first string:");
first = in.nextLine();
System.out.println("Enter second string:");
second = in.nextLine();
rand = Math.random();
if(rand<0.66)
{
System.out.println(first);
}
else
{
System.out.println(second);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.