扫描字符串然后将其与整数一起打印

时间:2015-07-27 05:15:10

标签: java

如果输入是 Mandy 99 99 97 95 92 90 98 而输出需要 曼迪A. 我到目前为止的代码是:

package utility;
import java.util.Scanner;
public class LetterGrader {



    public static void main(String[] args) {
            String Name = new String();
            int quiz1;
            int quiz2;
            int quiz3;
            int quiz4;
            int mid1;
            int mid2;
            int Final;
            double grade;
            Scanner getScores = new Scanner (System.in);
            Scanner getName = new Scanner(System.
            System.out.println("Please enter name with test scores with a space in between each, in the order of "
                    + "Quiz 1 Quiz 2 Quiz 3 Quiz 4\n Midterm 1 Midterm 2 and Final\n");//Shows the format needed
            Scanner scanner = new Scanner(System.in);
           Name = getName.next();
           quiz1 = getScores.nextInt();
           quiz2 = getScores.nextInt();
           quiz3 = getScores.nextInt();
           quiz4 = getScores.nextInt();
           mid1 = getScores.nextInt();
           mid2 = getScores.nextInt();
           Final = getScores.nextInt();

            grade =( quiz1  *   .10 +   quiz2   *   .10 +   quiz3   *   .10 +   quiz4   *   .10 
                   +    mid1    *   .20 +   mid2    *   .15 +   Final * .25);
            System.out.print(Name);
            if  (grade >= 90)
                System.out.println("A");
            if (grade >= 80 && grade< 90)
                System.out.println("B");
            if (grade >= 70 && grade< 80)
                System.out.println("C");
            if (grade >= 60 && grade< 70)
                System.out.println("D");
            if  (grade <= 59)
                System.out.println("F");

        }}

我如何扫描用户输入的名称,然后用数字打印? 任何帮助都很棒。

2 个答案:

答案 0 :(得分:1)

首先要做的事情......

  1. Scanner getName = new Scanner(System.作为代码不正确......
  2. 不需要那么多扫描仪......只需一台扫描仪即可完成所有这些工作。
  3. 使用后始终关闭资源......这是一个很好的做法......
  4. 同时拥有太多if需要花费时间执行...在这种类型的代码中无关紧要,但使用if()阶梯而不是使用太多if-else
  5. 完整代码....

    public static void main(String[] args) {
             String Name = new String();
             int quiz1;
             int quiz2;
             int quiz3;
             int quiz4;
             int mid1;
             int mid2;
             int Final;
             double grade;
    
             Scanner getScores = new Scanner(System.in);
             System.out.println("Please enter name with test scores with a space in between each, in the order of "
                     + "Quiz 1 Quiz 2 Quiz 3 Quiz 4\n Midterm 1 Midterm 2 and Final\n");//Shows the format needed
            Name = getScores.next();
            quiz1 = getScores.nextInt();
            quiz2 = getScores.nextInt();
            quiz3 = getScores.nextInt();
            quiz4 = getScores.nextInt();
            mid1 = getScores.nextInt();
            mid2 = getScores.nextInt();
            Final = getScores.nextInt();
    
             grade =( quiz1  *   .10 +   quiz2   *   .10 +   quiz3   *   .10 +   quiz4   *   .10 
                    +    mid1    *   .20 +   mid2    *   .15 +   Final * .25);
             System.out.print(Name);
             if  (grade >= 90)
                 System.out.println("A");
           else  if (grade >= 80 && grade< 90)
                 System.out.println("B");
            else if (grade >= 70 && grade< 80)
                 System.out.println("C");
            else if (grade >= 60 && grade< 70)
                 System.out.println("D");
            else  (grade <= 59)
                 System.out.println("F");
    
     getScores.close();
    
         }
    

答案 1 :(得分:1)

你应该阅读整行并将其拆分成碎片。

public static void main(String[] args) {
         String Name = new String();
         int quiz1;
         int quiz2;
         int quiz3;
         int quiz4;
         int mid1;
         int mid2;
         int Final;
         double grade;

         Scanner getScores = new Scanner(System.in);
         System.out.println("Please enter name with test scores with a space in between each, in the order of "
                 + "Quiz 1 Quiz 2 Quiz 3 Quiz 4\n Midterm 1 Midterm 2 and Final\n");//Shows the format needed
        String[] inputs = getScores.readLine ().Split (" ");
        Name = inputs[0]
        quiz1 = inputs[1]
        quiz2 = inputs[2]
        quiz3 = inputs[3]
        quiz4 = inputs[4]
        mid1 = inputs[5]
        mid2 = inputs[6]
        Final = inputs[7]

         grade =( quiz1  *   .10 +   quiz2   *   .10 +   quiz3   *   .10 +   quiz4   *   .10 
                +    mid1    *   .20 +   mid2    *   .15 +   Final * .25);
         System.out.print(Name);
         if  (grade >= 90)
             System.out.println("A");
         if (grade >= 80 && grade< 90)
             System.out.println("B");
         if (grade >= 70 && grade< 80)
             System.out.println("C");
         if (grade >= 60 && grade< 70)
             System.out.println("D");
         if  (grade <= 59)
             System.out.println("F");

 getScores.close();

     }