0
A
回答
0
如果我理解你的问题正确,您正在寻找这样的事情:
a = b = c = 3;
这像这样被评价:
a = (b = (c = 3))
而且是相同的:
c = 3;
b = c;
a = b;
2
您可以使用阵列来实现此目的,例如:
public static void main(String[] args) {
int[] values = new int[3];
Scanner in = new Scanner(System.in);
for(int i = 0; i < values.length; i++) {
values[i] = in.nextInt();
}
System.out.println(Arrays.toString(values));
}
UPDATE 2
在java中8的上述溶液可具有较短的版本:
Scanner in = new Scanner(System.in);
Integer[] inputs = Stream.generate(in::nextInt).limit(3).toArray(Integer[]::new);
UPDATE 1
还有另一种方法,这是更接近cin
:
public class ChainScanner {
private Scanner scanner;
public ChainScanner(Scanner scanner) {
this.scanner = scanner;
}
public ChainScanner readIntTo(Consumer<Integer> consumer) {
consumer.accept(scanner.nextInt());
return this;
}
public ChainScanner readStringTo(Consumer<String> consumer) {
consumer.accept(scanner.next());
return this;
}
}
public class Wrapper {
private int a;
private int b;
private String c;
public void setA(int a) {
this.a = a;
} /* ... */
}
public static void main(String[] args) {
ChainScanner cs = new ChainScanner(new Scanner(System.in));
Wrapper wrapper = new Wrapper();
cs.readIntTo(wrapper::setA).readIntTo(wrapper::setB).readStringTo(wrapper::setC);
System.out.println(wrapper);
}
相关问题
- 1. 在Java中是否有多输入JOptionPane?
- 2. 是否有可能在Java中获得扁平(unboxed)结构?
- 3. 是否有可能获得网关/路由器IP在Java中
- 4. 是否有可能在pdf中获得行号?
- 5. 是否有可能在Java
- 6. 是否有可能获得AngularJS认证?
- 7. 是否有可能获得RSS存档
- 8. 是否有可能在Python中的“导入模块”中获得“导入模块”?
- 9. 是否有可能在运行时从Select2获得minimumResultsForSearch
- 10. 是否有可能在Java中
- 11. 是否有可能在iTextSharp中获得底部像素
- 12. EventBus,是否有可能在onCreate中获得粘性事件?
- 13. 是否有可能在JavaScript中获得窗口实例somehome?
- 14. 是否有可能在Monogame/XNA中获得“上下文”手势?
- 15. 是否有可能在Android中获得视频的分辨率?
- 16. 是否有可能在此查询中获得overall_score的总和?
- 17. 是否有可能获得在postgres中查询的历史
- 18. 是否有可能在iOS中获得Game Center ID或帐户?
- 19. 是否有可能在QuillJS中获得op的位置?
- 20. 是否有可能在Windows中获得浮动触摸坐标?
- 21. 是否有可能在MySQL数据库中获得列长度?
- 22. 是否有可能在JavaScript中获得Scroll Lock状态?
- 23. 是否有可能在rails中获得新会话的id?
- 24. 是否有可能在Stata中获得文件修改时间?
- 25. 是否有可能在WebBrowser中获得后JavaScript参考?
- 26. 是否有可能与Java
- 27. 是否有可能在运行时在属性中获得Maven依赖关系?
- 28. 是否有可能检测到Java中是否有类可用?
- 29. 是否有可能在bash中部分自动填充输入?
- 30. 是否有可能获得嵌入在移动应用程序中的密钥
我不敢相信你没有使用类似“java同时赋值多个变量”的东西 - 前8个结果都是堆栈溢出! :) –
可能重复[Java - 同时将两个表达式分配给单个变量](https://stackoverflow.com/questions/3996593/java-assigning-two-expressions-to-a-single-variable-simultaneously) –
可能重复的[如何在java中打印多个可变行](https://stackoverflow.com/questions/23584563/how-to-print-multiple-variable-lines-in-java) –