我是初学者,正在尝试学习代码,根据我阅读的文献,我创建了以下程序。这段代码的问题是我输入的任何数字都返回GRADE 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);
}
}
答案 0 :(得分: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);
}
}
答案 1 :(得分:1)
read()返回第一个输入符号的charcode。
但是你需要将read the whole number个符号解析为int。
答案 2 :(得分: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");
}
}
}
}
}
}
}
}