在此编程作业中,我需要对用户给出的两个数字使用位运算符。首先,我需要从单个输入获取输入 a 和 b。这是我使用的:
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a and b numbers between the "
+ "interval [-128,127] (-1 -1 to exit): ");
byte[] userInput = new byte[2];
for(byte i = 0; i < userInput.length; i++) {
userInput[i] = stdin.nextByte();
}
我找不到比较给定的两个数字的方法。输出大概是这样的:
在区间 [-128,127] 中输入 a 和 b 数字(-1 -1 退出):59 18
如何将两个输入数字分配给 a 和 b,以便稍后将它们用于我的按位运算符?
最佳答案
试试这个:
Scanner stdin = new Scanner(System.in);
System.out.print("Enter a and b numbers between the interval [-128,127] (-1 -1 to exit): ");
String inputString = stdin.nextLine();
String[] inputArray = inputString.split(" ");
byte a = Byte.parseByte(inputArray[0]);
byte b = Byte.parseByte(inputArray[1]);
关于java - 如何从扫描仪获取多个输入并将数字分配给一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42466531/