简体   繁体   English

如何获取用户输入并将其用作整数?

[英]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.

相关问题 如何使用Java Scanner接受整数输入? NoSuchElementException - How can I use Java Scanner to take an integer input? NoSuchElementException 如何获取用户输入,将其验证为整数,并使用它来创建多个矩形? - How do I take a user input, validate it as an integer, and use it to create a number of rectangles? 如何使用扫描仪从用户那里输入输入以选择输入的最小数量? - How can I use scanner to take input from user to choose minimum number entered? 如何从扫描仪获取多个整数输入并将每个整数存储在单独的数组中? - How can I take multiple integer input from scanner and store each integer in separate arrays? 如何获取用户在输入表单 thymeleaf 中写入的文本 - how can I take text that user write in input form thymeleaf 如何在Java中以空格分隔的用户输入 - How can I take user input separated by space in Java 如何验证用户输入是双精度还是整数? - How to verify if user's input is a double or an integer? 在Java中,如何从一个方法获取从用户输入获得的变量并在另一个方法中使用输出? - In Java, how can I take a variable obtained from a user input from one method and use the output in another method? 如何通过控制台输入数字并将该整数传递给其他类 - How can I take number input through console and pass that integer to a different class 如何在 Java 的同一行中将两个或一个整数作为输入? - How can I take either two or one integer as input on the same line in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM