需要从进入扫描仪并打印它的对象/方法调用

时间:2014-12-17 18:44:47

标签: java

我需要能够参加“姓名年龄学校GPA和最高级别的成绩”。但是,我在执行这个看似简单的任务时遇到了困难。请帮忙!!

package schoolinfo;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class SchoolInfo {
void stu(String name, String
        age, String school, double gpa, char letter){

    System.out.println("You, " + name +", are " + age+ " years old and go to " + school + " Where you have a gpa of " + gpa + "and have recieved a highest letter grade of " + letter);

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    // TODO code application logic here
    SchoolInfo student = new SchoolInfo();
    Scanner scan = new Scanner(System.in);
    boolean onoff = true;

    while(onoff){
          String countAge = " ";
    String countGPA = " ";
    String countName = " ", countSchool = " ";
    String countLetter = " ";
    String first;
    first = scan.nextLine();
        if(first.equals("quit")){
            break;
        }


        first = scan.nextLine();
       countName = first.substring(0, first.indexOf(" ", 2));
       countAge = first.substring(first.indexOf(" ", 2), first.indexOf(" ", 2));
       countSchool = first.substring(first.indexOf(" ", first.indexOf(" ", 2)), first.indexOf(" ", 2));
        countGPA = scan.nextDouble();
       countLetter = scan.next().charAt(first.lastIndexOf(" ") + 1);           
      System.out.println("test");
        student.stu(countName, countAge, countSchool, countGPA, countLetter);

    }
}

}

我想我需要添加更多详细信息来发布此内容,但请帮助! TIA !!

3 个答案:

答案 0 :(得分:1)

您传递给stu的参数与您在课程中声明的参数不匹配。

void stu(String name, String
        age, String school, double gpa, char letter)

在您的主要内容中,您已将变量声明为String

String countAge = " ";
String countGPA = " ";
String countName = " ", countSchool = " ";
String countLetter = " ";

但是您尝试将countGPA的输入检索为双精度。

countGPA = scan.nextDouble();

您可能应该将countGPA声明为double并将countLetter投射到char或更改类方法以将其接受为String

答案 1 :(得分:1)

我认为你犯了一个小错误,因为你使用

countGPA = scan.nextDouble();

带字符串值,此方法读取双值

因此您必须将“countGPA”的类型更改为double而不是String

double countGPA ;

并使用

countLetter = scan.next().charAt(first.lastIndexOf(" ") + 1);

也使用字符串值,因此您需要将“countLetter”的类型更改为char而不是String

char countLetter;

我认为它会按你的意愿运作

答案 2 :(得分:0)

谢谢你们。此代码最终正常工作



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package schoolinfo;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author jpowell1225
 */
public class SchoolInfo {
    void stu(String name, int
            age, String school, double gpa, char letter){
     //   System.out.println("test");
        System.out.println("You, " + name +", are " + age+ " years old and go to " + school + " where you have a gpa of " + gpa + " and have recieved a highest letter grade of " + letter);
        
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        SchoolInfo student = new SchoolInfo();
        Scanner scan = new Scanner(System.in);
        boolean onoff = true;
      
        while(onoff){
              int countAge = 0;
        double countGPA = 0.0;
        String countName = " ", countSchool = " ";
        char countLetter = 'a';
        String first;
        first = scan.next();
            if(first.equals("quit")){
                break;
            }
            
            
            //first = scan.nextLine();
           countName = first;//.substring(0, first.indexOf(" ", 2));
           countAge = scan.nextInt();
           countSchool = scan.next();
            countGPA = scan.nextDouble();
           countLetter = scan.next().charAt(first.lastIndexOf(" ") + 1);           
        //  System.out.println("test");
            student.stu(countName, countAge, countSchool, countGPA, countLetter);
      
        }
    }
    
}