2017-09-01 59 views
-3

所以即时通讯非常新的Java和创造的东西学校。 我的问题是最后一个If语句我做了比较字符串,我明白!=或> = does not工作,但我不明白用什么来代替它。任何帮助?遇到了一个问题,我的Java程序

我试过查找正确的方式来使用该行,但我只是没有真正明白什么时候比较这两个字母的确切人人都说。

package Secrets_hw2p1; 

/** 
* 
* @author secrets 
    */ 
    //importing Scanner 
    //import scanner 
    import java.util.Scanner; 
     public class secrets_hw2p1 { 

    /** 
    * @param args the command line arguments 
    */ 
     public static void main(String[] args) { 
     // TODO code application logic here 

    //Creating the Scanner object 
    Scanner input = new Scanner (System.in); 
    //getting students goal grade 
    System.out.println("What is your Goal Letter Grade? "); 
    String goalGrade = input.next(); 

    //Enter assignment scores. and read scores 
    System.out.println("Please enter in your two assignment scores followed" 
    +"by your two exam scores one at a time followed by enter "); 
    float assignment1 = input.nextFloat(); 
    float assignment2 = input.nextFloat(); 
    float exam1 = input.nextFloat(); 
    float exam2 = input.nextFloat(); 

    //calculations of averages 
    double goal = assignment1 * .40 + assignment2 * .40 + exam1 * .30 + 
      exam2 * 0.30; 
    int grade; 
    //Calculate Letter grade 
    if (goal >= 90) 
     grade = 'A'; 
    else if (goal >= 80) 
     grade = 'B'; 
    else if (goal >= 70) 
     grade = 'C'; 
    else 
     grade = 'D'; 



    //prompt the user for how they want there grade 
    System.out.println("Press 1 to display letter grade or press 2 to" 
    +"see if you met your goal "); 
    double number = input.nextDouble(); 

    //if user inputed 1 
    if (number == 1) 
     System.out.println ("Final grade:" + grade); 
    //if user inputed 2  
    if (number == 2) 
     if (goalGrade != grade) 
       System.out.println("You have not met or exceeded your goal" 
         +" grade"); 
     else if (c1.goalGrade >= grade) 
       System.out.println("You met or exceded your goal grade !"); 



    } 

} 
+0

'grade'应该是一个'String'。你应该使用'equals()'比较字符串。 – shmosel

+1

请注意''遇到了我的java程序问题''是一个StackOverflow问题的糟糕标题,因为它告诉我们什么都没用。请考虑使用更多的信息性问题标题,这些标题总结了您的实际问题,因为这样做会帮助您获得更好的帮助。欲了解更多信息,请浏览[游览],[帮助]和[如何提出一个好问题](http://stackoverflow.com/help/how-to-ask)部分,了解这个网站工作并帮助您改善当前和未来的问题,这可以帮助您获得更好的答案。 –

+0

@shmosel或者可能使用'char',但绝对不是用于存储字符文字的'int'。 –

回答

0

使用equals()方法来代替,改变grade类型String

String grade = ""; // changed datatype to String 

然后:

//if user inputed 2  
      if (number == 2) 
       if (goalGrade.equals(grade)) 
         System.out.println("You have not met or exceeded your goal" 
           +" grade"); 

阅读:https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)

+0

如果您已将'grade'改为'String',那么您应该说明您的答案。采取逐字,你的答案也将失败。 –

+0

@TimBiegeleisen完成。 –

0

因为字符串是对象,你不能使用你的标准=或< =你将需要使用.equals方法。因此,而不是

if (goalGrade != grade) 

你想

if (!goalGrade.equals(grade)) 
+0

谢谢!我很欣赏它! –

相关问题