[英]How can I take a user's input and use it as an integer?
I am a very new Java user who just installed Eclipse two days ago. 我是一个非常新的Java用户,两天前刚刚安装了Eclipse。 I am getting my feet wet by writing a program that ask's the user what grade they got on a test in percentage form and returning back what their grade is in letter form. 通过编写一个程序,让用户感到不知所措,该程序以百分比形式询问用户在测试中获得的等级,并以字母形式返回其等级。 I was also wondering how I can set this integer in an if-else statement so taht if the test grade is greater than or equal to 90, the program will return A for a grade. 我还想知道如何在if-else语句中设置此整数,以便在测试成绩大于或等于90时,程序将为成绩返回A。 So on and so forth until 50 or below which would return an F. So far, all I have is this: 依此类推,直到50或小于50为止,它将返回F。到目前为止,我所拥有的是:
import java.util.Scanner;
public class gradechecker {
public static void main(String[]args) {
Scanner user_input = new Scanner(System.in);
String test_grade;
System.out.println("Enter the grade percentage you received without the percent sign.");
test_grade = user_input.next();
if(test_grade <= 90) {
}
}
}
To grab a integer value from the user, you use .nextInt(). 要从用户获取整数值,请使用.nextInt()。 In this case, 在这种情况下,
test_grade = user_input.nextInt();
test_grade gets the value entered from the user. test_grade获取从用户输入的值。 However, test_grade is declared as a String variable . 但是,将test_grade声明为String 变量 。 You'll need to change that to a int. 您需要将其更改为int。
What August and azurefrog said are correct in terms of one part of your question, you want to use user_input.nextInt()
instead of user_input.next()
to get an int input from the user. 就您的问题的一部分而言,August和azurefrog所说的是正确的,您想使用user_input.nextInt()
而不是user_input.next()
来从用户获取int输入。
For the second part of your question, consider the best order to check the input and how to check the input. 对于问题的第二部分,请考虑检查输入的最佳顺序以及如何检查输入。
I would consider using >=
instead. 我会考虑使用>=
代替。 Check for an A first, then B, etc. If something is >= 80 but you've already determined that it's not >= 90, you know it'll be a B. And remember to check if the user entered an invalid number as well. 先检查A,然后再检查B,依此类推。如果>> 80但您已经确定它不是> = 90,就知道它将是B。请记住检查用户是否输入了无效数字也一样 Remember to use if...else if...else
to logically join the comparisons, otherwise you could end up with the wrong result. 请记住使用if...else if...else
在逻辑上加入比较,否则可能会得到错误的结果。
Edit: As was pointed out, test_grade needs to be an int instead of a String. 编辑:如前所述,test_grade需要是一个int而不是一个String。
public static void main(String[]args) {
Scanner user_input = new Scanner(System.in);
You must use int to get the percentage in integer form
int test_grade;
System.out.println("Enter the grade percentage you received without the percent sign.");
to get an input use input.nextInt... check the http://www.java-made-easy.com/java-scanner.html 要获取输入,请使用input.nextInt ...检查http://www.java-made-easy.com/java-scanner.html
test_grade = user_input.nextInt();
//Check for the conditions similar to this
if(test_grade >= 90) {
// assign the grade here
String grade = A;
// print the grade
System.out.println("your grade is"+grade)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.