如何将输入作为字符串接收,并转换为存储在单独类中的整数?

huangapple go评论72阅读模式
英文:

How can I take input as a string and convert to an integer stored in a seperate class?

问题

import java.util.Scanner;

class Courses {
    static public String letterGrade;
    static public int numberGrade;
    static public int creditValue;
    static public float GPA;
}

public class GradeCruncher {
    static Scanner userinput = new Scanner(System.in);

    public static void main(String[] args) {
        int numOfCourses;
        int courseNum = 0;
        System.out.println("How many courses would you like to input?");
        numOfCourses = userinput.nextInt();

        Courses[] course = new Courses[numOfCourses];
        while (courseNum < numOfCourses) {
            System.out.println("Enter info for course " + courseNum);
            System.out.println("How many credits is this course worth?");
            course[courseNum].creditValue = userinput.nextInt();
            System.out.println("What is your current letter grade for this course?");
            userinput.next();
            course[courseNum].letterGrade = userinput.nextLine();
            String aPlus = "a+", a = "a", aMinus = "a-", bPlus = "b+", b = "b", bMinus = "b-", cPlus = "c+",
                    c = "c", cMinus = "c-", dPlus = "d+", d = "d", dMinus = "d-", f = "f";
            if (f.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 0;
            } else if (dMinus.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 60;
            } else if (d.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 63;
            } else if (dPlus.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 67;
            } else if (cMinus.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 70;
            } else if (c.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 73;
            } else if (cPlus.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 77;
            } else if (bMinus.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 80;
            } else if (b.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 83;
            } else if (bPlus.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 87;
            } else if (aMinus.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 90;
            } else if (a.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 93;
            } else if (aPlus.equalsIgnoreCase(Courses.letterGrade)) {
                course[courseNum].numberGrade = 100;
            }
            System.out.println(course[courseNum].numberGrade);
            courseNum++;
            continue;
        }
        System.out.println(course[courseNum].numberGrade);
        Courses.GPA = ((course[0].numberGrade * course[0].creditValue) + (course[1].numberGrade * course[1].creditValue)) / (course[0].creditValue + course[1].creditValue);
        System.out.println(Courses.GPA);
    }
}
英文:

I am trying to have the user input a letter grade and then convert it to an integer that is stored in a separate public class. I used several if loops to try to make this happen, but I can't get it to work. Any ideas what I am doing wrong and how to fix or work around it? When I run the code how it is now, all I can get for output is 0. I have a feeling that this is probably because I am trying to store the value in a separate class, but I don't know how to make it work.

import java.util.Scanner;
class Courses {
static public String letterGrade;
static public int numberGrade;
static public int creditValue;
static public float GPA;
}
public class GradeCruncher {
static Scanner userinput = new Scanner(System.in);
public static void main(String[] args) {
int numOfCourses;
int courseNum = 0;
System.out.println(&quot;How many courses would you like to input?&quot;);
numOfCourses = userinput.nextInt();
Courses[] course = new Courses[numOfCourses];
while (courseNum &lt; numOfCourses) {
System.out.println(&quot;Enter info for course &quot; + courseNum);
System.out.println(&quot;How many credits is this course worth?&quot;);
course[courseNum].creditValue = userinput.nextInt();
System.out.println(&quot;What is your current letter grade for this course?&quot;);
userinput.next();
course[courseNum].letterGrade = userinput.nextLine();
String aPlus = &quot;a+&quot;, a = &quot;a&quot;, aMinus = &quot;a-&quot;, bPlus = &quot;b+&quot;, b = &quot;b&quot;, bMinus = &quot;b-&quot;, cPlus =          &quot;c+&quot;, c = &quot;c&quot;,
cMinus = &quot;c-&quot;, dPlus = &quot;d+&quot;, d = &quot;d&quot;, dMinus = &quot;d-&quot;, f = &quot;f&quot;;
if (f.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 0;
}
else if (dMinus.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 60;
}
else if (d.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 63;
}
else if (dPlus.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 67; 
}
else if (cMinus.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 70;
}
else if (c.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 73;
}
else if (cPlus.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 77;
}
else if (bMinus.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 80;
}
else if (b.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 83;
}
else if (bPlus.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 87;
}
else if (aMinus.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 90;
}
else if (a.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 93;
}
else if (aPlus.equalsIgnoreCase(Courses.letterGrade)) {
course[courseNum].numberGrade = 100;
}
System.out.println(course[courseNum].numberGrade);
courseNum++;
continue;
}
System.out.println(course[courseNum].numberGrade);
Courses.GPA = ((course[0].numberGrade * course[0].creditValue) + (course[1].numberGrade *     course[1].creditValue)) / (course[0].creditValue + course[1].creditValue);
System.out.println(Courses.GPA);
}
}

