为什么我运行程序时会显示代码中的所有字母等级选项?我只希望显示正确答案

时间:2015-09-12 17:08:31

标签: java

我对Java编程非常陌生,并被告知这是一个很好的网站,可以帮助我朝着正确的方向发展。

为什么我运行程序时会显示代码中的所有字母等级选项?我只想要显示正确的一个。请帮忙。

此外,当输入时,如何让数字值显示在同一行而不返回到下一行?

感谢您对Java新手的任何帮助和见解。

/*
 * 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 assignment_1;

/**
 *
 * @author 
 */

import java.util.Scanner;
public class Assignment_1 
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        System.out.println("Welcome to the Letter Grade Convertor Program");
        System.out.println();

        Scanner sc = new Scanner(System.in);

        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {

            System.out.println("Enter numerical grade: ");
            int grade = sc.nextInt();
            char letter = 0;
            if(grade <= 100 || grade >= 90)
                letter = 'A';
                System.out.println("Letter grade: " + letter);
            if (grade <= 89 || grade >= 80)
                letter = 'B';
                System.out.println("Letter grade: " + letter);

            if (grade <= 79 || grade >= 70)
                letter = 'C';
                System.out.println("Letter grade: " + letter);

            if(grade <= 69 || grade >= 60)
                letter = 'D';
                System.out.println("Letter grade: " + letter);

            if(grade < 60 || grade <=0)
                letter = 'F';
                System.out.println("Letter grade: " + letter);


            System.out.println("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
        }
    }
}

5 个答案:

答案 0 :(得分:4)

如果您有多个语句,则应添加大括号。如果你留下括号,只有第一行属于if条件。所以sout代码总是被执行。当你添加大括号时,它是一个块,它内部的所有行都属于if条件。

if(grade <= 100 || grade >= 90){
     letter = 'A';
     System.out.println("Letter grade: " + letter);
}

总是添加花括号

是多么好的做法

然而,正如@dici建议您可以在最后打印成绩而不是在所有条件

中添加out.print 像这样

