计算负数和正数的整数,使用 while 循环。

huangapple go评论76阅读模式
英文:

count negative and positive integer with while loop

问题

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int count = 0;
        int negative = 0;
        int positive = 0;

        System.out.print("Enter an integer (Program ends if enter 0): ");
        int data = input.nextInt();

        while (data != 0) {
            if (data < 0) {
                negative++;
            } else if (data > 0) {
                positive++;
            }
            count++;
            System.out.print("Enter an integer (Program ends if enter 0): ");
            data = input.nextInt();
        }
        
        System.out.println((positive - 1) + " positive numbers");
        System.out.println(negative + " negative numbers");
    }
}

Output:

run:
Enter an integer (Program ends if enter 0): 1
Enter an integer (Program ends if enter 0): 2
Enter an integer (Program ends if enter 0): 3
Enter an integer (Program ends if enter 0): -1
Enter an integer (Program ends if enter 0): -2
Enter an integer (Program ends if enter 0): -3
Enter an integer (Program ends if enter 0): 0
2 positive numbers
3 negative numbers
BUILD SUCCESSFUL (total time: 11 seconds)
英文:

Need to fix my program.
Write a java program using WHILE LOOP: how many positive and negative numbers? User prompt
the integers and ends with number 0.
You will see in my output that if user enter 1 2 3 -1 -2 -3 0
My output answer: 2 positive / 3 negative.
The first positive number is not count.
Please let me know where is my error. I cannot find it.

    Scanner input= new Scanner(System.in);
    
    //int data;
    int count = 0;
    int negative=0;
    int positive =0;
    
    System.out.print(&quot;Enter an integer (Program ends if enter 0): &quot;);
    int data = input.nextInt();
    
    
    
    while (data !=0) {
    
    System.out.print(&quot;Enter an integer (Program ends if enter 0): &quot;);
    data=input.nextInt();
    //count++;
    if (data &lt; 0){
    negative++;
    }else if (data &gt; 0){
    positive++;
    }
    count++;
  }
    System.out.println(positive + &quot; positive numbers&quot;);
    System.out.println(negative + &quot; negative numbers&quot;);
    
 }
}

output:
run:
Enter an integer (Program ends if enter 0): 1
Enter an integer (Program ends if enter 0): 2
Enter an integer (Program ends if enter 0): 3
Enter an integer (Program ends if enter 0): -1
Enter an integer (Program ends if enter 0): -2
Enter an integer (Program ends if enter 0): -3
Enter an integer (Program ends if enter 0): 0
2 positive numbers
3 negative numbers
BUILD SUCCESSFUL (total time: 11 seconds)

答案1

得分: 2

你的代码跳过了第一个条目,因为你是这样编写的。看一下这些代码行:

System.out.print("Enter an integer (Program ends if enter 0): ");
int data = input.nextInt();

你要求用户输入数据,但是然后你没有对其进行任何操作。所有的数据处理都在while循环中进行。

将这两行代码替换为这样:

int data = 0;

你只需要声明一个变量,用于稍后使用输入,不需要立即使用它。

你还需要修改while条件,以便在循环的第一次迭代中,具有值为0的数据不会自动退出程序:

