简体   繁体   中英

Reading a series of numbers from the user until -1 is inputted, Java

I am trying to write a program to read a series of numbers from the user until -1 is entered. It should then print the average (with decimals) of those numbers (not including the -1). This is what I have so far, but it does not seem to work properly whenever I input -1. This is what I have thus far:

   import java.util.Scanner;
    import java.util.*;  
    public class Tute1
    {
public static void main (String[] args) {
   Scanner sc = new Scanner(System.in);
  System.out.println("Enter how many numbers you want: ");
  int numb = sc.nextInt();
  int i =0;
  int total =0;
  while (i <= numb) {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter an integer ");
      int myChoice=scan.nextInt();
      total=total+myChoice;
      i=i+1;
if(myChoice == -1) {
          System.out.println("The average is "+ (total+1)/numb);
          break;
}    
}
    System.out.println("The average is "+ total/numb);
     }  
     }

Your code seems a bit odd. Here is how I would do it

import java.util.Scanner;
import java.util.*;

public class Tute1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = 0;
        int total = 0;
        while (true) {
            System.out.println("Enter an integer ");
            int myChoice = scan.nextInt();
            if (myChoice != -1) {
                total = total + myChoice;
                i += 1;
            } else {
                break;
            }

        }
        float average = total / i;
        System.out.println("The average is " + average);

    }

Hope this helps. You can add try-catch and stuff to make it so that user does not exploit this

Few things:

  • first of all you dont need to create a new scanner as your code inside the while loop. Its because the while loop is inside you function scope so every thing you declare on you function the while loop knows about. (It does not work the other way around)
  • second your breaking condition should be inside the while loop the if with break is redundant and unnecessary.
  • third thing you should get rid of numb as its doing nithung
    import java.util.Scanner;
    import java.util.*;
    public class Tute1 {
        public static void main (String[] args) {
            Scanner sc = new Scanner(System.in);
            int numb = sc.nextInt();
            int i =0;
            int total =0;
            while (numb != -1) 
            { 
                total=total+numb;
                i=i+1;
                int numb = sc.nextInt() 
            }
            System.out.println("The average is "+ total/i); 
        }
    }

I think this solution is smaller and more elegant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM