简体   繁体   English

不能使String等于方法,并且不能将Strings转换为Numbers。

[英]Can't make String equal method and Converting Strings to Numbers work together.

It is a Java beginner question. 这是一个Java初学者的问题。 The program is not complicated, it just asks the user input grade and then finish it when the user input "y" , and then calculate max, min,average, and total. 该程序并不复杂,它只是询问用户输入的等级,然后在用户输入“ y”时完成输入,然后计算最大值,最小值,平均值和总计。

My trouble is that I can't stop the loop when I type y. 我的麻烦是输入y时无法停止循环。 Thank you so much, if anyone can help me out. 非常感谢,如果有人可以帮助我。 I have tried many approaches to figure that. 我尝试了很多方法来解决这个问题。

    do
       { 
        System.out.println("please go on");
        input = keyboard.next();                    
        grade = Integer.parseInt(input);
        InputTimes++;
        if (grade>Maxgrade)
        {   
            Maxgrade = grade;       
         }

        if(Mingrade>grade)
        {
            Mingrade= grade;            
        }

        if (input.equals("y"))
        {   
            GameOver = true;
            System.out.println("yyyyyyy");
            break;
        }   
        else 
        {
            GameOver = false;
            System.out.println("nnnnnn");   
        }   
        totalgradeNoF = grade+totalgradeNoF;

    }while(GameOver==false);
Scanner keyboard = new Scanner(System.in);
int Maxgrade = 0, Mingrade = 0, InputTimes = 0, grade = 0, totalgradeNoF = 0;
boolean GameOver = false;
String input = "";
do {
    System.out.println("Please go on : ('y' to stop)");
    input = keyboard.nextLine();
    if (input.equals("y")) {
        GameOver = true;
    } else {
        grade = Integer.parseInt(input);
        InputTimes++;

        if (grade > Maxgrade) {
             Maxgrade = grade;
        }
        if (Mingrade > grade) {
             Mingrade = grade;
        }

        totalgradeNoF = grade + totalgradeNoF;
   }
} while (!GameOver);

Some things to change : 有些事情要改变:

  • You need to check the "y" BEFORE and if it is not a "y" so you can cast to int 您需要先检查“ y”,如果不是“ y”,则可以转换为int
  • You don"t need to use break so 您不需要使用break这样
  • You may use !GameoOver instead of GameOver==false 您可以使用!GameoOver代替GameOver==false
  • Don't need to set Gameover=false at each iteration, just at the initialisation 无需在每次迭代时都将Gameover=false设置为初始化

当您输入“ y”时,NumberFormatException即将到来,因此您需要处理此异常,然后在键入“ y”后可以退出循环。

Use for Scanner methods nextInt() and nextLine() for reading input. 用于Scanner方法nextInt()nextLine()来读取输入。

EX:- 例如:-

Scanner scan = new Scanner(System.in);

then we can use scan.nextInt() for get int 然后我们可以使用scan.nextInt()来获取int

and we can use scan.nextLine() for get Strings 我们可以使用scan.nextLine()获取字符串

https://www.tutorialspoint.com/java/util/scanner_next.htm https://www.tutorialspoint.com/java/util/scanner_next.htm
this link may be help for you. 此链接可能对您有帮助。

sorry for my english. 对不起我的英语不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能让 Jackson 和 Lombok 一起工作 - Can't make Jackson and Lombok work together 无法使字符串compareTo方法正常工作 - Can't get Strings compareTo method to work 根据加在一起的字母值对字符串进行排序。 (JAVA) - Sorting strings based off of value of letters added together. (Java) 挥发性和原子性在一起 它有意义吗? - volatile and atomic together. Does it make any sense? 如何将字符串和整数绑定在一起。 爪哇 - How have a string and an int tied together. Java Java - 将 2 个数字相加(作为字符串,不能使用 parse.int) - Java - add 2 numbers together (as strings, can't use parse.int) 如果语句在字符串的一部分中不等于字符串中的位置,该如何使else - if语句起作用? - How do I make this else- if statement work by saying in a part of a string isn't equal to a location in the string? 将字母数字字符串转换为数字和字符串的映射时生成的空映射 - Empty map generated while converting alphanumeric string to a map of numbers and strings Java读取字符串并将字符串中的数字转换为Integers类型 - Java reading in a string and converting numbers in strings into Integers types 不能与BufferedInputStream和BufferedReader一起使用 - can't work with BufferedInputStream and BufferedReader together
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM