(1) Prompt the user to input an integer, a double, a character, and a string, st
ID: 2246572 • Letter: #
Question
(1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. (2 pts)
(3) Extend to cast the double to an integer, and output that integer. (2 pts)
public class BasicInput {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int userInt = 0;
double userDouble = 0.0;
// FIXME Define char and string variables similarly
System.out.println("Enter integer:");
userInt = scnr.nextInt();
// FIXME (1): Finish reading other items into variables, then output the four values on a single line separated by a space
// FIXME (2): Output the four values in reverse
// FIXME (3): Cast the double to an integer, and output that integer
return;
}
}
Explanation / Answer
BasicInput.java
import java.util.Scanner;
public class BasicInput {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int userInt = 0;
double userDouble = 0.0;
// FIXME Define char and string variables similarly
char ch;
String str;
// FIXME (1): Finish reading other items into variables, then output the
// four values on a single line separated by a space
System.out.println("Enter integer:");
userInt = scnr.nextInt();
System.out.println("Enter double:");
userDouble = scnr.nextDouble();
System.out.println("Enter character:");
ch = scnr.next(".").charAt(0);
System.out.println("Enter the String :");
str = scnr.next();
System.out.println(userInt + " " + userDouble + " " + ch + " " + str);
// FIXME (2): Output the four values in reverse
System.out.println(str + " " + ch + " " + userDouble + " " + userInt);
// FIXME (3): Cast the double to an integer, and output that integer
System.out.println((int) userDouble);
return;
}
}
_____________________
Output:
Enter integer:
44
Enter double:
55.66
Enter character:
t
Enter the String :
hello
44 55.66 t hello
hello t 55.66 44
55
_______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.