[英]Using user-inputted characters in If/Switch statements
I am taking my first Java class, and am having a hard time finishing this assignment. 我正在学习我的第一个Java课程,并且很难完成这项任务。
The assignment itself is quite simple. 作业本身很简单。 All I have to do is prompt the user to input an integer, a character, then another integer.
我所要做的就是提示用户输入一个整数,一个字符,然后输入另一个整数。
If the character inputted is +, the two integers should be added together. 如果输入的字符是+,则应将两个整数相加。 If the character inputted is -, the two integers should be subtracted.
如果输入的字符是 - ,则应减去两个整数。 Etc...
等等...
The problem is that I do not know how to accept a character from the user, then use it in my If/Switch statements. 问题是我不知道如何接受用户的字符,然后在我的If / Switch语句中使用它。
Here are my prompts: 以下是我的提示:
// Prompt the user to enter an integer
System.out.print("Please enter an integer number:");
int int1 = input.nextInt();
// Prompt the user to enter a character
// Since I could not use characters, I am using integers to represent them.
System.out.print("Enter 0 for +, 1 for -, 2 for /, 3 for %, and 4 for *");
int char1 = input.nextInt();
// Prompt the user to enter another integer
System.out.print("Please enter another integer number:");
int int2 = input.nextInt();
Here are my If/Switch statements: (I know they are redundant, but it's part of the assignment) 这是我的If / Switch语句:(我知道它们是多余的,但它是作业的一部分)
if (char1 == 0) {
int ans = int1 + int2;
System.out.println(int1 + " + " + int2 + " = " + ans);
} else if (char1 == 1) {
int ans = int1 - int2;
System.out.println(int1 + " - " + int2 + " = " + ans);
} else if (char1 == 2) {
int ans = int1 / int2;
System.out.println(int1 + " / " + int2 + " = " + ans);
} else if (char1 == 3) {
int ans = int1 % int2;
System.out.println(int1 + " % " + int2 + " = " + ans);
} else if (char1 == 4) {
int ans = int1 * int2;
System.out.println(int1 + " * " + int2 + " = " + ans);
} else
System.out.println("Invalid operator");
int result = 0;
switch (char1) {
case 0: result = int1 + int2;
System.out.println(int1 + " + " + int2 + " = " + result); break;
case 1: result = int1 - int2;
System.out.println(int1 + " - " + int2 + " = " + result); break;
case 2: result = int1 / int2;
System.out.println(int1 + " / " + int2 + " = " + result); break;
case 3: result = int1 % int2;
System.out.println(int1 + " % " + int2 + " = " + result); break;
case 4: result = int1 * int2;
System.out.println(int1 + " * " + int2 + " = " + result); break;
default: System.out.println("Invalid operator");
}
Everything else works just fine. 其他一切都很好。 I'm just trying to change from using 0 for +, 1 for -, to just using +, -, /, %, and *.
我只是想尝试将0改为+,1改为 - ,改为使用+, - ,/,%和*。
you can take the operator from the user as String or Char 您可以将用户的操作符作为String或Char
Like this : 像这样 :
Scanner input = new Scanner(System.in);
// Prompt the user to enter an integer
System.out.print("Please enter an integer number:");
int int1 = input.nextInt();
// Prompt the user to enter a character
// Since I could not use characters, I am using integers to represent them.
System.out.print("Enter +, -, /, %, *");
char char1 = input.next().charAt(0);
// Prompt the user to enter another integer
System.out.print("Please enter another integer number:");
int int2 = input.nextInt();
if (char1=='+') {
int ans = int1 + int2;
System.out.println(int1 + " + " + int2 + " = " + ans);
} else if (char1=='-') {
int ans = int1 - int2;
System.out.println(int1 + " - " + int2 + " = " + ans);
} else if (char1=='/') {
int ans = int1 / int2;
System.out.println(int1 + " / " + int2 + " = " + ans);
} else if (char1=='%') {
int ans = int1 % int2;
System.out.println(int1 + " % " + int2 + " = " + ans);
} else if (char1=='*') {
int ans = int1 * int2;
System.out.println(int1 + " * " + int2 + " = " + ans);
} else {
System.out.println("Invalid operator");
}
int result = 0;
switch (char1) {
case '+':
result = int1 + int2;
System.out.println(int1 + " + " + int2 + " = " + result);
break;
case '-':
result = int1 - int2;
System.out.println(int1 + " - " + int2 + " = " + result);
break;
case '/':
result = int1 / int2;
System.out.println(int1 + " / " + int2 + " = " + result);
break;
case '%':
result = int1 % int2;
System.out.println(int1 + " % " + int2 + " = " + result);
break;
case '*':
result = int1 * int2;
System.out.println(int1 + " * " + int2 + " = " + result);
break;
default:
System.out.println("Invalid operator");
}
You can do this: 你可以这样做:
System.out.print("Enter +, -, /, or *");
char char1 = input.nextLine().charAt(0);
nextLine
will get the entire thing that the user input, and charAt
gets the character at the specified index. nextLine
将获取用户输入的全部内容,而charAt
将获取指定索引处的字符。
Therefore, charAt(0)
gets the first character. 因此,
charAt(0)
获得第一个字符。 Here is a visual explanation: the top is the string and the bottom is what character number it is: 这是一个直观的解释:顶部是字符串,底部是字符数:
|t|h|i|s| |i|s| |a| |t|e|s|t|
-----------------------------
|0|1|2|3|4|5|6|7|8|9|1|1|1|1|
| | | | | | | | | | |0|1|2|3|
You can use a char
from the user input this way: 您可以通过以下方式使用来自用户输入的
char
:
A char
is only an unsigned integer value 0..65535 so you can write eg +
for the value 43
[Which is the unicode of '+'] char
只是一个无符号整数值0..65535所以你可以为值43
编写例如+
[这是'+'的unicode]
switch (char1) {
case '+': result = int1 + int2;
System.out.println(int1 + " + " + int2 + " = " + result); break;
case '-': result = int1 - int2;
System.out.println(int1 + " - " + int2 + " = " + result); break;
case '/': result = int1 / int2;
System.out.println(int1 + " / " + int2 + " = " + result); break;
case '%': result = int1 % int2;
System.out.println(int1 + " % " + int2 + " = " + result); break;
case '*': result = int1 * int2;
System.out.println(int1 + " * " + int2 + " = " + result); break;
default: System.out.println("Invalid operator");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.