Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 验证用户输入_Java_While Loop - Fatal编程技术网

Java 验证用户输入

Java 验证用户输入,java,while-loop,Java,While Loop,我几乎完成了这项工作,但在我的代码中有两件事困扰着我。 当我向用户查询测试分数时,如果分数不在0-100范围内,我不想接受它,然后告诉他们为什么,并要求另一个输入。 我还想在他们的平均分数旁边打印他们平均分数的字母分数。 由于某些原因,当我尝试检查以确保输入的分数在0-100范围内时,If-logic语句不起作用。 此外,我也不知道如何打印字母等级,但我没有得到任何错误输出,因此我认为我在正确的轨道上。我认为我可以在循环时主要使用我的计算机上的指针来检查数字是否在0-100范围内。我将不胜感激。

我几乎完成了这项工作,但在我的代码中有两件事困扰着我。 当我向用户查询测试分数时,如果分数不在0-100范围内,我不想接受它,然后告诉他们为什么,并要求另一个输入。 我还想在他们的平均分数旁边打印他们平均分数的字母分数。 由于某些原因,当我尝试检查以确保输入的分数在0-100范围内时,If-logic语句不起作用。 此外,我也不知道如何打印字母等级,但我没有得到任何错误输出,因此我认为我在正确的轨道上。我认为我可以在循环时主要使用我的计算机上的指针来检查数字是否在0-100范围内。我将不胜感激。 这是我的密码:

    import java.text.DecimalFormat;
import java.util.Scanner;
public class GradeReport 
{
 String name;
 int score1, score2, score3;
 double average;
 String grade;
 public GradeReport()  //creates the first constructor
 {
  Scanner sc = new Scanner (System.in);

  System.out.println ("Enter student's name: ");
  name = sc.nextLine();

  System.out.println ("Enter first grade: "); //try while loops to get grade in between 0-100
    score1 = sc.nextInt();
    while
        (score1 <0 || score1 > 100);
    System.out.println("please enter a grade 0-100"); //checks that score is inclusive 1-100

    System.out.println ("Enter second grade: ");
    score2 = sc.nextInt();
    while
        score2 <0 || score2 > 100;
    System.out.println("please enter a grade 0-100");//checks that score is inclusive 1-100

    System.out.println ("Enter third grade: ");
    score3 = sc.nextInt();
    while
        score3 <0 || score3 >100;
    System.out.println("please enter a grade 0-100");//checks that score is inclusive 1-100
 }
 public GradeReport (String v1, int v2, int v3, int v4)
 {
  name = v1;  //these are to initialize the variables so that I don't get null for the second set of results.
  score1 = v2;
  score2 = v3;
  score3 = v4;
 }
 public void calculateAvg()
 {
  average = (double)((score1 + score2 + score3) / 3.0);


 }
 public String calculateGrade()
 {
  if (average >= 90)
   grade = "A";
  else if (average >= 80)
   grade = "B";
  else if (average >= 70)
   grade = "C";
  else if (average >= 60)
   grade = "D";
  else
   grade = "F";
  return grade;
 }

 public String toString()
 {
  DecimalFormat fmt = new DecimalFormat ("0.00"); //to format average to 2 decimal places
  String gradeReport = name + "\n " + Double.toString(score1) + "\t" + Double.toString(score2)+ "\t" + Double.toString(score3) + "\n" + fmt.format(average) + grade;
  return gradeReport;
 }

 public static void main (String[] args)
 {
  GradeReport gr1 = new GradeReport();
  GradeReport gr2 = new GradeReport("Col Een", 76, 76, 75);
  gr1.calculateAvg();
  gr1.calculateGrade();
  gr2.calculateAvg();
  gr2.calculateGrade();
  System.out.println(gr1);
  System.out.println(gr2);
 }

}
导入java.text.DecimalFormat;
导入java.util.Scanner;
公开课成绩报告
{
字符串名;
智力得分1,得分2,得分3;
双倍平均;
串级;
public GradeReport()//创建第一个构造函数
{
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入学生姓名:”);
name=sc.nextLine();
System.out.println(“输入第一等级:”;//尝试while循环以获得0-100之间的等级
分数1=sc.nextInt();
虽然
(得分1100分);
System.out.println(“请输入0-100分”);//检查分数是否包含1-100分
System.out.println(“输入二级:”);
分数2=sc.nextInt();
虽然
得2100分;
System.out.println(“请输入0-100分”);//检查分数是否包含1-100分
System.out.println(“进入三年级:”);
分数3=sc.nextInt();
虽然
得3100分;
System.out.println(“请输入0-100分”);//检查分数是否包含1-100分
}
公共等级报告(字符串v1、int v2、int v3、int v4)
{
name=v1;//这些用于初始化变量,以便第二组结果不会为null。
分数1=v2;
得分2=v3;
得分3=v4;
}
public void calculateAvg()
{
平均得分=(双倍)((得分1+得分2+得分3)/3.0);
}
公共字符串calculateGrade()
{
如果(平均值>=90)
等级=“A”;
否则,如果(平均值>=80)
等级=“B”;
否则,如果(平均值>=70)
等级=“C”;
否则,如果(平均值>=60)
职系=“D”;
其他的
等级=“F”;
返回等级;
}
公共字符串toString()
{
DecimalFormat fmt=新的DecimalFormat(“0.00”);//将平均值设置为小数点后两位
String gradeReport=name+“\n”+Double.toString(分数1)+“\t”+Double.toString(分数2)+“\t”+Double.toString(分数3)+“\n”+fmt.format(平均值)+分数;
退货报告;
}
公共静态void main(字符串[]args)
{
成绩报告gr1=新成绩报告();
成绩报告gr2=新成绩报告(“Coleen”,76,76,75);
gr1.calculateAvg();
gr1.calculateGrade();
gr2.calculateAvg();
gr2.calculateGrade();
系统输出打印项次(gr1);
系统输出打印项次(gr2);
}
}

<代码> > p>某些代码对我来说是不可编译的,但除此之外,请考虑以下三行代码之间的差异:

while (score1 <0 || score1 > 100)

<>你的一些代码看起来对我来说是不可编译的,但除此之外,考虑下面三行代码之间的差异:

while (score1 <0 || score1 > 100)
一些评论

压痕

请尽量与缩进样式保持一致。它使您的代码更易于阅读

空白

注意如何管理空白。当你阅读一本书、一本杂志或一个网页时,空格用来分隔思想——段落、章节等。同样,空格也应该用来分隔你的职能或职能中的思想。例如:

void f1() { do(); domore(); } void f2() { doAnotherThing(); andYetAnother(); } void f3() { do1(); do2(); do3(); do4(); } 变量命名

忽略这个部分,如果它现在没有意义-认为这是一个糟糕的介绍你稍后会学习的东西。 如果您有选择权,您应该总是有意义地命名变量。GradeReport构造函数有4个变量,如果您无法访问源代码,则其用途完全不清楚:

public GradeReport (String v1, int v2, int v3, int v4) 如果我们用“名称”替换v1

公共等级报告(字符串名称,int v2,int v3,int v4) { this.name=name;//这些用于初始化变量,以便第二组结果不会为null。 该系数为1=v2; 这1.2=v3; 这1.3=v4; } 然后将v2替换为score1

public GradeReport (String name, int score1, int v3, int v4) { this.name = name; //these are to initialize the variables so that I don't get null for the second set of results. this.score1 = score1; this.score2 = v3; this.score3 = v4; } 公开成绩报告(字符串名称、整数分数1、整数v3、整数v4) { this.name=name;//这些用于初始化变量,以便第二组结果不会为null。 this.score1=得分1; 这1.2=v3; 这1.3=v4; } 一些评论

压痕

请尽量与缩进样式保持一致。它使您的代码更易于阅读

空白

注意如何管理空白。当你阅读一本书、一本杂志或一个网页时,空格用来分隔思想——段落、章节等。同样,空格也应该用来分隔你的职能或职能中的思想。例如:

void f1() { do(); domore(); } void f2() { doAnotherThing(); andYetAnother(); } void f3() { do1(); do2(); do3(); do4(); } 变量命名

忽略这个部分,如果它现在没有意义-认为这是一个糟糕的介绍你稍后会学习的东西。 如果您有选择权,您应该总是有意义地命名变量。GradeReport构造函数有4个变量,如果您无法访问源代码,则其用途完全不清楚:

public GradeReport (String v1, int v2, int v3, int v4) 如果我们用“名称”替换v1

公共等级报告(字符串名称,int v2,int v3,int v4) { this.name=name;//这些用于初始化变量,以便第二组结果不会为null。 该系数为1=v2; 这1.2=v3; 这1.3=v4; } 然后将v2替换为score1

public GradeReport (String name, int score1, int v3, int v4) { this.name = name; //these are to initialize the variables so that I don't get null for the second set of results. this.score1 = score1; this.score2 = v3; this.score3 = v4; } 公开成绩报告(字符串名称、整数分数1、整数v3、整数v4) { this.name=name;//这些用于初始化变量,以便第二组结果不会为null。 这1.score1=score1; 这1.2=v3; 这1.3=v4; }
啊。所以你建议使用do-while循环而不是while-lo循环 if(condition) line1 line2 while (score1 <0 || score1 > 100); while score2 <0 || score2 > 100; score = readLine() while(...) System.out.println(...) score = readline() while(score is invalid) { print error score = readline() } public GradeReport (String v1, int v2, int v3, int v4) public GradeReport (String v1, int v2, int v3, int v4) { this.name = v1; //these are to initialize the variables so that I don't get null for the second set of results. this.score1 = v2; this.score2 = v3; this.score3 = v4; } public GradeReport (String name, int v2, int v3, int v4) { this.name = name; //these are to initialize the variables so that I don't get null for the second set of results. this.score1 = v2; this.score2 = v3; this.score3 = v4; } public GradeReport (String name, int score1, int v3, int v4) { this.name = name; //these are to initialize the variables so that I don't get null for the second set of results. this.score1 = score1; this.score2 = v3; this.score3 = v4; }