答案1

得分: 0

(f.equalsIgnoreCase(Courses.letterGrade)) ----- 是错误的。

  • 用 Course[coursenum].lettergrade 替换 Courses.letterGrade
  • 移除 continue,不需要那个。
  • 更好的设计是使用 switch 语句。
  • Courses 是一个类,不是一个对象,不能在其中存储。你已经创建了一个类型为 Courses 的对象 'courses',你可以在其中存储。
  • 将 Courses.Gpa 替换为整数变量。
英文:

(f.equalsIgnoreCase(Courses.letterGrade)) ----- is wrong.

  • Replace Courses.letterGrade with Course[coursenum].lettergrade
  • remove continue,no need of that .
  • better design is to use switch statement.
  • Courses is a Class not a object u cannot store in it. You have created a object of type Courses 'courses' in this u can store.
    replace Courses.Gpa to a integer variable.

答案2

得分: 0

我在你的代码中找到了一些问题,除了评论中已经提到的事情:

  • 虽然你创建了一个 Courses 数组,但实际上你从未创建任何要添加到数组中的 Courses 对象。当数组中没有 Courses 时,也无法赋值给你的变量。
  • 你应该使用一个 Map 来避免使用这些多余的 if 语句。
  • 你的类命名有误导性。数组应该称为 courses,类应该称为 Course

我尝试重新构建了你的代码,以便我能够理解并按照我认为的方式使其正常工作。

import java.util.HashMap;
import java.util.Scanner;

class Course {
    public String letterGrade;
    public int numberGrade;
    public int creditValue;

    Course(int credits, int gradeNum, String gradeLet) {
        letterGrade = gradeLet;
        numberGrade = gradeNum;
        creditValue = credits;
    }
}

public class GradeCruncher {
    static Scanner userInput = new Scanner(System.in);
    static HashMap<String, Integer> gradeMap = getGradeMap(); 

    public static void main(String[] args) {
        int numOfCourses;
        int courseNum = 0;
        System.out.println("How many courses would you like to input?");
        numOfCourses = userInput.nextInt();

        Course[] courses = new Course[numOfCourses];
        for (; courseNum < numOfCourses; courseNum++) {
            System.out.println("Enter info for course " + courseNum);
            System.out.println("How many credits is this course worth?");
            int tempCredits = userInput.nextInt();
            System.out.println("Credits is " + tempCredits);
            System.out.println("What is your current letter grade for this course?");
            userInput.nextLine();
            String tempLetterGrade = userInput.nextLine();
            System.out.println("Letter grade is " + tempLetterGrade);
            int tempNumberGrade = gradeMap.get(tempLetterGrade);
            courses[courseNum] = new Course(tempCredits, tempNumberGrade, tempLetterGrade);
        }
        double gpa = 0.0;
        int overallCreditValue = 0;
        for (int i = 0; i < courses.length; i++) {
            gpa += (courses[i].numberGrade * courses[i].creditValue);
            overallCreditValue += courses[i].creditValue;
        }
        gpa = gpa / overallCreditValue;
        System.out.println("GPA Calculated: " + gpa);
    }

    private static HashMap<String, Integer> getGradeMap() {
        HashMap<String, Integer> gradeMap = new HashMap<String, Integer>();
        gradeMap.put("a+", 100);
        gradeMap.put("a", 93);
        gradeMap.put("a-", 90);
        gradeMap.put("b+", 87);
        gradeMap.put("b", 83);
        gradeMap.put("b-", 80);
        gradeMap.put("c+", 77);
        gradeMap.put("c", 73);
        gradeMap.put("c-", 70);
        gradeMap.put("d+", 67);
        gradeMap.put("d", 63);
        gradeMap.put("d-", 60);
        gradeMap.put("f", 0);
        return gradeMap;
    }
}

解释:

字母和数字成绩映射在一个映射中。Course 类现在被重构,以便通过构造函数输入内容。首先,为每门课程从用户那里检索所有输入,并临时保存。当检索到所有课程信息时,使用这些信息创建 Course 对象,然后将其存储在 courses 数组中。当所有课程都创建好后,按照你的方式计算平均值并将其打印到输出中。

