1. Give us a very brief list of code (sometimes a single line is all that is nee
ID: 3790516 • Letter: 1
Question
1. Give us a very brief list of code (sometimes a single line is all that is needed) for the following:
a. The statement to allow our programs to use the methods of the Scanner Class in our programs.
b. An "if" statement with an "else" clause (make up the comparison) - print out the word "True" for true and "False" for false.
c. The statement to assign myRandomNumber (an integer variable) a random number between 5 and 10 inclusive of 10 - the way to do this is highlighted in the "Things To Look Out For" section of this week's lesson document.
d. A "for" loop assuming myLoopVariable was declared above your statement as type int and the single statement in the body of the loop should be a print statement that outputs the the value of myLoopVariable.
e. A method that accepts a String value and prints out the value with a println() statement.
Explanation / Answer
Hi, Please find my answer.
Please let me know in case of any issue.
//a
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(); // taking integer input from user
//b
int a = 3, b = 5;
if(a > b)
System.out.println("True");
else
System.out.println("False");
//c
Random randObject = new Random();
int myRandomNumber = randObject.nextInt(6)+5;
//d
int myLoopVariable;
for(myLoopVariable=1; myLoopVariable<10; myLoopVariable++){
System.out.println(myLoopVariable);
}
//e
public void printString(String input){
System.out.println(input);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.