-2

我真的是尝试学习代码的初学者,根据我阅读的文献,我创建了以下程序。这段代码的问题是我输入的任何数字都会返回 F 级。如果我删除 System.in.read() 并设置永久值,那么它就可以正常工作。有人猜我的代码有什么问题吗?

class ladder2 {    
  public static void main(String args[])  
    throws java.io.IOException {

    char read, grade;
    // read = 75;   

    System.out.println("Enter a score between 0 and 100: ");
    read = (char) System.in.read();

    if (100 <= read) grade = 'A'; 
    else if (85 <= read) grade = 'B'; 
    else if (75 <= read) grade = 'C';
    else if (60 <= read) grade = 'D';
    else grade = 'F';

    System.out.println("GRADE " + grade);   
  }
}
4

3 回答 3

2

使用BufferedReader,你的评分逻辑也不正确

import java.io.BufferedReader;
import java.io.InputStreamReader;

class del {    
   public static void main(String args[]) throws java.io.IOException {

int read;char  grade;
 read = 75;   

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a score between 0 and 100: ");
read = Integer.parseInt(br.readLine());

if (100 >= read && read >85) grade = 'A'; 
else if (85 >= read && read >75) grade = 'B'; 
else if (75 >= read && read >60) grade = 'C';
else if (60 >= read && read >50) grade = 'D'; // you may want to change 50
else grade = 'F';
        System.out.println("GRADE " + grade);   
    }
}
于 2013-11-02T08:08:21.943 回答
1

read()返回第一个输入符号的字符码。

但是您需要读取要解析为 int 的整数符号。

于 2013-11-02T08:02:21.410 回答
1

此示例使用嵌套 if。

package botball;
import java.util.Scanner;

public class bots {
  public static void main(String[] args){
    int a;
    Scanner Cin = new Scanner (System.in);
    a = Cin.nextInt();

    if (a<=100);{
      if (a>90)
        JOptionPane.showMessageDialog(null,"A1");
      else {
        if (a>80)
          JOptionPane.showMessageDialog(null,"A2");
        else { 
          if (a>70)
            JOptionPane.showMessageDialog(null,"B1");
          else {
            if (a>60)
              JOptionPane.showMessageDialog(null,"B2");
            else {
              if (a>50)
                JOptionPane.showMessageDialog(null,"C1");
              else {
                if (a>40)
                  JOptionPane.showMessageDialog(null,"C2");
                else {
                  if (a>30)
                    JOptionPane.showMessageDialog(null,"D");
                  else {
                    if (a>20)
                      JOptionPane.showMessageDialog(null,"E1");
                    else
                      JOptionPane.showMessageDialog(null,"E2");
                  }
                }
              }
            }
          }
        }
      }
    }
于 2016-02-03T16:20:04.833 回答