java中的结束程序使用字母“将字符串转换为int”退出程序;";
我有一个评分程序,我正在尝试很多东西,以退出程序使用键盘输入,在这种情况下,字母“E”。我创建了另一个方法,但我无法使它与主方法一起工作,我是如此的新,以至于我被卡住了。我尝试使用布尔值和字符串到整数的转换,但得到的只是错误。下面是我的代码,提前谢谢你,我知道这可能是个愚蠢的问题java中的结束程序使用字母“将字符串转换为int”退出程序;";,java,for-loop,Java,For Loop,我有一个评分程序,我正在尝试很多东西,以退出程序使用键盘输入,在这种情况下,字母“E”。我创建了另一个方法,但我无法使它与主方法一起工作,我是如此的新,以至于我被卡住了。我尝试使用布尔值和字符串到整数的转换,但得到的只是错误。下面是我的代码,提前谢谢你,我知道这可能是个愚蠢的问题 import java.util.Scanner; public class grading { public static void main (String args[]){ boolea
import java.util.Scanner;
public class grading {
public static void main (String args[]){
boolean run = true;//this code together with bottom keep loop running
while(run){ //the code inside this brackets will continue the loop
int yourScore;
Scanner input = new Scanner(System.in);
System.out.println("Enter your Score: ");
yourScore = input.nextInt();
boolean kill;
System.out.println("Press E to Exit");
Scanner input0 = new Scanner(System.in);
if (yourScore < 60 && yourScore >=0){
System.out.println("You had an F, Sorry!");
}
else if (yourScore >=60 && yourScore < 70){
System.out.println("You had an D, Study more!");
}
else if (yourScore >=70 && yourScore < 80){
System.out.println("you had an C, you can do Better!");
}
else if (yourScore >= 80 && yourScore < 90){
System.out.println("You had an B, very Well done!");
}
else if ( yourScore >=90 && yourScore <= 100){
System.out.println("you had an A, you are Great!");
}
else if (kill = "e" != null){
System.out.println("Bye!");
Runtime.getRuntime().exit(0);
}
else
System.out.println("Incorrect Input!");
}
}
}
import java.util.Scanner;
公开课评分{
公共静态void main(字符串参数[]){
boolean run=true;//此代码与底部保持循环一起运行
while(run){//此括号内的代码将继续循环
积分;
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入您的分数:”);
yourScore=input.nextInt();
布尔杀死;
System.out.println(“按E键退出”);
扫描仪输入0=新扫描仪(System.in);
如果(yourScore<60&&yourScore>=0){
System.out.println(“你得了F,对不起!”);
}
否则,如果(你的分数>=60&&yourScore<70){
System.out.println(“你得了D,多学习!”);
}
否则,如果(你的分数>=70&&yourScore<80){
println(“你有一个C,你可以做得更好!”;
}
否则,如果(你的分数>=80&&yourScore<90){
System.out.println(“你得了B,做得很好!”);
}
否则,如果(yourScore>=90&&yourScore您的代码中有两个错误。首先,您允许用户同时输入数字和字母,但将输入分配给yourScore,即整数。如果用户输入字母,则您尝试将该字母分配为整数,这会引发InputMismatchException
第二,您正在初始化两个扫描仪,但从未使用第二个扫描仪(即input0)。事实上,此程序中不需要两个扫描仪。只有一个扫描仪可以完成此工作
第三,在最后一条else if语句中,您分配kill=“e”!=null。我很确定这不是您想要做的。您应该首先熟悉java的工作原理。在java中,执行是从右到左进行的。这里将首先计算表达式“e”!=null。因为“e”是一个字符串,它永远不会等于null。因此,表达式“e”!=null的计算结果将为true。然后将此true值分配给kill变量。因此,kill变量无论发生什么情况都始终为true。这不是您想要的。下面是按预期工作的代码
public class grading {
public static void main (String args[]){
boolean run = true;//this code together with bottom keep loop running
while(run){ //the code inside this brackets will continue the loop
int yourScore;
Scanner input = new Scanner(System.in);
System.out.println("Enter your Score or E to exit: ");
//checking if the input is integer or not
if(input.hasNextInt()){
yourScore = input.nextInt();
if (yourScore < 60 && yourScore >=0){
System.out.println("You had an F, Sorry!");
}
else if (yourScore >=60 && yourScore < 70){
System.out.println("You had an D, Study more!");
}
else if (yourScore >=70 && yourScore < 80){
System.out.println("you had an C, you can do Better!");
}
else if (yourScore >= 80 && yourScore < 90){
System.out.println("You had an B, very Well done!");
}
else if ( yourScore >=90 && yourScore <= 100){
System.out.println("you had an A, you are Great!");
}
else
System.out.println("Incorrect Input!");
}
// checking if the input is string
else if(input.hasNext()){
//assigning input to String varible
String userInput = input.next();
//comparing the input value with letter e ignoring the case
if(userInput.equalsIgnoreCase("e")){
run = false;
}
else{
System.out.println("Invalid key. Press e to exit");
}
}
}
}
}
公共课评分{
公共静态void main(字符串参数[]){
boolean run=true;//此代码与底部保持循环一起运行
while(run){//此括号内的代码将继续循环
积分;
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入您的分数或E退出:”;
//检查输入是否为整数
if(input.hasNextInt()){
yourScore=input.nextInt();
如果(yourScore<60&&yourScore>=0){
System.out.println(“你得了F,对不起!”);
}
否则,如果(你的分数>=60&&yourScore<70){
System.out.println(“你得了D,多学习!”);
}
否则,如果(你的分数>=70&&yourScore<80){
println(“你有一个C,你可以做得更好!”;
}
否则,如果(你的分数>=80&&yourScore<90){
System.out.println(“你得了B,做得很好!”);
}
否则如果(yourScore>=90&&yourScore(kill=“e”!=null)
应该是(kill==“e”)
。我不知道这是否是你唯一的问题,因为你没有公布你遇到的错误。@Blorgbeard绝对不应该是kill==“e”
;那只会比较参考值。而且,kill
是一个布尔值
。哦,我的Java比我想象的更粗糙。没有注意到kill
也是布尔值。忽略我。=
用于赋值,而不是比较,kill
是布尔值,因此只能等于true
或false
,并且您也从不给kill
赋值,也不给用户机会输入“e”
。为什么不break;
或设置run=false;
?哇,谢谢你的回答,我知道我做错了什么,我可以用一种不那么复杂的方式写这篇文章。很抱歉没有说明我的运行错误……问题主要是没有接受字母E或E作为结束程序的字符串,我想我是个新手t我想建造两台扫描仪就可以逃脱了,哈哈,但是特别感谢大家@Bijay Regmi