Java 如何从扫描仪获取多个输入并将数字指定给一个值?
在这个编程任务中,我需要对用户给出的两个数字使用位运算符。首先,我需要从单个输入获取输入a和b。以下是我使用的方法:Java 如何从扫描仪获取多个输入并将数字指定给一个值?,java,arrays,for-loop,Java,Arrays,For Loop,在这个编程任务中,我需要对用户给出的两个数字使用位运算符。首先,我需要从单个输入获取输入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]
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();
}
Scanner stdin=新扫描仪(System.in);
System.out.println(“在
+“间隔[-128127](-1-1退出):”;
字节[]用户输入=新字节[2];
for(字节i=0;i
我找不到一种方法来比较给出的两个数字。以下是输出的大致情况:
在区间[-128127](-1-1退出)中输入a和b编号:59 18
如何将这两个输入数字分配给a和b,以便以后将它们用于位运算符?正如Pavneet Singh提到的那样
a=userInput[0]
b=userInput[1]
应该有用。或者,为了避免使用阵列,可以执行以下操作:
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a and b numbers between the "
+ "interval [-128,127] (-1 -1 to exit): ");
byte a = stdin.nextByte();
byte b = stdin.nextByte();
}
正如帕夫尼特·辛格提到的那样
a=userInput[0]
b=userInput[1]
应该有用。或者,为了避免使用阵列,可以执行以下操作:
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a and b numbers between the "
+ "interval [-128,127] (-1 -1 to exit): ");
byte a = stdin.nextByte();
byte b = stdin.nextByte();
}
试试这个:
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]);
试试这个:
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]);
a=userInput[0]
和b=userInput[1]
a=userInput[0]
和b=userInput[1]
非常感谢非常感谢这非常有帮助谢谢这非常有帮助