如何将数字显示为星号

时间:2015-02-10 04:29:48

标签: java

所以我必须将总分数(我已经计算过)的数量显示为astrisk。输出应该是这样的,

A=******

B=******

C=******

D=*****

这是我的代码:

import java.io.*; 
import java.util.*;
public class grades 
{

    public static final void main(String args[]) throws FileNotFoundException
    {
        System.out.println("name");
        Scanner myScanner = new Scanner(new File("students.txt"));//Creating new scanner object 

            int lineNumber =1;//Line Number for each student.
            while (myScanner.hasNext())
            {

                String firstName = myScanner.next();//Scanning student first name and storing in the string
                String lastName = myScanner.next();// Scanning student last name and storing in the string
                String athleteFlag=myScanner.next();//Scanning for verification of athlete and storing in the string
                String athlete= "Y";
                String notAthlete = "N";
                //if else statement to declare if student is a athlete
                 if (athleteFlag.equals(athlete))
                {
                     athleteFlag="YES";
                }
                 else if(athleteFlag.equals(notAthlete))
                 {
                    athleteFlag="NO";               
                 }
                int quiz1=myScanner.nextInt();// Grades on Quiz 1
                int quiz2=myScanner.nextInt();// Grades on Quiz 2
                int quiz3=myScanner.nextInt();// Grades on Quiz 3
                int quiz4=myScanner.nextInt();// Grades on Quiz 4
                int quiz5=myScanner.nextInt();// Grades on Quiz 5
                int test1=myScanner.nextInt();// Grades on Test 1
                int test2=myScanner.nextInt();// Grades on Test 2
                //student quiz average
                double quizAverage = (double)(quiz1+quiz2+quiz3+quiz4+quiz5)/5;
                //Overall Numerical Grade
                float overallNumericalGrade =(float)(quizAverage+test1+test2)/3;
                //Students letter Grade, eligibility and counting number of each grade
                String grade;
                String eligibility;
                int gradeA=0;
                int gradeB=0;
                int gradeC=0;
                int gradeD=0;
                int gradeF=0;
                    if(overallNumericalGrade>=90.0)
                    {
                        grade="A";
                        eligibility ="YES";
                        gradeA++;
                    }

                    else if(overallNumericalGrade>=80.0)
                    {
                        grade = "B";
                        eligibility ="YES";
                        gradeB++;
                    }
                    else if(overallNumericalGrade>=70.0)    
                    {
                        grade = "C";
                        eligibility ="YES";
                        gradeC++;
                    }
                    else if(overallNumericalGrade>=60.0)
                    {
                        grade = "D";
                        eligibility ="NO";
                        gradeD++;
                    }
                    else
                    {
                        grade = "F";
                        eligibility ="NO";
                        gradeF++;
                    }


                System.out.println(lineNumber + " "+lastName+","+firstName+"/ "+athleteFlag+ "/ " + eligibility +
                        " / " +"Grades on all quiz "+ quiz1+ " "+quiz2+" "+quiz3+" "+ quiz4+" "+quiz5+" "+test1+" "+
                        test2+ " /" +"quiz average is " + quizAverage +"\n " + "Overall Numerical grade is " +
                        overallNumericalGrade+" / "+ "Student letter Grade is " + grade);
                lineNumber++;  

            }

    } 

}

我只是不知道如何输出这个。任何帮助都会得到满足。提前谢谢。

1 个答案:

答案 0 :(得分:0)

假设所有成绩的总和为overallNumericalGrade,并且您希望在最后一个输出中将其显示为*(因为这是您代码中的唯一输出),您可以

System.out.println(lineNumber + " "+lastName+","+firstName+"/ "+athleteFlag+ "/ " + eligibility +
                    " / " +"Grades on all quiz "+ quiz1+ " "+quiz2+" "+quiz3+" "+ quiz4+" "+quiz5+" "+test1+" "+
                    test2+ " /" +"quiz average is " + quizAverage +"\n " + "Overall Numerical grade is " +
                    String.valueOf(overallNumericalGrade).replaceAll(".", "*")+" / "+ "Student letter Grade is " + grade);

重要的部分在这里

String.valueOf(overallNumericalGrade).replaceAll(".", "*")

我将总值转换为String,然后用*替换所有内容。

如果总数不是overallNumericalGrade,那么只需使用替换值即可。

修改:要回答您的评论,请在此处进行更多解释。

String.replaceAll是一种方法,可以替换与我们作为第二个参数(在这种情况下为Regular expression(regex))给出的给定*相对应的所有内容。我给的正则表达式只是一个.,这意味着除了换行符之外的任何字符。因此,String.valueOf(overallNumericalGrade)会将其转换为String,因此我可以调用replaceAll方法,然后使用*替换所有内容。

至于你的第二个问题的答案,你可以再次使用替换来用linebreak替换grade一个简单的空格,以便它显示在同一行上。像

这样的东西
grade.replaceAll(System.getProperty("line.separator"), " ");

请注意,我使用System.getProperty("line.separator")代替char,因此它适用于任何系统。