gpt4 book ai didi

java - while 循环中的输入(扫描仪)变量

转载 作者:行者123 更新时间:2023-12-01 18:59:31 29 4
gpt4 key购买 nike

我试图让程序询问百分比(等级),但我希望它在用户进行第一个输入并看到输出后再次询问。我在循环中遇到问题,因为未分配变量 myMark。

import java.util.Scanner;
public class passFail{


public static void main(String[] args){

Scanner result = new Scanner(System.in);
int myMark = 0;

while(myMark >=0 && myMark <=100){

System.out.println("Please enter the percentage you have received:");
myMark = result.nextInt();

if(myMark <=49 && myMark >=0){

System.out.println("You have failed!");
}

else if(myMark <=59 && myMark >=50){
System.out.println("You have passed!");
}

else if(myMark <=69 && myMark >=60){
System.out.println("You have received a Credit");
}

else if(myMark <=79 && myMark >=70){
System.out.println("You have received a Distinction!");
}

else if(myMark <=100 && myMark >=80){
System.out.println("You have received a High Distinction");
}

else{
System.out.println("Please enter a whole number");
}


}

}


}

最佳答案

使用前需要先定义myMark。如下所示:

  Scanner result = new Scanner(System.in); 
int myMark = result.nextInt();

while(myMark >=0 && myMark <= 100){

System.out.println("Please enter the percentage you have received:");
myMark = result.nextInt();
..................

关于java - while 循环中的输入(扫描仪)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12775559/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com