输入和输出:

How many courses would you like to input?
2
Enter info for course 0
How many credits is this course worth?
5
What is your current letter grade for this course?
a+
Enter info for course 1
How many credits is this course worth?
5
What is your current letter grade for this course?
a
GPA Calculated: 96.5
英文:

I have found several issues in your code, besides the things already stated in the comments:

  • Although you create an array of Courses, you never actually create any Courses objects, which you add to the array. When there are no Courses in your array, you also cannot assign your variables.
  • You should use a Map to avoid having to use all of these ifs.
  • The naming of your class is misleading. The array should be called courses and the class should be called Course.

I tried to rebuild your code so that it makes sense to me and works as I think it should.

import java.util.HashMap;
import java.util.Scanner;
class Course {
public String letterGrade;
public int numberGrade;
public int creditValue;
//public float GPA; // this should not be part of your Course class
Course(int credits, int gradeNum, String gradeLet) {
letterGrade = gradeLet;
numberGrade = gradeNum;
creditValue = credits;
}
}
public class GradeCruncher {
static Scanner userInput = new Scanner(System.in);
static HashMap&lt;String, Integer&gt; gradeMap = getGradeMap(); 
public static void main(String[] args) {
int numOfCourses;
int courseNum = 0;
System.out.println(&quot;How many courses would you like to input?&quot;);
numOfCourses = userInput.nextInt();
// create the array to store the courses
Course[] courses = new Course[numOfCourses];
// loop to get all courses
for (; courseNum &lt; numOfCourses; courseNum++) {
System.out.println(&quot;Enter info for course &quot; + courseNum);
System.out.println(&quot;How many credits is this course worth?&quot;);
// Get the credits for this course from input and save it temporarily
int tempCredits = userInput.nextInt();
System.out.println(&quot;Credits is &quot; + tempCredits);
System.out.println(&quot;What is your current letter grade for this course?&quot;);
// this is needed so that the scanner removes the newline (\n) from the input
userInput.nextLine();
// Get the letter grade for this course from input and save it temporarily
String tempLetterGrade = userInput.nextLine();
System.out.println(&quot;Letter grade is &quot; + tempLetterGrade);
// Retrieve the number grade from the map and save it
int tempNumberGrade = gradeMap.get(tempLetterGrade);
// Create the Course object with the input values and save it in the courses array
courses[courseNum] = new Course(tempCredits, tempNumberGrade, tempLetterGrade);
}
// All courses built
// Calculate GPA
double gpa = 0.0;
int overallCreditValue = 0;
for (int i = 0; i &lt; courses.length; i++) {
gpa += (courses[i].numberGrade * courses[i].creditValue);
overallCreditValue += courses[i].creditValue;
}
gpa = gpa / overallCreditValue;
System.out.println(&quot;GPA Calculated: &quot; + gpa);
}
private static HashMap&lt;String, Integer&gt; getGradeMap() {
HashMap&lt;String, Integer&gt; gradeMap = new HashMap&lt;String, Integer&gt;();
gradeMap.put(&quot;a+&quot;, 100);
gradeMap.put(&quot;a&quot;, 93);
gradeMap.put(&quot;a-&quot;, 90);
gradeMap.put(&quot;b+&quot;, 87);
gradeMap.put(&quot;b&quot;, 83);
gradeMap.put(&quot;b-&quot;, 80);
gradeMap.put(&quot;c+&quot;, 77);
gradeMap.put(&quot;c&quot;, 73);
gradeMap.put(&quot;c-&quot;, 70);
gradeMap.put(&quot;d+&quot;, 67);
gradeMap.put(&quot;d&quot;, 63);
gradeMap.put(&quot;d-&quot;, 60);
gradeMap.put(&quot;f&quot;, 0);
return gradeMap;
}
}

Explanation:

The letter and number grades are mapped in a map. The Course Class is now structured, so that the contents are input via the constructor. First all the inputs are retrieved from the user for each course and stored temporarily. When all the course info is retrieved, the Course object is created with this information and then stored in the courses array. When all courses are created, the average is calculated (according to the way you did it) and printed to the output.

Input and output:

How many courses would you like to input?
2
Enter info for course 0
How many credits is this course worth?
5
What is your current letter grade for this course?
a+
Enter info for course 1
How many credits is this course worth?
5
What is your current letter grade for this course?
a
GPA Calculated: 96.5

huangapple
  • 本文由 发表于 2020年10月23日 14:27:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64494931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定