字符串中的正数和负数

时间:2014-12-19 16:03:05

标签: java

我必须制作一个程序,允许用户输入整数,直到他按'0'。该程序必须打印: 1)输入的数字的总数 2)正数的数量 3)积极的平均值 4)负数的数量 5)否定的总和

到目前为止,我所能做的就是让“输入直到'0'被按下”并查找输入数字的数量,这对我和我的编程技巧来说很重要。我在试图找出数字是正数还是负数时遇到了麻烦。可能我没有比较正确,所以如果我得到一些先进的人的帮助,我会很高兴。

到目前为止,这是我的代码:

import java.io.*; 

class Nums { 
  public static void main(String args[])  
    throws IOException 
  { 
    BufferedReader br = new BufferedReader(new 
                            InputStreamReader(System.in)); 
    String str;
    int EnteredNumbers = -1;
    int Positive = 0;
    int Negative = 0;
    int NegativeSum = 0;
    double AveragePositive = 0;

    System.out.println("Enter '0' to quit."); 
    System.out.println("Enter Numbers: "); 
    do { 
      EnteredNumbers++;
      str = br.readLine();

    } while(!str.equals("0")); 
      System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
      System.out.println("You have have entered "+Positive+ " Positive numbers!");
      System.out.println("The Average of the Positive Numebers is "+AveragePositive+ "!");
      System.out.println("You have have entered "+Negative+ " Negative numbers!");
      System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
  } 
}

7 个答案:

答案 0 :(得分:2)

首先指出Integer.parseInt在java中用于从String转换为int。 其次,您需要有一个额外的变量来存储和累积正数的总和。我添加了一个try-catch异常块来处理错误。

以下是供参考的代码。

import java.io.*; 

public class Nums { 
  public static void main(String args[])  
    throws IOException 
  { 
    BufferedReader br = new BufferedReader(new 
                            InputStreamReader(System.in)); 
    String str;
    int EnteredNumbers = -1;
    int Positive = 0;
    int Negative = 0;
    int NegativeSum = 0;
    int PositiveSum = 0; // Added extra variable
    double AveragePositive = 0;

    System.out.println("Enter '0' to quit."); 
    System.out.println("Enter Numbers: "); 
   try{
    do { 
      EnteredNumbers++;
      str = br.readLine();

    int num = Integer.parseInt(str);


    if (num>0)
      {
        Positive++;
        PositiveSum+=num;
      }
      else if (num<0)
      {
        Negative++;
        NegativeSum+=num;
      }
    } while(!str.equals("0")); 
    AveragePositive = (double)PositiveSum/(double)Positive;
      System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
      System.out.println("You have have entered "+Positive+ " Positive numbers!");
      System.out.println("The Average of the Positive Numebers is "+AveragePositive+ "!");
      System.out.println("You have have entered "+Negative+ " Negative numbers!");
      System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
    }
    catch (java.lang.NumberFormatException e)
    {
        System.out.println("Wrong format");
    }  

} 
}

如果没有错误则输出

Enter '0' to quit.
Enter Numbers: 
1
5
5
-5
-5
0
You have have entered 5 numbers!
You have have entered 3 Positive numbers!
The Average of the Positive Numebers is 3.6666666666666665!
You have have entered 2 Negative numbers!
The Sum of the Negative numbers is -10!

答案 1 :(得分:1)

通过将String解析为int

进行检查
Integer.parseInt(str)>0  //positive number
Integer.parseInt(str)<0 //negative