while ((data != 0) || (count == 0 && data == 0)) {

输出:

输入一个整数(如果输入0,则程序结束):1
输入一个整数(如果输入0,则程序结束):2
输入一个整数(如果输入0,则程序结束):3
输入一个整数(如果输入0,则程序结束):-1
输入一个整数(如果输入0,则程序结束):-2
输入一个整数(如果输入0,则程序结束):-3
输入一个整数(如果输入0,则程序结束):0
3个正数
3个负数
英文:

Your code skips the first entry because you coded it that way. Look at these lines:

System.out.print(&quot;Enter an integer (Program ends if enter 0): &quot;);
int data = input.nextInt();

You ask for the user to enter data, but then you don't do anything with it. All your data handing takes place in the while loop.

Replace those two lines with this:

int data = 0;

All you need to do is declare the variable you use to take input for use later on -- you don't need to use it immediately.

You'll also have to modify the while condition so that on the first iteration of the loop, data with the value 0 will not automatically exit the program:

while ((data !=0) || (count == 0 &amp;&amp; data == 0)) {

OUTPUT:

Enter an integer (Program ends if enter 0): 1
Enter an integer (Program ends if enter 0): 2
Enter an integer (Program ends if enter 0): 3
Enter an integer (Program ends if enter 0): -1
Enter an integer (Program ends if enter 0): -2
Enter an integer (Program ends if enter 0): -3
Enter an integer (Program ends if enter 0): 0
3 positive numbers
3 negative numbers

答案2

得分: 2

似乎你是在循环外部获取了第一个输入。

System.out.print("Enter an integer (Program ends if enter 0): ");
int data = input.nextInt(); // 第一个输入的值 1 存储在这里

而这个 1 没有被检查

while (data != 0) {
    System.out.print("Enter an integer (Program ends if enter 0): ");
    data = input.nextInt(); // 2、3、-1、-2、-3、0 在循环内部。

只有这些值会被检查
英文:

its seems like you are taking first input outside the while loop.

System.out.print(&quot;Enter an integer (Program ends if enter 0): &quot;);
int data = input.nextInt(); // the first input 1 store in here

and this 1 is not checked .

      while (data !=0) {
 System.out.print(&quot;Enter an integer (Program ends if enter 0): &quot;);
data=input.nextInt();// 2,3,-1,-2,-3, 0 are inside the loop.

Only this values are checked.

答案3

得分: 0

你正在覆盖第一个输入,在进入循环后。

  1. 你正在读取输入并检查输入是否不等于零
  2. 在再次进入循环后,你正在从扫描器读取输入并将其存储到变量数据中(这里会覆盖之前的数据)

更新后的代码:

Scanner input = new Scanner(System.in);

// int data;
int count = 0;
int negative = 0;
int positive = 0;

System.out.print("输入一个整数(如果输入0则结束程序):");
int data = input.nextInt();

while (data != 0) {
    // count++;
    if (data < 0) {
        negative++;
    } else if (data > 0) {
        positive++;
    }
    count++;
    
    System.out.print("输入一个整数(如果输入0则结束程序):");
    data = input.nextInt();
}
System.out.println(positive + " 个正数");
System.out.println(negative + " 个负数");
英文:

You are over riding the first input, after entering into the loop.

  1. You are reading the input and checking if input is not equal to zero or not
  2. After entering into the loop again you are reading the input from scanner and storing it to variable data(here the previous data is getting overridden)

updated Code:

Scanner input = new Scanner(System.in);

// int data;
int count = 0;
int negative = 0;
int positive = 0;

System.out.print(&quot;Enter an integer (Program ends if enter 0): &quot;);
int data = input.nextInt();

while (data != 0) {
	// count++;
	if (data &lt; 0) {
		negative++;
	} else if (data &gt; 0) {
		positive++;
	}
	count++;
	
	System.out.print(&quot;Enter an integer (Program ends if enter 0): &quot;);
	data = input.nextInt();
}
System.out.println(positive + &quot; positive numbers&quot;);
System.out.println(negative + &quot; negative numbers&quot;);

答案4

得分: 0

package 循环;

import java.util.Scanner;
import java.util.SortedMap;

public class 问题12 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("你想输入多少个数字?\n");
        int x = sc.nextInt();
        int[] data = new int[x];
        for (int i = 0; i < data.length; i++) {
            System.out.println("请输入第 " + (i + 1) + " 个数字");
            data[i] = sc.nextInt();
        }
        int count = 0;
        int negative = 0;
        int zero = 0;
        for (int i : data) {
            if (i > 0) {
                count++;
            } else if (i < 0) {
                negative++;
            } else if (i == 0) {
                zero++;
            }
        }
        System.out.println("正数的数量为: " + count);
        System.out.println("负数的数量为: " + negative);
        System.out.println("零的数量为: " + zero);
    }
}
英文:
    package Loops;
    import java.util.Scanner;
    import java.util.SortedMap;
    public class Question12 {
        public static void main(String[] args) {
            Scanner sc= new Scanner(System.in);
            System.out.println(&quot;How many numbers you want to enter?\n&quot;);
            int x= sc.nextInt();
            int [] data= new int[x];
            for(int i=0;i&lt;data.length;i++){
                System.out.println(&quot;Enter the number &quot;+(i+1));
                data[i]=sc.nextInt();
            }
            int count=0;
            int negative=0;
            int zero=0;
            for(int i: data){
                if(i&gt;0){
                    count++;
                }else if(i&lt;0){
                    negative++;
                }else if(i==0){
                    zero++;
                }}
            System.out.println(&quot;The positive number count is: &quot;+count);
            System.out.println(&quot;The negative number count is: &quot;+negative);
            System.out.println(&quot;The zero number count is: &quot;+zero);
        }
    }

huangapple
  • 本文由 发表于 2020年10月5日 12:39:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64202566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定