public static void main(String[] args) {
    System.out.println("Welcome to the Letter Grade Convertor Program");
    System.out.println();

    Scanner sc = new Scanner(System.in);

    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {

        System.out.println("Enter numerical grade: ");
        int grade = sc.nextInt();
        char letter = 0;
        if (grade <= 100 || grade >= 90) {
            letter = 'A';
        }

        if (grade <= 89 || grade >= 80) {
            letter = 'B';
        }

        if (grade <= 79 || grade >= 70) {
            letter = 'C';
        }

        if (grade <= 69 || grade >= 60) {
            letter = 'D';
        }

        if (grade < 60 || grade <= 0) {
            letter = 'F';
        }
        System.out.println("Letter grade: " + letter);

        System.out.println("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();
    }
}

答案 1 :(得分:1)

使用if-else if block for exclusive condition:

if (grade <= 100 || grade >= 90) {
     letter = 'A';
     System.out.println("Letter grade: " + letter);
} else if (grade <= 89 || grade >= 80) {
     letter = 'B';
     System.out.println("Letter grade: " + letter);
} else if (grade <= 79 || grade >= 70) {
     letter = 'C';
     System.out.println("Letter grade: " + letter);
} else if (grade <= 69 || grade >= 60) {
     letter = 'D';
     System.out.println("Letter grade: " + letter);
} else if(grade < 60 || grade <= 0) {
     letter = 'F';
     System.out.println("Letter grade: " + letter);
}

这些条件可以优化如下:

if (grade <= 100 || grade >= 90) {
     letter = 'A';
     System.out.println("Letter grade: " + letter);
} else if (grade >= 80) {
     letter = 'B';
     System.out.println("Letter grade: " + letter);
} else if (grade >= 70) {
     letter = 'C';
     System.out.println("Letter grade: " + letter);
} else if (grade >= 60) {
     letter = 'D';
     System.out.println("Letter grade: " + letter);
} else if(grade >= 0) {
     letter = 'F';
     System.out.println("Letter grade: " + letter);
}

除此之外,对于Dici的建议,您可以避免重复printlns

if (grade <= 100 || grade >= 90) {
     letter = 'A';
} else if (grade >= 80) {
     letter = 'B';
} else if (grade >= 70) {
     letter = 'C';
} else if (grade >= 60) {
     letter = 'D';
} else if(grade >= 0) {
     letter = 'F';
} else {
    // If you want to check illegal grade under 0 or over 100
    // throw new RuntimeException("The grade is out of range!");
}

System.out.println("Letter grade: " + letter);

答案 2 :(得分:1)

显示字母等级是因为您没有使用括号。

在Java中,如果您不对ifwhile块使用括号,则只有其下方的行被视为附加到if / while 。你正在做的主要是:

“如果此成绩符合此条件,请将字母设置为”,“打印字母”

您应该寻找:“如果此等级符合条件,请将字母设置为此并打印字母”,这需要使用括号。

这意味着您想要的代码是:

if(grade <= 100 || grade >= 90) {
    letter = 'A';
    System.out.println("Letter grade: " + letter);
}
if (grade <= 89 || grade >= 80) {
    letter = 'B';
    System.out.println("Letter grade: " + letter);
}

if (grade <= 79 || grade >= 70) {
    letter = 'C';
    System.out.println("Letter grade: " + letter);
}

if(grade <= 69 || grade >= 60){
    letter = 'D';
    System.out.println("Letter grade: " + letter);
}

if(grade < 60 || grade <=0){
    letter = 'F';
    System.out.println("Letter grade: " + letter);
}

作为一般说明:请始终使用括号。

但是,如果您真的不想使用括号,则可以从每个部分删除System.out.println("Letter grade: " + letter);行,并在请求Continue (y/n) <之前将其放在最后一行/ p>

答案 3 :(得分:1)

每个人都已经给了你一个正确的答案,所以我不会在括号上重复这一部分。但是,我将向您展示最紧凑的方式(在一般情况下,因为如果成绩是线性的,您可以做得更短)来编写完全相同的逻辑:

int[] intervals = { 60, 70, 80, 90, 100 };
char[] grades = { 'E', 'D', 'C', 'B', 'A' };
int i = 0;

while (grade >= intervals[i] && i < intervals.length) i++;
System.out.println("Letter grade: " + grades[i]);

你也可以做一些更模糊但更短的事情:

char[] grades = { 'E', 'D', 'C', 'B', 'A' };
System.out.println("Letter grade: " + grades[Math.max(0, (grade - 50) / 10)]);

答案 4 :(得分:0)

其他人修复了语法,但很快,我想指出逻辑。

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate(Resource.Layout.BusinessList, container, false);
        businessListView = view.FindViewById<ListView>(Resource.Id.businessListView);

        _list = await GetData();

        var adapter = new BusinessListAdapter(Activity, _list);
        businessListView.Adapter = adapter;
        adapter.NotifyDataSetChanged ();

        return view;
    }

    public async Task<List<Business>> GetData(){

        var query = ParseObject.GetQuery("Business");
        IEnumerable<ParseObject> results = await query.FindAsync();
        var data = new List<Business>();

        foreach (var temp in results)
        {
            var _business = new Business();

            // This does not require a network access.
            _business.Name = temp.Get<string>("name");
            _business.Address = temp.Get<string>("address");
            _business.Town = temp.Get<string>("town");
            _business.Country = temp.Get<string>("country");
            data.Add (_business);
        }

      return data;
    }

您正在使用&#34; ||&#34;运营商是运营商。因此,程序总是会打破if else语句,因为等级总是低于100.你可以把5和它说A。不应该是&#34;&amp;&amp;&# 34;,这样它会检查你想要的范围? 如果我错过了什么,请告诉我。