我有一个任务,我们必须制作一个Java程序,它将读取用户的4个输入:
- 整型
- 双倍
- 性格
- 细绳
然后,它在一行中以上述顺序输出它们,在另一行中以相反的顺序输出它们.
输入内容为
- 整型: 99
- 双倍: 3.77
- 性格: z
- 细绳: Howdy
但是,当它们被输出时,字符串就是空白的
预期结果:
Enter integer:
Enter double:
Enter character:
Enter string:
99 3.77 z Howdy
Howdy z 3.77 99
3.77 cast to an integer is 3
实际结果:
Enter integer:
Enter double:
Enter character:
Enter string:
99 3.77 z
z 3.77 99
3.77 cast to an integer is 3
这是我拥有的代码
import java.util.Scanner;
public class BasicInput {
public static void main(细绳[] args) {
Scanner scnr = new Scanner(System.in);
int userInt = 0;
double user双倍 = 0.0;
// FIXME Define char and string variables similarly
char myChar;
细绳 my细绳 = "";
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
System.out.println("Enter double: ");
user双倍 = scnr.next双倍();
System.out.println("Enter character: ");
myChar = scnr.next().charAt(0);
System.out.println("Enter string: ");
my细绳 = scnr.nextLine();
System.out.println(userInt + " " + user双倍 + " " + myChar + " " + my细绳);
// FIXME (2): Output the four values in reverse
System.out.println(my细绳 + " " + myChar + " " + user双倍 + " " + userInt);
// FIXME (3): Cast the double to an integer, and output that integer
System.out.println(user双倍 + " cast to an integer is " + (int)user双倍);
return;
}
}
如何使字符串显示在输出中?它只是显示为空字符串.我甚至try 将my细绳
变量初始化为"Hello"
,但它仍然显示为空.