do { 
      EnteredNumbers++;
      str = br.readLine();
      int number= Integer.parseInt(str);
      if(number>0){  //do positive stuffs}
      else if(number<0){//do negative stuffs}
    } while(!str.equals("0")); 

答案 2 :(得分:1)

希望以下代码能够实现您想要的目标

import java.io.*; 

class Nums { 
  public static void main(String args[])  
    throws IOException 
  { 
    BufferedReader br = new BufferedReader(new 
                            InputStreamReader(System.in)); 
    String str;
    int EnteredNumbers = -1;
    int Positive = 0;
    int Negative = 0;
    int NegativeSum = 0;
    double AveragePositive = 0;
    double PositiveSum = 0;

    System.out.println("Enter '0' to quit."); 
    System.out.println("Enter Numbers: "); 
    do { 
      EnteredNumbers++;
      str = br.readLine();
      if(Integer.parseInt(str) > 0){
          Positive++;
          PositiveSum = PositiveSum + Integer.parseInt(str);
          AveragePositive = PositiveSum/Positive;
      }
      if(Integer.parseInt(str) < 0){
          Negative++;
          NegativeSum = NegativeSum + Integer.parseInt(str);
      }

    } while(!str.equals("0")); 
      System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
      System.out.println("You have have entered "+Positive+ " Positive numbers!");
      System.out.println("The Average of the Positive Numebers is "+AveragePositive+ "!");
      System.out.println("You have have entered "+Negative+ " Negative numbers!");
      System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
  } 
}

答案 3 :(得分:0)

创建一个列表并继续将数字添加到List中。你应该做点什么:

List<Integer> numberList = new ArrayList<Integer>();

do { 
  str = br.readLine();
  int number = Integer.parseInt(str);
} while(number != 0); 

然后使用for循环来计算如下:

int negativeNumberSum = 0;
int positiveNumberSum = 0;
int positiveNumberCounts = 0;
for (int i =0; i<numberList.size(); i++) {
    if (numberList.get(i) >= 0) {
        positiveNumberSum += numberList.get(i);
        positiveNumberCounts ++;
    } else {
        negativeNumberSum += numberList.get(i);
    }
}

//print average of positive as positiveNumberSum /(double) positiveNumberCounts if positiveNumberCounts != 0
//print average of positive as negativeNumberSum / (double)(numberList.size() - positiveNumberCounts) same if (numberList.size() - positiveNumberCounts) != 0
//print positive count as positiveNumberSum  and same for negative.
//

答案 4 :(得分:0)

尝试使用Integer.parseInt()从字符串int获取str,并检查整数是< 0(否定)还是> 0(正)

答案 5 :(得分:0)

我添加了一个名为PositiveSum的额外变量来协助AveragePositive计算。

为了执行此计算,您需要确保该值为Integer,因此我添加了解析语句。

class Nums { 
  public static void main(String args[])  
    throws IOException 
  { 
    BufferedReader br = new BufferedReader(new 
                        InputStreamReader(System.in)); 
    String str;
    int EnteredNumbers = -1;
    int Positive = 0;
    int Negative = 0;
    int NegativeSum = 0;
    int PositiveSum = 0; 
    double AveragePositive = 0;

    System.out.println("Enter '0' to quit."); 
    System.out.println("Enter Numbers: "); 
    do { 
      EnteredNumbers++;
      str = br.readLine();
      int number = Integer.parseInt(str);
      if(number<0) {
          Negative++;
          NegativeSum += number;
      } else if(number>0) {
          Positive++;
          PositiveSum += number;

      }

    } while(!str.equals("0"));
      AveragePositive = PositiveSum / EnteredNumbers;
      System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
      System.out.println("You have have entered "+Positive+ " Positive numbers!");
      System.out.println("The Average of the Positive Numebers is "+AveragePositive+ "!");
      System.out.println("You have have entered "+Negative+ " Negative numbers!");
      System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
  } 
}

答案 6 :(得分:0)

更改您的do-while循环:它正在使用字符串,而不是整数

do { 
      EnteredNumbers++;
      num = sc.nextInt();

    } while(num != 0);

此外,您需要import scanner实用程序:

import java.util.Scanner;

请勿使用BufferedReader,但请使用:

static Scanner sc = new Scanner(System.in);

此外,您还希望在do-while循环中使用检查器来显示正数和负数:

if(Integer.parseInt(num)<0) { Negative++; }
else { Positive++; }

如果有人没有输入try-catch,我还添加了int语句来处理错误:

      try {
      num = sc.nextInt();
      }
      catch(Exception e) {System.out.println("Invalid integer"); System.exit(0); }

我添加了一些代码,在打印数字时关闭了0,因为我知道当你输入它时它将包括0以退出。 将您的class语句更改为public,它只是编程:

public class Nums {

另外,我在主代码和平均器中添加了一个负数计数器。 这是完整的代码,以避免混淆:

import java.io.*; 
import java.util.Scanner;
public class prime { 
  static Scanner sc = new Scanner(System.in);
  public static void main(String args[])  
  { 
    int EnteredNumbers = 0;
    int Positive = 0;
    int Negative = 0;
    int NegativeSum = 0;
    double AveragePositive = 0;
    int num = 0;
    int PosTotal = 0;
    System.out.println("Enter '0' to quit."); 
    System.out.println("Enter Numbers: "); 
    do { 
      try {
      num = sc.nextInt();
      }
      catch(Exception e) {System.out.println("Invalid integer!"); System.exit(0); }
      if(num<0) { Negative++; NegativeSum += num; }
      if(num>0){ Positive++; PosTotal += num; }
      EnteredNumbers++;
    } while(num != 0); 
      AveragePositive = PosTotal / Positive;
      EnteredNumbers -= 1;
      System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
      System.out.println("You have have entered "+Positive+ " Positive numbers!");
      System.out.println("The Average of the Positive Numbers is "+AveragePositive+ "!");
      System.out.println("You have have entered "+Negative+ " Negative numbers!");
      System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
  } 
}

希望它有所帮助! 注意我已经过试用,它的工作非常出色。如果您有任何错误,请告诉我:)