Java循环数系列
这是我的代码,当n>0 sout时,它似乎不执行代码。您输入了一个正数,或者我认为我做错了Java循环数系列,java,while-loop,logical-operators,Java,While Loop,Logical Operators,这是我的代码,当n>0 sout时,它似乎不执行代码。您输入了一个正数,或者我认为我做错了 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 co
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);
}
}
但是在这个问题中,如果用户只键入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?
你可以试试下面的代码
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");
}
}
import java.util.Scanner;
班速{
公共静态void main(字符串[]args){
扫描器x=新扫描器(System.in);
int-num;
int neg=0,count=0,pos=0;
System.out.println(“输入任何数字以继续。输入0以停止:”;
num=x.nextInt();
如果(num==0){
系统输出打印(“您立即停止”);
系统出口(0);
}
while(num!=0){
计数++;
如果(数值>0){
pos++;
}
if(num<0){
neg++;
}
num=x.nextInt();
如果(计数=1){
}
}
如果(计数==1&&num>0){
系统输出打印(“您输入了一个正数”);
如果(计数=1&&num<0){
System.out.print(“您输入了负数”);
}
系统出口(0);
}
System.out.println(“您输入了”+pos+“正数”);
System.out.println(“您输入了”+neg+“负数”);
}
}
退出while循环时,“num”始终为零。因此,测试
if(count == 1 && num > 0)
总是失败。试一试
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
}
还有台词
if(count==1){
}
什么也不做。您正在检查if块内的if
num<0
,该if块检查ifnum>0
num
不能同时为正和负。0为正,因此应该像num>-1@NoorNawaz它对正数有效,但当我输入-2和0时,它仍然会给我“你输入了一个正数”为什么会这样?我应该使用什么条件?写在答案中。你必须考虑你要测试的是什么。这是对的,但是负数字的输出是错误的。当我输入-2和0时,输出是你输入的1个数字0正1负它需要“你输入了一个负数,但我似乎看不出来。我建议你拿一张纸。然后在心里一步一步地浏览每一行代码,在每一行中,在你前进的过程中更新纸上每个变量的值。对于一个正数的跑步、一个负数的跑步和一个以上数字的跑步,都要这样做。这会很快告诉你你做错了什么。如果您有权访问调试器,请使用该调试器。
if(count==1){
}