简体   繁体   English

Java系列循环数

[英]Java Series of number of loop

Here is my code it doesn't seem to perform the code when n > 0 sout.you entered a positive number, Or i think i'm doing it wrong? 这是我的代码,当n> 0 sout.you输入一个正数时,似乎没有执行该代码,或者我认为我做错了吗?

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

Scanner x = new Scanner(System.in);
int num;
int neg = 0, count = 0, pos = 0;

System.out.println("Enter any number to continue. Enter 0 to stop : ");
num = x.nextInt();

if(num==0){
System.out.print("You immediately stop");
System.exit(0);
}

while (num != 0) {
    count ++;
    if (num > 0 ) {
        pos ++;
    } 
    if (num < 0 ) {
        neg ++;

    }
     num = x.nextInt();
    if(count==1){
   }
}
if(count == 1 && num > 0) {

        System.out.print("You entered a positive number");

    if(count ==1 && num < 0) {
        System.out.print("You entered a negative number"); // this code is not performing why?
     } 
System.exit(0);
}  
System.out.printf("You Entered %d numbers\n",count);
System.out.printf("%d positive \n",pos);
System.out.printf("%d negative\n",neg);
}
}

Here is the outputs that i have which is correct. 这是我的输出是正确的。

My output
Enter a number to continue. Enter 0 to stop :
5
6
-8
0               // doesn't count as a number.
You entered 3 numbers.
2 positive
1 negative

BUT in this problem i want the output to be different if the user only typed 1 number. 但是在这个问题上,如果用户只键入1个数字,我希望输出是不同的。

 What i want the output to be >
 Enter a number to continue. Enter 0 to stop :
 2
 0
 You Entered a positive number. // same with a negative number?

When you exit from the while loop, 'num' is always zero. 从while循环退出时,“ num”始终为零。 Therefore the test 因此测试

if(count == 1 && num > 0) 

always fails. 总是失败。 Try 尝试

if(count == 1) {
    if (pos > 0) {
        System.out.print("You entered a positive number");
    } else {
         System.out.print("You entered a negative number");
    }
} else {
    //...put the code for more than one number here
}

Also the lines 还有线

if(count==1){
}

do nothing. 没做什么。

You could try the below code 您可以尝试以下代码

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

Scanner x = new Scanner(System.in);
int num;
int neg = 0, count = 0, pos = 0;

System.out.println("Enter any number to continue. Enter 0 to stop : ");
num = x.nextInt();

if(num==0){
System.out.print("You immediately stop");
System.exit(0);
}

while (num != 0) {
    count ++;
    if (num > 0 ) {
        pos ++;
    } 
    if (num < 0 ) {
        neg ++;

    }
     num = x.nextInt();
    if(count==1){
   }
}
if(count == 1 && num > 0) {

        System.out.print("You entered a positive number");

    if(count ==1 && num < 0) {
        System.out.print("You entered a negative number");
     } 
System.exit(0);
}  

  System.out.println("You Entered "+pos+" positive numbers");
  System.out.println("You Entered "+neg+" negative numbers");
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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