使用分隔符将Scanner输入分成不同的数据类型。(String,int,

时间:2017-03-01 20:46:59

标签: delimiter

我如何使用分隔符" :"接收来自(system.in)的字符串例如Ben:50。Jessica:30等等,然后打印为Ben,50使用我自己的System.out.print(名称+"," +得分了);我能够将字符串打印为字符串,但不会将整数打印出整数。我需要计算所有分数的平均值,这就是为什么我需要整数保持整数所以平均方法可以工作这是我的代码。到目前为止,这是我的代码。请帮忙!!!

-------------------------------------------------------------------------
import java.util.Scanner;
public class StudentScore{
  public static void main(String [] args){
    Scanner input = new Scanner(System.in);
  input.useDelimiter(":"); 
    System.out.print("Please enter your name and score in format: Ben : 50" );

String name = input.next();
int score = input.nextInt();
while(input.hasNext() || input.hasNextInt());{
System.out.println(name + ", " + score);
}

  input.close();
  }





  }

这使得新的扫描仪不再存在,也没有打印出来。

1 个答案:

答案 0 :(得分:0)

在研究并询问了几个人之后,我发现String类实际上有一个函数,它接受字符串然后将它分成几个原始值"长"," INT"等等。您需要将输入转换为数组,确保它由选择的分隔符进行吐出.e.g","或":"在我自己的情况下。然后,数组将成为String类中.split方法的参数。我在下面发布了完整的程序。这些课程有三个名字,得分并输出成绩单,然后告诉三个学生最高分。

import java.util.Scanner;

public class ThreeNamesAndScore {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter your name and score in this format, Name : Score");
        String input1 = input.nextLine();
        String[] userData1 = input1.split(":");
        String firstStudentName = userData1[0];
        String firstStudentStrScore = userData1[1];
        int firstStudentScore = Integer.parseInt(firstStudentStrScore);
        System.out.print("Please enter your name and score in this format, Name : Score");

        String input2 = input.nextLine();
        String[] userData2 = input2.split(":");
        String secondStudentName = userData2[0];
        String secondStudentStrScore = userData2[1];
        int secondStudentScore = Integer.parseInt(secondStudentStrScore);
        System.out.print("Please enter your name and score in this format, Name : Score");

        String input3 = input.nextLine();
        String[] userData3 = input3.split(":");
        String thirdStudentName = userData3[0];
        String thirdStudentStrScore = userData3[1];
        int thirdStudentScore = Integer.parseInt(thirdStudentStrScore);

        System.out.println("The following is the fianl result ");
        System.out.println(firstStudentName + ", " + getGradeLetter(firstStudentScore));
        System.out.println(secondStudentName + ", " + getGradeLetter(secondStudentScore));
        System.out.println(thirdStudentName + ", " + getGradeLetter(thirdStudentScore));

        System.out.println("The best score is from " + bestScore(input1, input2, input3));
        input.close();
    }

    public static String getGradeLetter(int score) {

        if (score >= 90) {
            return "A";
        } else if (score >= 80) {
            return "B";
        } else if (score >= 70) {
            return "C";
        } else if (score >= 60) {
            return "D";
        } else
            return "F";

    }

    public static String bestScore(String a, String b, String c) {
        String bestStudent;
        String[] userInputs = { a, b, c };
        String[] user1 = a.split(":");

        String aStrScore = user1[1];
        int aScore = Integer.parseInt(aStrScore);
        String[] user2 = b.split(":");

        String bStrScore = user2[1];
        int bScore = Integer.parseInt(bStrScore);
        String[] user3 = c.split(":");

        String cStrScore = user3[1];
        int cScore = Integer.parseInt(cStrScore);

        if ((aScore > bScore) && (aScore > cScore))
            bestStudent = a;
        else if ((bScore > aScore) && (bScore > cScore))
            bestStudent = b;
        else
            bestStudent = c;

        return bestStudent;
    }
}