简体   繁体   English

系统扫描仪,用于循环格式的整数和单词

[英]system scanner for integers and words in looping format

I am currently taking a computer science class in high school, and its my first time programming (2 weeks since i started). 我目前正在高中上一门计算机科学课程,这是我第一次编程(距离我上学至今已有2周)。 I have currently finished my program however there were a few details that bothered me. 我目前已经完成程序,但是有些细节困扰着我。 I was supposed to create a program which can calculate average of individual marks and then calculate all of the average marks previously entered by user. 我应该创建一个程序,该程序可以计算单个标记的平均值,然后计算用户先前输入的所有平均标记。 However I noticed something that bothered me, which was the fact that if i were to enter a number when asked to enter a name, it would assume that number is a name. 但是,我注意到令我不安的一个事实,那就是如果当我被要求输入姓名时输入一个数字,它将认为该数字就是一个名字。 Also when I asked for the 5 marks the program would run fine until an error occurs if i accidentally type in a letter or word. 另外,当我要求输入5个标记时,如果我不小心输入字母或单词,该程序将可以正常运行,直到出现错误。 I have done some research and found system scanners however i still don't understand the concept in a looping situation. 我进行了一些研究,发现系统扫描仪,但是在循环情况下,我仍然不了解该概念。 Here is what I have so far. 这是我到目前为止所拥有的。 Any advice or explanation on how to correct this would be appreciated! 关于如何纠正此问题的任何建议或解释将不胜感激!

import java.io.*;
public class loopingEx6Final {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        double average, totalaverage = 0;
        int Mark, Marktotal=0,  marktotalsum=0, count=0;
        String strName, strMark;
        System.out.println("This program will calculate an individual's personal average and then calculate the class average whn instructed.");
        System.out.println("Please type (finish) in order to calculate class average.");
        System.out.println("Please enter a name.");
        strName = br.readLine ();

        while (!strName.equals("finish")){
            System.out.println("The follwing will calculate the average five marks of "+ strName +".");
            System.out.println("Please enter 5 marks.");
            count++;
            for  (int i=0;i<5;i++) {
                Mark = Integer.parseInt(br.readLine());
                    Marktotal= Marktotal + Mark;        

            }
            average = Marktotal/5;
            System.out.println(strName+"'s average is "+average);
            Marktotal= 0;
            System.out.println("\n"+"Please enter a name.");
            strName = br.readLine ();
            totalaverage=(totalaverage+average)/count;
        }       
        System.out.println("The class average of all input grades is:");
        System.out.println(totalaverage);
        System.out.println();
        System.out.println("Thank you for using the program.");
    }
}

With the Scanner in java.util.* you can do something like this to read your inputs for marks. 使用java.util。*中的Scanner,您可以执行类似的操作来读取输入的标记。

Scanner inputs = new Scanner(System.in); // instead of Buffered Reader
int marks, total;
for(int i=0; i<5; i++){
    if(inputs.hasNext()){                // if the input is an integer
        marks = inputs.nextInt();        // use the input
        total += marks;
    } else {
        inputs.readLine();               // read the invalid int input and ignore
        i--;                             // iterate the loop for once more
    }
}

You can use inputs.readLine(); 您可以使用inputs.readLine(); to read a string input.. 读取字符串输入

If you need more references on Scanner.. Try 如果您需要有关扫描仪的更多参考。
http://www.java-made-easy.com/java-scanner.html http://www.java-made-easy.com/java-scanner.html
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM