Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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 - Fatal编程技术网

Java 我正在尝试编写一个程序,允许用户输入一系列考试分数作为整数

Java 我正在尝试编写一个程序,允许用户输入一系列考试分数作为整数,java,Java,我正在尝试编写一个程序,允许用户输入一系列考试分数作为整数 我希望输出看起来像: 输入整数,或-99退出:80 输入整数,或-99退出:95 输入整数,或-99退出:65 输入整数,或-99退出:-99 最大:95 最小:65 第二轮: 输入整数,或-99退出:-99 您没有输入任何数字 package chapter5; import java.util.Scanner; public class Grades { public static void main(String[

我正在尝试编写一个程序,允许用户输入一系列考试分数作为整数

我希望输出看起来像:

输入整数,或-99退出:80

输入整数,或-99退出:95

输入整数,或-99退出:65

输入整数,或-99退出:-99

最大:95 最小:65

第二轮:

输入整数,或-99退出:-99

您没有输入任何数字

  package chapter5;


import java.util.Scanner; 
public class Grades
{
   public static void main(String[] args)
   {

      int min = 100;       // Hold smallest score
      int max = 0;         // Hold largest score

      // Create a Scanner object for keyboard input.
      Scanner keyboard = new Scanner(System.in);

      // Display general instructions.
      System.out.print("Enter an integer, or -99 to quit: ");
     int score = keyboard.nextInt();
      if(score<0||score>100){
          System.out.println("You did not enter any numbers.");
      }

      // Input exam scores until -99 is entered.
      if(score>=0||score<=100){
      while (score != -99)
      {
         // Add points to totalPoints.
         if (score > max)
            max = score;
         if (score < min)
            min = score;
         // Get the next number of points.
         System.out.print("Enter an integer, or -99 to quit: ");
         score = keyboard.nextInt();
          if(score==-99){
                System.out.println();}

                else if(score!=-99&&score<0||score>100){
          System.out.println("You did not enter any numbers.");}

         }
      }
      // Display the largest and smallest score.
      System.out.println("Largest: " + max);
      System.out.println("Smallest: " + min);


   }
}
我记下了第一部分,但当我输入-99时,我似乎无法找到如何获得“你没有输入任何数字”行

这就是我目前所拥有的

import java.util.Scanner;    // Needed for the Scanner class

/**
   This program shows the largest and smallest exam scores. The user
   enters a series of exam scores, then -99 when finished.
   UPDATED to show even number of points using if-statement
*/

public class Grades
{
   public static void main(String[] args)
   {
      int score = 0;       // Exam score
      int min = 100;       // Hold smallest score
      int max = 0;         // Hold largest score

      // Create a Scanner object for keyboard input.
      Scanner keyboard = new Scanner(System.in);

      // Display general instructions.
      System.out.println("Enter an integer, or -99 to quit: ");
      System.out.println();

      // Get the first exam score.
      System.out.print("Enter an integer, or -99 to quit: ");
      score = keyboard.nextInt();

      // Input exam scores until -99 is entered.
      while (score != -99)
      {
         // Add points to totalPoints.
         if (score > max)
            max = score;
         if (score < min)
            min = score;
         // Get the next number of points.
         System.out.print("Enter an integer, or -99 to quit: ");
         score = keyboard.nextInt();
      }

      // Display the largest and smallest score.
      System.out.println("Largest: " + max);
      System.out.println("Smallest: " + min);
   }
}
import java.util.Scanner;//Scanner类所需的
/**
此程序显示最大和最小的考试分数。用户
输入一系列考试分数,完成后为-99。
更新为使用if语句显示偶数个点
*/
公营班级职系
{
公共静态void main(字符串[]args)
{
int score=0;//考试分数
int min=100;//保持最低分数
int max=0;//得分最高
//为键盘输入创建扫描仪对象。
扫描仪键盘=新扫描仪(System.in);
//显示一般说明。
System.out.println(“输入一个整数,或-99退出:”;
System.out.println();
//获得第一次考试的分数。
System.out.print(“输入一个整数,或-99退出:”;
score=keyboard.nextInt();
//输入考试分数直到输入-99。
同时(分数=-99)
{
//将点添加到totalPoints。
如果(分数>最大值)
最大值=分数;
如果(分数
在课程结束时:

if (max == 0 && min == 100)
    System.out.println("You did not enter any numbers");
else{
    System.out.println("Largest: " + max);
    System.out.println("Smallest: " + min);
}

引入一个对输入的数字进行计数并在while循环中增加的变量怎么样

或者,您也可以在while循环后检查数字是否更改:

if(min > max) {
    System.out.println("You did not enter any numbers.");
}
else {
    System.out.println("Largest: " + max);
    System.out.println("Smallest: " + min);
}
这将起作用,因为在开始时,您将
min
变量初始化为
100
,将
max
变量初始化为
0
,这将导致
min>max
检查是否未输入任何数字。

公共静态void main(字符串[]args){
public static void main(String[] args) {
    int score = 0;       // Exam score
    int min = 100;       // Hold smallest score
    int max = 0;         // Hold largest score
    boolean isNumberEntered = false; //Test if any number has been entered.

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Input exam scores until -99 is entered.
    while (score != -99)
    {
        // Get the next number of points.
        System.out.print("Enter an integer, or -99 to quit: ");
        score = keyboard.nextInt();

        if (score == -99) {
            break;
        } else {
            // If the first number is entered
            if (!isNumberEntered)
                isNumberEntered = true;
            // Add points to totalPoints.
            if (score > max)
                max = score;
            if (score < min)
                min = score;
        }


    }

    if (isNumberEntered) {
        // Display the largest and smallest score.
        System.out.println("Largest: " + max);
        System.out.println("Smallest: " + min);
    } else {
        System.out.println("You did not enter any numbers!");
    }

}
int score=0;//考试分数 int min=100;//保持最低分数 int max=0;//得分最高 布尔值isNumberEntered=false;//测试是否输入了任何数字。 //为键盘输入创建扫描仪对象。 扫描仪键盘=新扫描仪(System.in); //输入考试分数直到输入-99。 同时(分数=-99) { //获得下一个点数。 System.out.print(“输入一个整数,或-99退出:”; score=keyboard.nextInt(); 如果(分数==-99){ 打破 }否则{ //如果输入了第一个数字 如果(!IsNumberRentered) isNumberEntered=true; //将点添加到totalPoints。 如果(分数>最大值) 最大值=分数; 如果(分数

你试试这个怎么样?是否希望在添加一个数字之前一直处于循环中,还是按您希望的方式添加?

当您输入0和100时,以下代码不起作用:

if (max = 0 && min = 100)
    System.out.println("You did not enter any numbers");
else{
    System.out.println("Largest: " + max);
    System.out.println("Smallest: " + min);
}
使用额外的布尔变量检查任何与-99不同的输入是一个不错的选择:

public class Grades
{
    public static void main(String[] args)
    {
        int score = 0;       // Exam score
        int min = 100;       // Hold smallest score
        int max = -100;         // Hold largest score
        boolean isAnyScore = false;

        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        // Display general instructions.
        System.out.print("Enter an integer, or -99 to quit: ");
        System.out.println();

        // Input exam scores until -99 is entered.
        while(true)
        {
            System.out.print("Enter an integer, or -99 to quit: ");
            score = keyboard.nextInt(); //Get the exam score.
            if(score == -99) { break; }
            else { isAnyScore = true; }

            // Add points to totalPoints.
            if (score > max) { max = score; }
            if (score < min) { min = score; }
        }

        // Display the largest and smallest score.
        if(!isAnyScore) { System.out.println("You did not enter any numbers"); }
        else
        {
            System.out.println("Largest: " + max);
            System.out.println("Smallest: " + min);
        }
    }
}
公共课成绩
{
公共静态void main(字符串[]args)
{
int score=0;//考试分数
int min=100;//保持最低分数
int max=-100;//得分最高
布尔isAnyScore=false;
//为键盘输入创建扫描仪对象。
扫描仪键盘=新扫描仪(System.in);
//显示一般说明。
System.out.print(“输入一个整数,或-99退出:”;
System.out.println();
//输入考试分数直到输入-99。
while(true)
{
System.out.print(“输入一个整数,或-99退出:”;
score=keyboard.nextInt();//获取考试分数。
如果(分数==-99){break;}
else{isAnyScore=true;}
//将点添加到totalPoints。
如果(分数>最大值){max=score;}
如果(分数

变量isAnyScore
为false。当您在第一次循环运行中输入-99时,它仍然将为false,因为并没有赋值。当您在第一次循环运行中输入-99以外的内容时,它将始终为true(在任何循环运行中都将被视为true)。当isAnyScore从false变为true时,它将始终为true,因为您总是指定true,而不是false。

我认为分数在100到0之间。因此我做了一些更改。如果输入<0且>100,则输出为“您未输入任何数字”

包第5章;
导入java.util.Scanner;
公营班级职系
{
公共静态void main(字符串[]args)
{
int最小值=100;
public class Grades
{
 public static void main(String[] args) {
        int score = 0;       // Exam score
        int min = 100;       // Hold smallest score
        int max = 0;         // Hold largest score
        Scanner keyboard = new Scanner(System.in);
        // Display general instructions.
        int i = 1;
        do {
            System.out.println("Enter an integer, or -99 to quit: ");
            score = keyboard.nextInt();
            if (score > max) {
                max = score;
            }
            if (score < min) {
                min = score;
            }
            i++;
            if (i > 5) {
                break;
            }
        } while ((score != -99));

        // Display the largest and smallest score.
        System.out.println("Largest: " + max);
        System.out.println("Smallest: " + min);
    }
}